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