glsl: Make a local variable to avoid restating this array lookup.
[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);
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 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
752 *
753 * "As with all arrays, indices used to subscript gl_TexCoord must
754 * either be an integral constant expressions, or this array must be
755 * re-declared by the shader with a size. The size can be at most
756 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
757 * implementation in preserving varying resources."
758 */
759 const glsl_type *const vec4_array_type =
760 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
761
762 add_variable(instructions, state->symbols,
763 "gl_TexCoord", vec4_array_type, ir_var_shader_out,
764 VARYING_SLOT_TEX0);
765
766 generate_ARB_draw_buffers_variables(instructions, state, false,
767 vertex_shader);
768 }
769
770
771 static void
772 generate_120_vs_variables(exec_list *instructions,
773 struct _mesa_glsl_parse_state *state,
774 bool add_deprecated)
775 {
776 /* GLSL version 1.20 did not add any built-in variables in the vertex
777 * shader.
778 */
779 generate_110_vs_variables(instructions, state, add_deprecated);
780 }
781
782
783 static void
784 generate_130_uniforms(exec_list *instructions,
785 struct _mesa_glsl_parse_state *state)
786 {
787 glsl_symbol_table *const symtab = state->symbols;
788
789 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
790 state->Const.MaxClipPlanes);
791 add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
792 state->Const.MaxVaryingFloats);
793 }
794
795
796 static void
797 generate_130_vs_variables(exec_list *instructions,
798 struct _mesa_glsl_parse_state *state,
799 bool add_deprecated)
800 {
801 generate_120_vs_variables(instructions, state, add_deprecated);
802
803 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
804 add_builtin_variable(instructions, state->symbols,
805 & builtin_130_vs_variables[i]);
806 }
807
808 generate_130_uniforms(instructions, state);
809
810 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
811 * Variables):
812 *
813 * The gl_ClipDistance array is predeclared as unsized and must
814 * be sized by the shader either redeclaring it with a size or
815 * indexing it only with integral constant expressions.
816 *
817 * We represent this in Mesa by initially declaring the array as
818 * size 0.
819 */
820 const glsl_type *const clip_distance_array_type =
821 glsl_type::get_array_instance(glsl_type::float_type, 0);
822
823 add_variable(instructions, state->symbols,
824 "gl_ClipDistance", clip_distance_array_type, ir_var_shader_out,
825 VARYING_SLOT_CLIP_DIST0);
826
827 generate_AMD_vertex_shader_layer_variables(instructions, state, false,
828 vertex_shader);
829 }
830
831
832 static void
833 initialize_vs_variables(exec_list *instructions,
834 struct _mesa_glsl_parse_state *state)
835 {
836 if (state->es_shader) {
837 switch (state->language_version) {
838 case 100:
839 generate_100ES_vs_variables(instructions, state);
840 break;
841 case 300:
842 generate_300ES_vs_variables(instructions, state);
843 break;
844 default:
845 assert(!"Unexpected language version");
846 break;
847 }
848 } else {
849 switch (state->language_version) {
850 case 110:
851 generate_110_vs_variables(instructions, state, true);
852 break;
853 case 120:
854 generate_120_vs_variables(instructions, state, true);
855 break;
856 case 130:
857 generate_130_vs_variables(instructions, state, true);
858 break;
859 case 140:
860 case 150:
861 generate_130_vs_variables(instructions, state, false);
862 break;
863 default:
864 assert(!"Unexpected language version");
865 break;
866 }
867 }
868
869 generate_ARB_draw_instanced_variables(instructions, state, false,
870 vertex_shader);
871 }
872
873
874 /* This function should only be called for ES, not desktop GL. */
875 static void
876 generate_100ES_fs_variables(exec_list *instructions,
877 struct _mesa_glsl_parse_state *state)
878 {
879 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
880 add_builtin_variable(instructions, state->symbols,
881 & builtin_core_fs_variables[i]);
882 }
883
884 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
885 add_builtin_variable(instructions, state->symbols,
886 & builtin_100ES_fs_variables[i]);
887 }
888
889 generate_100ES_uniforms(instructions, state);
890
891 generate_ARB_draw_buffers_variables(instructions, state, false,
892 fragment_shader);
893 }
894
895 static void
896 generate_300ES_fs_variables(exec_list *instructions,
897 struct _mesa_glsl_parse_state *state)
898 {
899 /* Note: we don't add builtin_core_fs_variables, because it contains
900 * gl_FragColor, which is not in GLSL 3.00 ES.
901 */
902
903 for (unsigned i = 0; i < Elements(builtin_300ES_fs_variables); i++) {
904 add_builtin_variable(instructions, state->symbols,
905 & builtin_300ES_fs_variables[i]);
906 }
907
908 generate_300ES_uniforms(instructions, state);
909
910 generate_ARB_draw_buffers_variables(instructions, state, false,
911 fragment_shader);
912 }
913
914 static void
915 generate_110_fs_variables(exec_list *instructions,
916 struct _mesa_glsl_parse_state *state,
917 bool add_deprecated)
918 {
919 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
920 add_builtin_variable(instructions, state->symbols,
921 & builtin_core_fs_variables[i]);
922 }
923
924 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
925 add_builtin_variable(instructions, state->symbols,
926 & builtin_110_fs_variables[i]);
927 }
928
929 if (add_deprecated) {
930 for (unsigned i = 0
931 ; i < Elements(builtin_110_deprecated_fs_variables)
932 ; i++) {
933 add_builtin_variable(instructions, state->symbols,
934 & builtin_110_deprecated_fs_variables[i]);
935 }
936 }
937
938 generate_110_uniforms(instructions, state, add_deprecated);
939
940 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
941 *
942 * "As with all arrays, indices used to subscript gl_TexCoord must
943 * either be an integral constant expressions, or this array must be
944 * re-declared by the shader with a size. The size can be at most
945 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
946 * implementation in preserving varying resources."
947 */
948 const glsl_type *const vec4_array_type =
949 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
950
951 add_variable(instructions, state->symbols,
952 "gl_TexCoord", vec4_array_type, ir_var_shader_in,
953 VARYING_SLOT_TEX0);
954
955 generate_ARB_draw_buffers_variables(instructions, state, false,
956 fragment_shader);
957 }
958
959
960 static void
961 generate_ARB_draw_buffers_variables(exec_list *instructions,
962 struct _mesa_glsl_parse_state *state,
963 bool warn, _mesa_glsl_parser_targets target)
964 {
965 /* gl_MaxDrawBuffers is available in all shader stages.
966 */
967 ir_variable *const mdb =
968 add_builtin_constant(instructions, state->symbols, "gl_MaxDrawBuffers",
969 state->Const.MaxDrawBuffers);
970
971 if (warn)
972 mdb->warn_extension = "GL_ARB_draw_buffers";
973
974 /* gl_FragData is only available in the fragment shader.
975 * It is not present in GLSL 3.00 ES.
976 */
977 if (target == fragment_shader && !state->is_version(0, 300)) {
978 const glsl_type *const vec4_array_type =
979 glsl_type::get_array_instance(glsl_type::vec4_type,
980 state->Const.MaxDrawBuffers);
981
982 ir_variable *const fd =
983 add_variable(instructions, state->symbols,
984 "gl_FragData", vec4_array_type,
985 ir_var_shader_out, FRAG_RESULT_DATA0);
986
987 if (warn)
988 fd->warn_extension = "GL_ARB_draw_buffers";
989 }
990 }
991
992
993 static void
994 generate_ARB_draw_instanced_variables(exec_list *instructions,
995 struct _mesa_glsl_parse_state *state,
996 bool warn,
997 _mesa_glsl_parser_targets target)
998 {
999 /* gl_InstanceIDARB is only available in the vertex shader.
1000 */
1001 if (target != vertex_shader)
1002 return;
1003
1004 if (state->ARB_draw_instanced_enable) {
1005 ir_variable *inst =
1006 add_variable(instructions, state->symbols,
1007 "gl_InstanceIDARB", glsl_type::int_type,
1008 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
1009
1010 if (warn)
1011 inst->warn_extension = "GL_ARB_draw_instanced";
1012 }
1013
1014 bool available_in_core = state->is_version(140, 300);
1015 if (state->ARB_draw_instanced_enable || available_in_core) {
1016 /* Originally ARB_draw_instanced only specified that ARB decorated name.
1017 * Since no vendor actually implemented that behavior and some apps use
1018 * the undecorated name, the extension now specifies that both names are
1019 * available.
1020 */
1021 ir_variable *inst =
1022 add_variable(instructions, state->symbols,
1023 "gl_InstanceID", glsl_type::int_type,
1024 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
1025
1026 if (!available_in_core && warn)
1027 inst->warn_extension = "GL_ARB_draw_instanced";
1028 }
1029 }
1030
1031 static void
1032 generate_AMD_vertex_shader_layer_variables(exec_list *instructions,
1033 struct _mesa_glsl_parse_state *state,
1034 bool warn,
1035 _mesa_glsl_parser_targets target)
1036 {
1037 /* gl_Layer is only available in the vertex shader for the
1038 * AMD_vertex_shader_layer extension. It will also be available in the
1039 * geometry shader when GLSL 1.50 is supported.
1040 */
1041 if (target != vertex_shader)
1042 return;
1043
1044 if (state->AMD_vertex_shader_layer_enable) {
1045 ir_variable *inst =
1046 add_variable(instructions, state->symbols,
1047 "gl_Layer", glsl_type::int_type,
1048 ir_var_shader_out, VARYING_SLOT_LAYER);
1049
1050 if (warn)
1051 inst->warn_extension = "GL_AMD_vertex_shader_layer";
1052 }
1053 }
1054
1055 static void
1056 generate_ARB_shader_stencil_export_variables(exec_list *instructions,
1057 struct _mesa_glsl_parse_state *state,
1058 bool warn)
1059 {
1060 /* gl_FragStencilRefARB is only available in the fragment shader.
1061 */
1062 ir_variable *const fd =
1063 add_variable(instructions, state->symbols,
1064 "gl_FragStencilRefARB", glsl_type::int_type,
1065 ir_var_shader_out, FRAG_RESULT_STENCIL);
1066
1067 if (warn)
1068 fd->warn_extension = "GL_ARB_shader_stencil_export";
1069 }
1070
1071 static void
1072 generate_AMD_shader_stencil_export_variables(exec_list *instructions,
1073 struct _mesa_glsl_parse_state *state,
1074 bool warn)
1075 {
1076 /* gl_FragStencilRefAMD is only available in the fragment shader.
1077 */
1078 ir_variable *const fd =
1079 add_variable(instructions, state->symbols,
1080 "gl_FragStencilRefAMD", glsl_type::int_type,
1081 ir_var_shader_out, FRAG_RESULT_STENCIL);
1082
1083 if (warn)
1084 fd->warn_extension = "GL_AMD_shader_stencil_export";
1085 }
1086
1087 static void
1088 generate_120_fs_variables(exec_list *instructions,
1089 struct _mesa_glsl_parse_state *state,
1090 bool add_deprecated)
1091 {
1092 generate_110_fs_variables(instructions, state, add_deprecated);
1093
1094 for (unsigned i = 0
1095 ; i < Elements(builtin_120_fs_variables)
1096 ; i++) {
1097 add_builtin_variable(instructions, state->symbols,
1098 & builtin_120_fs_variables[i]);
1099 }
1100 }
1101
1102 static void
1103 generate_fs_clipdistance(exec_list *instructions,
1104 struct _mesa_glsl_parse_state *state)
1105 {
1106 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
1107 * Variables):
1108 *
1109 * The built-in input variable gl_ClipDistance array contains linearly
1110 * interpolated values for the vertex values written by the vertex shader
1111 * to the gl_ClipDistance vertex output variable. This array must be
1112 * sized in the fragment shader either implicitly or explicitly to be the
1113 * same size as it was sized in the vertex shader.
1114 *
1115 * In other words, the array must be pre-declared as implicitly sized. We
1116 * represent this in Mesa by initially declaring the array as size 0.
1117 */
1118 const glsl_type *const clip_distance_array_type =
1119 glsl_type::get_array_instance(glsl_type::float_type, 0);
1120
1121 add_variable(instructions, state->symbols,
1122 "gl_ClipDistance", clip_distance_array_type, ir_var_shader_in,
1123 VARYING_SLOT_CLIP_DIST0);
1124 }
1125
1126 static void
1127 generate_130_fs_variables(exec_list *instructions,
1128 struct _mesa_glsl_parse_state *state)
1129 {
1130 generate_120_fs_variables(instructions, state, true);
1131
1132 generate_130_uniforms(instructions, state);
1133 generate_fs_clipdistance(instructions, state);
1134 }
1135
1136
1137 static void
1138 generate_140_fs_variables(exec_list *instructions,
1139 struct _mesa_glsl_parse_state *state)
1140 {
1141 generate_120_fs_variables(instructions, state, false);
1142
1143 generate_130_uniforms(instructions, state);
1144 generate_fs_clipdistance(instructions, state);
1145 }
1146
1147 static void
1148 initialize_fs_variables(exec_list *instructions,
1149 struct _mesa_glsl_parse_state *state)
1150 {
1151 if (state->es_shader) {
1152 switch (state->language_version) {
1153 case 100:
1154 generate_100ES_fs_variables(instructions, state);
1155 break;
1156 case 300:
1157 generate_300ES_fs_variables(instructions, state);
1158 break;
1159 default:
1160 assert(!"Unexpected language version");
1161 break;
1162 }
1163 } else {
1164 switch (state->language_version) {
1165 case 110:
1166 generate_110_fs_variables(instructions, state, true);
1167 break;
1168 case 120:
1169 generate_120_fs_variables(instructions, state, true);
1170 break;
1171 case 130:
1172 generate_130_fs_variables(instructions, state);
1173 break;
1174 case 140:
1175 case 150:
1176 generate_140_fs_variables(instructions, state);
1177 break;
1178 default:
1179 assert(!"Unexpected language version");
1180 break;
1181 }
1182 }
1183
1184 if (state->ARB_shader_stencil_export_enable)
1185 generate_ARB_shader_stencil_export_variables(instructions, state,
1186 state->ARB_shader_stencil_export_warn);
1187
1188 if (state->AMD_shader_stencil_export_enable)
1189 generate_AMD_shader_stencil_export_variables(instructions, state,
1190 state->AMD_shader_stencil_export_warn);
1191 }
1192
1193 void
1194 _mesa_glsl_initialize_variables(exec_list *instructions,
1195 struct _mesa_glsl_parse_state *state)
1196 {
1197 switch (state->target) {
1198 case vertex_shader:
1199 initialize_vs_variables(instructions, state);
1200 break;
1201 case geometry_shader:
1202 break;
1203 case fragment_shader:
1204 initialize_fs_variables(instructions, state);
1205 break;
1206 }
1207 }