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