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