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