glsl: Add gl_Layer and gl_ViewportIndex builtins to fragment shader
[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_statevars.h"
30 #include "program/prog_instruction.h"
31
32 static const struct gl_builtin_uniform_element gl_NumSamples_elements[] = {
33 {NULL, {STATE_NUM_SAMPLES, 0, 0}, SWIZZLE_XXXX}
34 };
35
36 static const struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
37 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
38 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
39 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
40 };
41
42 static const struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
43 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
44 };
45
46 static const struct gl_builtin_uniform_element gl_Point_elements[] = {
47 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
48 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
49 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
50 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
51 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
52 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
53 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
54 };
55
56 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
57 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
58 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
59 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
60 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
61 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
62 };
63
64 static const struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
65 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
66 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
67 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
68 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
69 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
70 };
71
72 static const struct gl_builtin_uniform_element gl_LightSource_elements[] = {
73 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
74 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
75 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
76 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
77 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
78 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
79 MAKE_SWIZZLE4(SWIZZLE_X,
80 SWIZZLE_Y,
81 SWIZZLE_Z,
82 SWIZZLE_Z)},
83 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
84 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
85 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
86 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
87 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
88 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
89 };
90
91 static const struct gl_builtin_uniform_element gl_LightModel_elements[] = {
92 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
93 };
94
95 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
96 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
97 };
98
99 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
100 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
101 };
102
103 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
104 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
105 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
106 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
107 };
108
109 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
110 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
111 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
112 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
113 };
114
115 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
116 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
117 };
118
119 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
120 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
121 };
122
123 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
124 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
125 };
126
127 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
128 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
129 };
130
131 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
132 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
133 };
134
135 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
136 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
137 };
138
139 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
140 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
141 };
142
143 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
144 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
145 };
146
147 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
148 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
149 };
150
151 static const struct gl_builtin_uniform_element gl_Fog_elements[] = {
152 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
153 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
154 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
155 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
156 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
157 };
158
159 static const struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
160 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
161 };
162
163 static const struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
164 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
165 };
166
167 static const struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
168 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
169 };
170
171 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
172 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
173 };
174
175 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
176 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
177 };
178
179 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
180 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
181 };
182
183 #define MATRIX(name, statevar, modifier) \
184 static const struct gl_builtin_uniform_element name ## _elements[] = { \
185 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
186 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
187 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
188 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
189 }
190
191 MATRIX(gl_ModelViewMatrix,
192 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
193 MATRIX(gl_ModelViewMatrixInverse,
194 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
195 MATRIX(gl_ModelViewMatrixTranspose,
196 STATE_MODELVIEW_MATRIX, 0);
197 MATRIX(gl_ModelViewMatrixInverseTranspose,
198 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
199
200 MATRIX(gl_ProjectionMatrix,
201 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
202 MATRIX(gl_ProjectionMatrixInverse,
203 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
204 MATRIX(gl_ProjectionMatrixTranspose,
205 STATE_PROJECTION_MATRIX, 0);
206 MATRIX(gl_ProjectionMatrixInverseTranspose,
207 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
208
209 MATRIX(gl_ModelViewProjectionMatrix,
210 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
211 MATRIX(gl_ModelViewProjectionMatrixInverse,
212 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
213 MATRIX(gl_ModelViewProjectionMatrixTranspose,
214 STATE_MVP_MATRIX, 0);
215 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
216 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
217
218 MATRIX(gl_TextureMatrix,
219 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
220 MATRIX(gl_TextureMatrixInverse,
221 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
222 MATRIX(gl_TextureMatrixTranspose,
223 STATE_TEXTURE_MATRIX, 0);
224 MATRIX(gl_TextureMatrixInverseTranspose,
225 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
226
227 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
228 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
229 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
230 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
231 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
232 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
233 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
234 };
235
236 #undef MATRIX
237
238 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
239
240 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
241 STATEVAR(gl_NumSamples),
242 STATEVAR(gl_DepthRange),
243 STATEVAR(gl_ClipPlane),
244 STATEVAR(gl_Point),
245 STATEVAR(gl_FrontMaterial),
246 STATEVAR(gl_BackMaterial),
247 STATEVAR(gl_LightSource),
248 STATEVAR(gl_LightModel),
249 STATEVAR(gl_FrontLightModelProduct),
250 STATEVAR(gl_BackLightModelProduct),
251 STATEVAR(gl_FrontLightProduct),
252 STATEVAR(gl_BackLightProduct),
253 STATEVAR(gl_TextureEnvColor),
254 STATEVAR(gl_EyePlaneS),
255 STATEVAR(gl_EyePlaneT),
256 STATEVAR(gl_EyePlaneR),
257 STATEVAR(gl_EyePlaneQ),
258 STATEVAR(gl_ObjectPlaneS),
259 STATEVAR(gl_ObjectPlaneT),
260 STATEVAR(gl_ObjectPlaneR),
261 STATEVAR(gl_ObjectPlaneQ),
262 STATEVAR(gl_Fog),
263
264 STATEVAR(gl_ModelViewMatrix),
265 STATEVAR(gl_ModelViewMatrixInverse),
266 STATEVAR(gl_ModelViewMatrixTranspose),
267 STATEVAR(gl_ModelViewMatrixInverseTranspose),
268
269 STATEVAR(gl_ProjectionMatrix),
270 STATEVAR(gl_ProjectionMatrixInverse),
271 STATEVAR(gl_ProjectionMatrixTranspose),
272 STATEVAR(gl_ProjectionMatrixInverseTranspose),
273
274 STATEVAR(gl_ModelViewProjectionMatrix),
275 STATEVAR(gl_ModelViewProjectionMatrixInverse),
276 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
277 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
278
279 STATEVAR(gl_TextureMatrix),
280 STATEVAR(gl_TextureMatrixInverse),
281 STATEVAR(gl_TextureMatrixTranspose),
282 STATEVAR(gl_TextureMatrixInverseTranspose),
283
284 STATEVAR(gl_NormalMatrix),
285 STATEVAR(gl_NormalScale),
286
287 STATEVAR(gl_BumpRotMatrix0MESA),
288 STATEVAR(gl_BumpRotMatrix1MESA),
289 STATEVAR(gl_FogParamsOptimizedMESA),
290 STATEVAR(gl_CurrentAttribVertMESA),
291 STATEVAR(gl_CurrentAttribFragMESA),
292
293 {NULL, NULL, 0}
294 };
295
296
297 namespace {
298
299 /**
300 * Data structure that accumulates fields for the gl_PerVertex interface
301 * block.
302 */
303 class per_vertex_accumulator
304 {
305 public:
306 per_vertex_accumulator();
307 void add_field(int slot, const glsl_type *type, const char *name);
308 const glsl_type *construct_interface_instance() const;
309
310 private:
311 glsl_struct_field fields[10];
312 unsigned num_fields;
313 };
314
315
316 per_vertex_accumulator::per_vertex_accumulator()
317 : fields(),
318 num_fields(0)
319 {
320 }
321
322
323 void
324 per_vertex_accumulator::add_field(int slot, const glsl_type *type,
325 const char *name)
326 {
327 assert(this->num_fields < ARRAY_SIZE(this->fields));
328 this->fields[this->num_fields].type = type;
329 this->fields[this->num_fields].name = name;
330 this->fields[this->num_fields].row_major = false;
331 this->fields[this->num_fields].location = slot;
332 this->fields[this->num_fields].interpolation = INTERP_QUALIFIER_NONE;
333 this->fields[this->num_fields].centroid = 0;
334 this->fields[this->num_fields].sample = 0;
335 this->num_fields++;
336 }
337
338
339 const glsl_type *
340 per_vertex_accumulator::construct_interface_instance() const
341 {
342 return glsl_type::get_interface_instance(this->fields, this->num_fields,
343 GLSL_INTERFACE_PACKING_STD140,
344 "gl_PerVertex");
345 }
346
347
348 class builtin_variable_generator
349 {
350 public:
351 builtin_variable_generator(exec_list *instructions,
352 struct _mesa_glsl_parse_state *state);
353 void generate_constants();
354 void generate_uniforms();
355 void generate_vs_special_vars();
356 void generate_gs_special_vars();
357 void generate_fs_special_vars();
358 void generate_cs_special_vars();
359 void generate_varyings();
360
361 private:
362 const glsl_type *array(const glsl_type *base, unsigned elements)
363 {
364 return glsl_type::get_array_instance(base, elements);
365 }
366
367 const glsl_type *type(const char *name)
368 {
369 return symtab->get_type(name);
370 }
371
372 ir_variable *add_input(int slot, const glsl_type *type, const char *name)
373 {
374 return add_variable(name, type, ir_var_shader_in, slot);
375 }
376
377 ir_variable *add_output(int slot, const glsl_type *type, const char *name)
378 {
379 return add_variable(name, type, ir_var_shader_out, slot);
380 }
381
382 ir_variable *add_system_value(int slot, const glsl_type *type,
383 const char *name)
384 {
385 return add_variable(name, type, ir_var_system_value, slot);
386 }
387
388 ir_variable *add_variable(const char *name, const glsl_type *type,
389 enum ir_variable_mode mode, int slot);
390 ir_variable *add_uniform(const glsl_type *type, const char *name);
391 ir_variable *add_const(const char *name, int value);
392 ir_variable *add_const_ivec3(const char *name, int x, int y, int z);
393 void add_varying(int slot, const glsl_type *type, const char *name,
394 const char *name_as_gs_input);
395
396 exec_list * const instructions;
397 struct _mesa_glsl_parse_state * const state;
398 glsl_symbol_table * const symtab;
399
400 /**
401 * True if compatibility-profile-only variables should be included. (In
402 * desktop GL, these are always included when the GLSL version is 1.30 and
403 * or below).
404 */
405 const bool compatibility;
406
407 const glsl_type * const bool_t;
408 const glsl_type * const int_t;
409 const glsl_type * const float_t;
410 const glsl_type * const vec2_t;
411 const glsl_type * const vec3_t;
412 const glsl_type * const vec4_t;
413 const glsl_type * const mat3_t;
414 const glsl_type * const mat4_t;
415
416 per_vertex_accumulator per_vertex_in;
417 per_vertex_accumulator per_vertex_out;
418 };
419
420
421 builtin_variable_generator::builtin_variable_generator(
422 exec_list *instructions, struct _mesa_glsl_parse_state *state)
423 : instructions(instructions), state(state), symtab(state->symbols),
424 compatibility(!state->is_version(140, 100)),
425 bool_t(glsl_type::bool_type), int_t(glsl_type::int_type),
426 float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type),
427 vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type),
428 mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type)
429 {
430 }
431
432
433 ir_variable *
434 builtin_variable_generator::add_variable(const char *name,
435 const glsl_type *type,
436 enum ir_variable_mode mode, int slot)
437 {
438 ir_variable *var = new(symtab) ir_variable(type, name, mode);
439 var->data.how_declared = ir_var_declared_implicitly;
440
441 switch (var->data.mode) {
442 case ir_var_auto:
443 case ir_var_shader_in:
444 case ir_var_uniform:
445 case ir_var_system_value:
446 var->data.read_only = true;
447 break;
448 case ir_var_shader_out:
449 break;
450 default:
451 /* The only variables that are added using this function should be
452 * uniforms, shader inputs, and shader outputs, constants (which use
453 * ir_var_auto), and system values.
454 */
455 assert(0);
456 break;
457 }
458
459 var->data.location = slot;
460 var->data.explicit_location = (slot >= 0);
461 var->data.explicit_index = 0;
462
463 /* Once the variable is created an initialized, add it to the symbol table
464 * and add the declaration to the IR stream.
465 */
466 instructions->push_tail(var);
467
468 symtab->add_variable(var);
469 return var;
470 }
471
472
473 ir_variable *
474 builtin_variable_generator::add_uniform(const glsl_type *type,
475 const char *name)
476 {
477 ir_variable *const uni = add_variable(name, type, ir_var_uniform, -1);
478
479 unsigned i;
480 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
481 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
482 break;
483 }
484 }
485
486 assert(_mesa_builtin_uniform_desc[i].name != NULL);
487 const struct gl_builtin_uniform_desc* const statevar =
488 &_mesa_builtin_uniform_desc[i];
489
490 const unsigned array_count = type->is_array() ? type->length : 1;
491 uni->num_state_slots = array_count * statevar->num_elements;
492
493 ir_state_slot *slots =
494 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
495
496 uni->state_slots = slots;
497
498 for (unsigned a = 0; a < array_count; a++) {
499 for (unsigned j = 0; j < statevar->num_elements; j++) {
500 const struct gl_builtin_uniform_element *element =
501 &statevar->elements[j];
502
503 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
504 if (type->is_array()) {
505 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
506 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
507 slots->tokens[2] = a;
508 } else {
509 slots->tokens[1] = a;
510 }
511 }
512
513 slots->swizzle = element->swizzle;
514 slots++;
515 }
516 }
517
518 return uni;
519 }
520
521
522 ir_variable *
523 builtin_variable_generator::add_const(const char *name, int value)
524 {
525 ir_variable *const var = add_variable(name, glsl_type::int_type,
526 ir_var_auto, -1);
527 var->constant_value = new(var) ir_constant(value);
528 var->constant_initializer = new(var) ir_constant(value);
529 var->data.has_initializer = true;
530 return var;
531 }
532
533
534 ir_variable *
535 builtin_variable_generator::add_const_ivec3(const char *name, int x, int y,
536 int z)
537 {
538 ir_variable *const var = add_variable(name, glsl_type::ivec3_type,
539 ir_var_auto, -1);
540 ir_constant_data data;
541 memset(&data, 0, sizeof(data));
542 data.i[0] = x;
543 data.i[1] = y;
544 data.i[2] = z;
545 var->constant_value = new(var) ir_constant(glsl_type::ivec3_type, &data);
546 var->constant_initializer =
547 new(var) ir_constant(glsl_type::ivec3_type, &data);
548 var->data.has_initializer = true;
549 return var;
550 }
551
552
553 void
554 builtin_variable_generator::generate_constants()
555 {
556 add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs);
557 add_const("gl_MaxVertexTextureImageUnits",
558 state->Const.MaxVertexTextureImageUnits);
559 add_const("gl_MaxCombinedTextureImageUnits",
560 state->Const.MaxCombinedTextureImageUnits);
561 add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits);
562 add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers);
563
564 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
565 * GL counts them in units of "components" or "floats".
566 */
567 if (state->es_shader) {
568 add_const("gl_MaxVertexUniformVectors",
569 state->Const.MaxVertexUniformComponents / 4);
570 add_const("gl_MaxFragmentUniformVectors",
571 state->Const.MaxFragmentUniformComponents / 4);
572
573 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
574 * vertex and fragment shader constants.
575 */
576 if (state->is_version(0, 300)) {
577 add_const("gl_MaxVertexOutputVectors",
578 state->ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4);
579 add_const("gl_MaxFragmentInputVectors",
580 state->ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4);
581 } else {
582 add_const("gl_MaxVaryingVectors",
583 state->ctx->Const.MaxVarying);
584 }
585 } else {
586 add_const("gl_MaxVertexUniformComponents",
587 state->Const.MaxVertexUniformComponents);
588
589 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
590 * removed
591 */
592 add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4);
593
594 add_const("gl_MaxFragmentUniformComponents",
595 state->Const.MaxFragmentUniformComponents);
596 }
597
598 /* Texel offsets were introduced in ARB_shading_language_420pack (which
599 * requires desktop GLSL version 130), and adopted into desktop GLSL
600 * version 4.20 and GLSL ES version 3.00.
601 */
602 if ((state->is_version(130, 0) &&
603 state->ARB_shading_language_420pack_enable) ||
604 state->is_version(420, 300)) {
605 add_const("gl_MinProgramTexelOffset",
606 state->Const.MinProgramTexelOffset);
607 add_const("gl_MaxProgramTexelOffset",
608 state->Const.MaxProgramTexelOffset);
609 }
610
611 if (state->is_version(130, 0)) {
612 add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes);
613 add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4);
614 }
615
616 if (state->is_version(150, 0)) {
617 add_const("gl_MaxVertexOutputComponents",
618 state->Const.MaxVertexOutputComponents);
619 add_const("gl_MaxGeometryInputComponents",
620 state->Const.MaxGeometryInputComponents);
621 add_const("gl_MaxGeometryOutputComponents",
622 state->Const.MaxGeometryOutputComponents);
623 add_const("gl_MaxFragmentInputComponents",
624 state->Const.MaxFragmentInputComponents);
625 add_const("gl_MaxGeometryTextureImageUnits",
626 state->Const.MaxGeometryTextureImageUnits);
627 add_const("gl_MaxGeometryOutputVertices",
628 state->Const.MaxGeometryOutputVertices);
629 add_const("gl_MaxGeometryTotalOutputComponents",
630 state->Const.MaxGeometryTotalOutputComponents);
631 add_const("gl_MaxGeometryUniformComponents",
632 state->Const.MaxGeometryUniformComponents);
633
634 /* Note: the GLSL 1.50-4.40 specs require
635 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
636 * But they do not define what it means (and there does not appear to be
637 * any corresponding constant in the GL specs). However,
638 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
639 * be the maximum number of components available for use as geometry
640 * outputs. So we assume this is a synonym for
641 * gl_MaxGeometryOutputComponents.
642 */
643 add_const("gl_MaxGeometryVaryingComponents",
644 state->Const.MaxGeometryOutputComponents);
645 }
646
647 if (compatibility) {
648 /* Note: gl_MaxLights stopped being listed as an explicit constant in
649 * GLSL 1.30, however it continues to be referred to (as a minimum size
650 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
651 * this seems like it was probably an oversight.
652 */
653 add_const("gl_MaxLights", state->Const.MaxLights);
654
655 add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes);
656
657 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
658 * 1.50, however this seems like it was probably an oversight.
659 */
660 add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits);
661
662 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
663 * re-introduced in GLSL 1.50, so this seems like it was probably an
664 * oversight.
665 */
666 add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords);
667 }
668
669 if (state->ARB_shader_atomic_counters_enable) {
670 add_const("gl_MaxVertexAtomicCounters",
671 state->Const.MaxVertexAtomicCounters);
672 add_const("gl_MaxGeometryAtomicCounters",
673 state->Const.MaxGeometryAtomicCounters);
674 add_const("gl_MaxFragmentAtomicCounters",
675 state->Const.MaxFragmentAtomicCounters);
676 add_const("gl_MaxCombinedAtomicCounters",
677 state->Const.MaxCombinedAtomicCounters);
678 add_const("gl_MaxAtomicCounterBindings",
679 state->Const.MaxAtomicBufferBindings);
680 add_const("gl_MaxTessControlAtomicCounters", 0);
681 add_const("gl_MaxTessEvaluationAtomicCounters", 0);
682 }
683
684 if (state->is_version(430, 0) || state->ARB_compute_shader_enable) {
685 add_const_ivec3("gl_MaxComputeWorkGroupCount",
686 state->Const.MaxComputeWorkGroupCount[0],
687 state->Const.MaxComputeWorkGroupCount[1],
688 state->Const.MaxComputeWorkGroupCount[2]);
689 add_const_ivec3("gl_MaxComputeWorkGroupSize",
690 state->Const.MaxComputeWorkGroupSize[0],
691 state->Const.MaxComputeWorkGroupSize[1],
692 state->Const.MaxComputeWorkGroupSize[2]);
693
694 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
695 *
696 * The built-in constant gl_WorkGroupSize is a compute-shader
697 * constant containing the local work-group size of the shader. The
698 * size of the work group in the X, Y, and Z dimensions is stored in
699 * the x, y, and z components. The constants values in
700 * gl_WorkGroupSize will match those specified in the required
701 * local_size_x, local_size_y, and local_size_z layout qualifiers
702 * for the current shader. This is a constant so that it can be
703 * used to size arrays of memory that can be shared within the local
704 * work group. It is a compile-time error to use gl_WorkGroupSize
705 * in a shader that does not declare a fixed local group size, or
706 * before that shader has declared a fixed local group size, using
707 * local_size_x, local_size_y, and local_size_z.
708 *
709 * To prevent the shader from trying to refer to gl_WorkGroupSize before
710 * the layout declaration, we don't define it here. Intead we define it
711 * in ast_cs_input_layout::hir().
712 */
713 }
714
715 if (state->is_version(420, 0) ||
716 state->ARB_shader_image_load_store_enable) {
717 add_const("gl_MaxImageUnits",
718 state->Const.MaxImageUnits);
719 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
720 state->Const.MaxCombinedImageUnitsAndFragmentOutputs);
721 add_const("gl_MaxImageSamples",
722 state->Const.MaxImageSamples);
723 add_const("gl_MaxVertexImageUniforms",
724 state->Const.MaxVertexImageUniforms);
725 add_const("gl_MaxTessControlImageUniforms", 0);
726 add_const("gl_MaxTessEvaluationImageUniforms", 0);
727 add_const("gl_MaxGeometryImageUniforms",
728 state->Const.MaxGeometryImageUniforms);
729 add_const("gl_MaxFragmentImageUniforms",
730 state->Const.MaxFragmentImageUniforms);
731 add_const("gl_MaxCombinedImageUniforms",
732 state->Const.MaxCombinedImageUniforms);
733 }
734 }
735
736
737 /**
738 * Generate uniform variables (which exist in all types of shaders).
739 */
740 void
741 builtin_variable_generator::generate_uniforms()
742 {
743 add_uniform(int_t, "gl_NumSamples");
744 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
745 add_uniform(array(vec4_t, VERT_ATTRIB_MAX), "gl_CurrentAttribVertMESA");
746 add_uniform(array(vec4_t, VARYING_SLOT_MAX), "gl_CurrentAttribFragMESA");
747
748 if (compatibility) {
749 add_uniform(mat4_t, "gl_ModelViewMatrix");
750 add_uniform(mat4_t, "gl_ProjectionMatrix");
751 add_uniform(mat4_t, "gl_ModelViewProjectionMatrix");
752 add_uniform(mat3_t, "gl_NormalMatrix");
753 add_uniform(mat4_t, "gl_ModelViewMatrixInverse");
754 add_uniform(mat4_t, "gl_ProjectionMatrixInverse");
755 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse");
756 add_uniform(mat4_t, "gl_ModelViewMatrixTranspose");
757 add_uniform(mat4_t, "gl_ProjectionMatrixTranspose");
758 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose");
759 add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose");
760 add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose");
761 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose");
762 add_uniform(float_t, "gl_NormalScale");
763 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
764 add_uniform(vec2_t, "gl_BumpRotMatrix0MESA");
765 add_uniform(vec2_t, "gl_BumpRotMatrix1MESA");
766 add_uniform(vec4_t, "gl_FogParamsOptimizedMESA");
767
768 const glsl_type *const mat4_array_type =
769 array(mat4_t, state->Const.MaxTextureCoords);
770 add_uniform(mat4_array_type, "gl_TextureMatrix");
771 add_uniform(mat4_array_type, "gl_TextureMatrixInverse");
772 add_uniform(mat4_array_type, "gl_TextureMatrixTranspose");
773 add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose");
774
775 add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane");
776 add_uniform(type("gl_PointParameters"), "gl_Point");
777
778 const glsl_type *const material_parameters_type =
779 type("gl_MaterialParameters");
780 add_uniform(material_parameters_type, "gl_FrontMaterial");
781 add_uniform(material_parameters_type, "gl_BackMaterial");
782
783 add_uniform(array(type("gl_LightSourceParameters"),
784 state->Const.MaxLights),
785 "gl_LightSource");
786
787 const glsl_type *const light_model_products_type =
788 type("gl_LightModelProducts");
789 add_uniform(light_model_products_type, "gl_FrontLightModelProduct");
790 add_uniform(light_model_products_type, "gl_BackLightModelProduct");
791
792 const glsl_type *const light_products_type =
793 array(type("gl_LightProducts"), state->Const.MaxLights);
794 add_uniform(light_products_type, "gl_FrontLightProduct");
795 add_uniform(light_products_type, "gl_BackLightProduct");
796
797 add_uniform(array(vec4_t, state->Const.MaxTextureUnits),
798 "gl_TextureEnvColor");
799
800 const glsl_type *const texcoords_vec4 =
801 array(vec4_t, state->Const.MaxTextureCoords);
802 add_uniform(texcoords_vec4, "gl_EyePlaneS");
803 add_uniform(texcoords_vec4, "gl_EyePlaneT");
804 add_uniform(texcoords_vec4, "gl_EyePlaneR");
805 add_uniform(texcoords_vec4, "gl_EyePlaneQ");
806 add_uniform(texcoords_vec4, "gl_ObjectPlaneS");
807 add_uniform(texcoords_vec4, "gl_ObjectPlaneT");
808 add_uniform(texcoords_vec4, "gl_ObjectPlaneR");
809 add_uniform(texcoords_vec4, "gl_ObjectPlaneQ");
810
811 add_uniform(type("gl_FogParameters"), "gl_Fog");
812 }
813 }
814
815
816 /**
817 * Generate variables which only exist in vertex shaders.
818 */
819 void
820 builtin_variable_generator::generate_vs_special_vars()
821 {
822 if (state->is_version(130, 300))
823 add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID");
824 if (state->ARB_draw_instanced_enable)
825 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB");
826 if (state->ARB_draw_instanced_enable || state->is_version(140, 300))
827 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceID");
828 if (state->AMD_vertex_shader_layer_enable)
829 add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
830 if (compatibility) {
831 add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex");
832 add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal");
833 add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color");
834 add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor");
835 add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0");
836 add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1");
837 add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2");
838 add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3");
839 add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4");
840 add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5");
841 add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6");
842 add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7");
843 add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord");
844 }
845 }
846
847
848 /**
849 * Generate variables which only exist in geometry shaders.
850 */
851 void
852 builtin_variable_generator::generate_gs_special_vars()
853 {
854 add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
855 if (state->ARB_viewport_array_enable)
856 add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
857 if (state->ARB_gpu_shader5_enable)
858 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID");
859
860 /* Although gl_PrimitiveID appears in tessellation control and tessellation
861 * evaluation shaders, it has a different function there than it has in
862 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
863 * as special geometry shader variables.
864 *
865 * Note that although the general convention of suffixing geometry shader
866 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
867 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
868 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
869 */
870 ir_variable *var;
871 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveIDIn");
872 var->data.interpolation = INTERP_QUALIFIER_FLAT;
873 var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
874 var->data.interpolation = INTERP_QUALIFIER_FLAT;
875 }
876
877
878 /**
879 * Generate variables which only exist in fragment shaders.
880 */
881 void
882 builtin_variable_generator::generate_fs_special_vars()
883 {
884 add_input(VARYING_SLOT_POS, vec4_t, "gl_FragCoord");
885 add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing");
886 if (state->is_version(120, 100))
887 add_input(VARYING_SLOT_PNTC, vec2_t, "gl_PointCoord");
888
889 if (state->is_version(150, 0)) {
890 ir_variable *var =
891 add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
892 var->data.interpolation = INTERP_QUALIFIER_FLAT;
893 }
894
895 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
896 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
897 * They were removed from GLSL ES 3.00.
898 */
899 if (compatibility || !state->is_version(420, 300)) {
900 add_output(FRAG_RESULT_COLOR, vec4_t, "gl_FragColor");
901 add_output(FRAG_RESULT_DATA0,
902 array(vec4_t, state->Const.MaxDrawBuffers), "gl_FragData");
903 }
904
905 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
906 * ES 1.00.
907 */
908 if (state->is_version(110, 300))
909 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepth");
910
911 if (state->ARB_shader_stencil_export_enable) {
912 ir_variable *const var =
913 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB");
914 if (state->ARB_shader_stencil_export_warn)
915 var->warn_extension = "GL_ARB_shader_stencil_export";
916 }
917
918 if (state->AMD_shader_stencil_export_enable) {
919 ir_variable *const var =
920 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD");
921 if (state->AMD_shader_stencil_export_warn)
922 var->warn_extension = "GL_AMD_shader_stencil_export";
923 }
924
925 if (state->ARB_sample_shading_enable) {
926 add_system_value(SYSTEM_VALUE_SAMPLE_ID, int_t, "gl_SampleID");
927 add_system_value(SYSTEM_VALUE_SAMPLE_POS, vec2_t, "gl_SamplePosition");
928 /* From the ARB_sample_shading specification:
929 * "The number of elements in the array is ceil(<s>/32), where
930 * <s> is the maximum number of color samples supported by the
931 * implementation."
932 * Since no drivers expose more than 32x MSAA, we can simply set
933 * the array size to 1 rather than computing it.
934 */
935 add_output(FRAG_RESULT_SAMPLE_MASK, array(int_t, 1), "gl_SampleMask");
936 }
937
938 if (state->ARB_gpu_shader5_enable) {
939 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN, array(int_t, 1), "gl_SampleMaskIn");
940 }
941
942 if (state->ARB_fragment_layer_viewport_enable) {
943 add_input(VARYING_SLOT_LAYER, int_t, "gl_Layer");
944 add_input(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
945 }
946 }
947
948
949 /**
950 * Generate variables which only exist in compute shaders.
951 */
952 void
953 builtin_variable_generator::generate_cs_special_vars()
954 {
955 /* TODO: finish this. */
956 }
957
958
959 /**
960 * Add a single "varying" variable. The variable's type and direction (input
961 * or output) are adjusted as appropriate for the type of shader being
962 * compiled. For geometry shaders using {ARB,EXT}_geometry_shader4,
963 * name_as_gs_input is used for the input (to avoid ambiguity).
964 */
965 void
966 builtin_variable_generator::add_varying(int slot, const glsl_type *type,
967 const char *name,
968 const char *name_as_gs_input)
969 {
970 switch (state->stage) {
971 case MESA_SHADER_GEOMETRY:
972 this->per_vertex_in.add_field(slot, type, name);
973 /* FALLTHROUGH */
974 case MESA_SHADER_VERTEX:
975 this->per_vertex_out.add_field(slot, type, name);
976 break;
977 case MESA_SHADER_FRAGMENT:
978 add_input(slot, type, name);
979 break;
980 case MESA_SHADER_COMPUTE:
981 /* Compute shaders don't have varyings. */
982 break;
983 }
984 }
985
986
987 /**
988 * Generate variables that are used to communicate data from one shader stage
989 * to the next ("varyings").
990 */
991 void
992 builtin_variable_generator::generate_varyings()
993 {
994 #define ADD_VARYING(loc, type, name) \
995 add_varying(loc, type, name, name "In")
996
997 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
998 if (state->stage != MESA_SHADER_FRAGMENT) {
999 ADD_VARYING(VARYING_SLOT_POS, vec4_t, "gl_Position");
1000 ADD_VARYING(VARYING_SLOT_PSIZ, float_t, "gl_PointSize");
1001 }
1002
1003 if (state->is_version(130, 0)) {
1004 ADD_VARYING(VARYING_SLOT_CLIP_DIST0, array(float_t, 0),
1005 "gl_ClipDistance");
1006 }
1007
1008 if (compatibility) {
1009 ADD_VARYING(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord");
1010 ADD_VARYING(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord");
1011 if (state->stage == MESA_SHADER_FRAGMENT) {
1012 ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_Color");
1013 ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor");
1014 } else {
1015 ADD_VARYING(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex");
1016 ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor");
1017 ADD_VARYING(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor");
1018 ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor");
1019 ADD_VARYING(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor");
1020 }
1021 }
1022
1023 if (state->stage == MESA_SHADER_GEOMETRY) {
1024 const glsl_type *per_vertex_in_type =
1025 this->per_vertex_in.construct_interface_instance();
1026 add_variable("gl_in", array(per_vertex_in_type, 0),
1027 ir_var_shader_in, -1);
1028 }
1029 if (state->stage == MESA_SHADER_VERTEX || state->stage == MESA_SHADER_GEOMETRY) {
1030 const glsl_type *per_vertex_out_type =
1031 this->per_vertex_out.construct_interface_instance();
1032 const glsl_struct_field *fields = per_vertex_out_type->fields.structure;
1033 for (unsigned i = 0; i < per_vertex_out_type->length; i++) {
1034 ir_variable *var =
1035 add_variable(fields[i].name, fields[i].type, ir_var_shader_out,
1036 fields[i].location);
1037 var->data.interpolation = fields[i].interpolation;
1038 var->data.centroid = fields[i].centroid;
1039 var->data.sample = fields[i].sample;
1040 var->init_interface_type(per_vertex_out_type);
1041 }
1042 }
1043 }
1044
1045
1046 }; /* Anonymous namespace */
1047
1048
1049 void
1050 _mesa_glsl_initialize_variables(exec_list *instructions,
1051 struct _mesa_glsl_parse_state *state)
1052 {
1053 builtin_variable_generator gen(instructions, state);
1054
1055 gen.generate_constants();
1056 gen.generate_uniforms();
1057
1058 gen.generate_varyings();
1059
1060 switch (state->stage) {
1061 case MESA_SHADER_VERTEX:
1062 gen.generate_vs_special_vars();
1063 break;
1064 case MESA_SHADER_GEOMETRY:
1065 gen.generate_gs_special_vars();
1066 break;
1067 case MESA_SHADER_FRAGMENT:
1068 gen.generate_fs_special_vars();
1069 break;
1070 case MESA_SHADER_COMPUTE:
1071 gen.generate_cs_special_vars();
1072 break;
1073 }
1074 }