glsl: Add an ir_variable::reinit_interface_type() function.
[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 : num_fields(0)
315 {
316 }
317
318
319 void
320 per_vertex_accumulator::add_field(int slot, const glsl_type *type,
321 const char *name)
322 {
323 assert(this->num_fields < ARRAY_SIZE(this->fields));
324 this->fields[this->num_fields].type = type;
325 this->fields[this->num_fields].name = name;
326 this->fields[this->num_fields].row_major = false;
327 this->fields[this->num_fields].location = slot;
328 this->num_fields++;
329 }
330
331
332 const glsl_type *
333 per_vertex_accumulator::construct_interface_instance() const
334 {
335 return glsl_type::get_interface_instance(this->fields, this->num_fields,
336 GLSL_INTERFACE_PACKING_STD140,
337 "gl_PerVertex");
338 }
339
340
341 class builtin_variable_generator
342 {
343 public:
344 builtin_variable_generator(exec_list *instructions,
345 struct _mesa_glsl_parse_state *state);
346 void generate_constants();
347 void generate_uniforms();
348 void generate_vs_special_vars();
349 void generate_gs_special_vars();
350 void generate_fs_special_vars();
351 void generate_varyings();
352
353 private:
354 const glsl_type *array(const glsl_type *base, unsigned elements)
355 {
356 return glsl_type::get_array_instance(base, elements);
357 }
358
359 const glsl_type *type(const char *name)
360 {
361 return symtab->get_type(name);
362 }
363
364 ir_variable *add_input(int slot, const glsl_type *type, const char *name)
365 {
366 return add_variable(name, type, ir_var_shader_in, slot);
367 }
368
369 ir_variable *add_output(int slot, const glsl_type *type, const char *name)
370 {
371 return add_variable(name, type, ir_var_shader_out, slot);
372 }
373
374 ir_variable *add_system_value(int slot, const glsl_type *type,
375 const char *name)
376 {
377 return add_variable(name, type, ir_var_system_value, slot);
378 }
379
380 ir_variable *add_variable(const char *name, const glsl_type *type,
381 enum ir_variable_mode mode, int slot);
382 ir_variable *add_uniform(const glsl_type *type, const char *name);
383 ir_variable *add_const(const char *name, int value);
384 void add_varying(int slot, const glsl_type *type, const char *name,
385 const char *name_as_gs_input);
386
387 exec_list * const instructions;
388 struct _mesa_glsl_parse_state * const state;
389 glsl_symbol_table * const symtab;
390
391 /**
392 * True if compatibility-profile-only variables should be included. (In
393 * desktop GL, these are always included when the GLSL version is 1.30 and
394 * or below).
395 */
396 const bool compatibility;
397
398 const glsl_type * const bool_t;
399 const glsl_type * const int_t;
400 const glsl_type * const float_t;
401 const glsl_type * const vec2_t;
402 const glsl_type * const vec3_t;
403 const glsl_type * const vec4_t;
404 const glsl_type * const mat3_t;
405 const glsl_type * const mat4_t;
406
407 per_vertex_accumulator per_vertex_in;
408 per_vertex_accumulator per_vertex_out;
409 };
410
411
412 builtin_variable_generator::builtin_variable_generator(
413 exec_list *instructions, struct _mesa_glsl_parse_state *state)
414 : instructions(instructions), state(state), symtab(state->symbols),
415 compatibility(!state->is_version(140, 100)),
416 bool_t(glsl_type::bool_type), int_t(glsl_type::int_type),
417 float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type),
418 vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type),
419 mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type)
420 {
421 }
422
423
424 ir_variable *
425 builtin_variable_generator::add_variable(const char *name,
426 const glsl_type *type,
427 enum ir_variable_mode mode, int slot)
428 {
429 ir_variable *var = new(symtab) ir_variable(type, name, mode);
430
431 switch (var->mode) {
432 case ir_var_auto:
433 case ir_var_shader_in:
434 case ir_var_uniform:
435 case ir_var_system_value:
436 var->read_only = true;
437 break;
438 case ir_var_shader_out:
439 break;
440 default:
441 /* The only variables that are added using this function should be
442 * uniforms, shader inputs, and shader outputs, constants (which use
443 * ir_var_auto), and system values.
444 */
445 assert(0);
446 break;
447 }
448
449 var->location = slot;
450 var->explicit_location = (slot >= 0);
451 var->explicit_index = 0;
452
453 /* Once the variable is created an initialized, add it to the symbol table
454 * and add the declaration to the IR stream.
455 */
456 instructions->push_tail(var);
457
458 symtab->add_variable(var);
459 return var;
460 }
461
462
463 ir_variable *
464 builtin_variable_generator::add_uniform(const glsl_type *type,
465 const char *name)
466 {
467 ir_variable *const uni = add_variable(name, type, ir_var_uniform, -1);
468
469 unsigned i;
470 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
471 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
472 break;
473 }
474 }
475
476 assert(_mesa_builtin_uniform_desc[i].name != NULL);
477 const struct gl_builtin_uniform_desc* const statevar =
478 &_mesa_builtin_uniform_desc[i];
479
480 const unsigned array_count = type->is_array() ? type->length : 1;
481 uni->num_state_slots = array_count * statevar->num_elements;
482
483 ir_state_slot *slots =
484 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
485
486 uni->state_slots = slots;
487
488 for (unsigned a = 0; a < array_count; a++) {
489 for (unsigned j = 0; j < statevar->num_elements; j++) {
490 struct gl_builtin_uniform_element *element = &statevar->elements[j];
491
492 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
493 if (type->is_array()) {
494 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
495 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
496 slots->tokens[2] = a;
497 } else {
498 slots->tokens[1] = a;
499 }
500 }
501
502 slots->swizzle = element->swizzle;
503 slots++;
504 }
505 }
506
507 return uni;
508 }
509
510
511 ir_variable *
512 builtin_variable_generator::add_const(const char *name, int value)
513 {
514 ir_variable *const var = add_variable(name, glsl_type::int_type,
515 ir_var_auto, -1);
516 var->constant_value = new(var) ir_constant(value);
517 var->constant_initializer = new(var) ir_constant(value);
518 var->has_initializer = true;
519 return var;
520 }
521
522
523 void
524 builtin_variable_generator::generate_constants()
525 {
526 add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs);
527 add_const("gl_MaxVertexTextureImageUnits",
528 state->Const.MaxVertexTextureImageUnits);
529 add_const("gl_MaxCombinedTextureImageUnits",
530 state->Const.MaxCombinedTextureImageUnits);
531 add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits);
532 add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers);
533
534 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
535 * GL counts them in units of "components" or "floats".
536 */
537 if (state->es_shader) {
538 add_const("gl_MaxVertexUniformVectors",
539 state->Const.MaxVertexUniformComponents / 4);
540 add_const("gl_MaxFragmentUniformVectors",
541 state->Const.MaxFragmentUniformComponents / 4);
542
543 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
544 * vertex and fragment shader constants.
545 */
546 if (state->is_version(0, 300)) {
547 add_const("gl_MaxVertexOutputVectors",
548 state->ctx->Const.VertexProgram.MaxOutputComponents / 4);
549 add_const("gl_MaxFragmentInputVectors",
550 state->ctx->Const.FragmentProgram.MaxInputComponents / 4);
551 } else {
552 add_const("gl_MaxVaryingVectors",
553 state->ctx->Const.MaxVarying);
554 }
555 } else {
556 add_const("gl_MaxVertexUniformComponents",
557 state->Const.MaxVertexUniformComponents);
558
559 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
560 * removed
561 */
562 add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4);
563
564 add_const("gl_MaxFragmentUniformComponents",
565 state->Const.MaxFragmentUniformComponents);
566 }
567
568 /* Texel offsets were introduced in ARB_shading_language_420pack (which
569 * requires desktop GLSL version 130), and adopted into desktop GLSL
570 * version 4.20 and GLSL ES version 3.00.
571 */
572 if ((state->is_version(130, 0) &&
573 state->ARB_shading_language_420pack_enable) ||
574 state->is_version(420, 300)) {
575 add_const("gl_MinProgramTexelOffset",
576 state->Const.MinProgramTexelOffset);
577 add_const("gl_MaxProgramTexelOffset",
578 state->Const.MaxProgramTexelOffset);
579 }
580
581 if (state->is_version(130, 0)) {
582 add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes);
583 add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4);
584 }
585
586 if (compatibility) {
587 /* Note: gl_MaxLights stopped being listed as an explicit constant in
588 * GLSL 1.30, however it continues to be referred to (as a minimum size
589 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
590 * this seems like it was probably an oversight.
591 */
592 add_const("gl_MaxLights", state->Const.MaxLights);
593
594 add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes);
595
596 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
597 * 1.50, however this seems like it was probably an oversight.
598 */
599 add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits);
600
601 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
602 * re-introduced in GLSL 1.50, so this seems like it was probably an
603 * oversight.
604 */
605 add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords);
606 }
607 }
608
609
610 /**
611 * Generate uniform variables (which exist in all types of shaders).
612 */
613 void
614 builtin_variable_generator::generate_uniforms()
615 {
616 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
617 add_uniform(array(vec4_t, VERT_ATTRIB_MAX), "gl_CurrentAttribVertMESA");
618 add_uniform(array(vec4_t, VARYING_SLOT_MAX), "gl_CurrentAttribFragMESA");
619
620 if (compatibility) {
621 add_uniform(mat4_t, "gl_ModelViewMatrix");
622 add_uniform(mat4_t, "gl_ProjectionMatrix");
623 add_uniform(mat4_t, "gl_ModelViewProjectionMatrix");
624 add_uniform(mat3_t, "gl_NormalMatrix");
625 add_uniform(mat4_t, "gl_ModelViewMatrixInverse");
626 add_uniform(mat4_t, "gl_ProjectionMatrixInverse");
627 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse");
628 add_uniform(mat4_t, "gl_ModelViewMatrixTranspose");
629 add_uniform(mat4_t, "gl_ProjectionMatrixTranspose");
630 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose");
631 add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose");
632 add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose");
633 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose");
634 add_uniform(float_t, "gl_NormalScale");
635 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
636 add_uniform(vec2_t, "gl_BumpRotMatrix0MESA");
637 add_uniform(vec2_t, "gl_BumpRotMatrix1MESA");
638 add_uniform(vec4_t, "gl_FogParamsOptimizedMESA");
639
640 const glsl_type *const mat4_array_type =
641 array(mat4_t, state->Const.MaxTextureCoords);
642 add_uniform(mat4_array_type, "gl_TextureMatrix");
643 add_uniform(mat4_array_type, "gl_TextureMatrixInverse");
644 add_uniform(mat4_array_type, "gl_TextureMatrixTranspose");
645 add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose");
646
647 add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane");
648 add_uniform(type("gl_PointParameters"), "gl_Point");
649
650 const glsl_type *const material_parameters_type =
651 type("gl_MaterialParameters");
652 add_uniform(material_parameters_type, "gl_FrontMaterial");
653 add_uniform(material_parameters_type, "gl_BackMaterial");
654
655 add_uniform(array(type("gl_LightSourceParameters"),
656 state->Const.MaxLights),
657 "gl_LightSource");
658
659 const glsl_type *const light_model_products_type =
660 type("gl_LightModelProducts");
661 add_uniform(light_model_products_type, "gl_FrontLightModelProduct");
662 add_uniform(light_model_products_type, "gl_BackLightModelProduct");
663
664 const glsl_type *const light_products_type =
665 array(type("gl_LightProducts"), state->Const.MaxLights);
666 add_uniform(light_products_type, "gl_FrontLightProduct");
667 add_uniform(light_products_type, "gl_BackLightProduct");
668
669 add_uniform(array(vec4_t, state->Const.MaxTextureUnits),
670 "gl_TextureEnvColor");
671
672 const glsl_type *const texcoords_vec4 =
673 array(vec4_t, state->Const.MaxTextureCoords);
674 add_uniform(texcoords_vec4, "gl_EyePlaneS");
675 add_uniform(texcoords_vec4, "gl_EyePlaneT");
676 add_uniform(texcoords_vec4, "gl_EyePlaneR");
677 add_uniform(texcoords_vec4, "gl_EyePlaneQ");
678 add_uniform(texcoords_vec4, "gl_ObjectPlaneS");
679 add_uniform(texcoords_vec4, "gl_ObjectPlaneT");
680 add_uniform(texcoords_vec4, "gl_ObjectPlaneR");
681 add_uniform(texcoords_vec4, "gl_ObjectPlaneQ");
682
683 add_uniform(type("gl_FogParameters"), "gl_Fog");
684 }
685 }
686
687
688 /**
689 * Generate variables which only exist in vertex shaders.
690 */
691 void
692 builtin_variable_generator::generate_vs_special_vars()
693 {
694 if (state->is_version(130, 300))
695 add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID");
696 if (state->ARB_draw_instanced_enable)
697 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB");
698 if (state->ARB_draw_instanced_enable || state->is_version(140, 300))
699 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceID");
700 if (state->AMD_vertex_shader_layer_enable)
701 add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
702 if (compatibility) {
703 add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex");
704 add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal");
705 add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color");
706 add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor");
707 add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0");
708 add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1");
709 add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2");
710 add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3");
711 add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4");
712 add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5");
713 add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6");
714 add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7");
715 add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord");
716 }
717 }
718
719
720 /**
721 * Generate variables which only exist in geometry shaders.
722 */
723 void
724 builtin_variable_generator::generate_gs_special_vars()
725 {
726 add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
727
728 /* Although gl_PrimitiveID appears in tessellation control and tessellation
729 * evaluation shaders, it has a different function there than it has in
730 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
731 * as special geometry shader variables.
732 *
733 * Note that although the general convention of suffixing geometry shader
734 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
735 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
736 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
737 */
738 ir_variable *var;
739 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveIDIn");
740 var->interpolation = INTERP_QUALIFIER_FLAT;
741 var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
742 var->interpolation = INTERP_QUALIFIER_FLAT;
743 }
744
745
746 /**
747 * Generate variables which only exist in fragment shaders.
748 */
749 void
750 builtin_variable_generator::generate_fs_special_vars()
751 {
752 add_input(VARYING_SLOT_POS, vec4_t, "gl_FragCoord");
753 add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing");
754 if (state->is_version(120, 100))
755 add_input(VARYING_SLOT_PNTC, vec2_t, "gl_PointCoord");
756
757 if (state->is_version(150, 0)) {
758 ir_variable *var =
759 add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
760 var->interpolation = INTERP_QUALIFIER_FLAT;
761 }
762
763 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
764 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
765 * They were removed from GLSL ES 3.00.
766 */
767 if (compatibility || !state->is_version(420, 300)) {
768 add_output(FRAG_RESULT_COLOR, vec4_t, "gl_FragColor");
769 add_output(FRAG_RESULT_DATA0,
770 array(vec4_t, state->Const.MaxDrawBuffers), "gl_FragData");
771 }
772
773 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
774 * ES 1.00.
775 */
776 if (state->is_version(110, 300))
777 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepth");
778
779 if (state->ARB_shader_stencil_export_enable) {
780 ir_variable *const var =
781 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB");
782 if (state->ARB_shader_stencil_export_warn)
783 var->warn_extension = "GL_ARB_shader_stencil_export";
784 }
785
786 if (state->AMD_shader_stencil_export_enable) {
787 ir_variable *const var =
788 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD");
789 if (state->AMD_shader_stencil_export_warn)
790 var->warn_extension = "GL_AMD_shader_stencil_export";
791 }
792 }
793
794
795 /**
796 * Add a single "varying" variable. The variable's type and direction (input
797 * or output) are adjusted as appropriate for the type of shader being
798 * compiled. For geometry shaders using {ARB,EXT}_geometry_shader4,
799 * name_as_gs_input is used for the input (to avoid ambiguity).
800 */
801 void
802 builtin_variable_generator::add_varying(int slot, const glsl_type *type,
803 const char *name,
804 const char *name_as_gs_input)
805 {
806 switch (state->target) {
807 case geometry_shader:
808 this->per_vertex_in.add_field(slot, type, name);
809 /* FALLTHROUGH */
810 case vertex_shader:
811 this->per_vertex_out.add_field(slot, type, name);
812 break;
813 case fragment_shader:
814 add_input(slot, type, name);
815 break;
816 }
817 }
818
819
820 /**
821 * Generate variables that are used to communicate data from one shader stage
822 * to the next ("varyings").
823 */
824 void
825 builtin_variable_generator::generate_varyings()
826 {
827 #define ADD_VARYING(loc, type, name) \
828 add_varying(loc, type, name, name "In")
829
830 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
831 if (state->target != fragment_shader) {
832 ADD_VARYING(VARYING_SLOT_POS, vec4_t, "gl_Position");
833 ADD_VARYING(VARYING_SLOT_PSIZ, float_t, "gl_PointSize");
834 }
835
836 if (state->is_version(130, 0)) {
837 ADD_VARYING(VARYING_SLOT_CLIP_DIST0, array(float_t, 0),
838 "gl_ClipDistance");
839 }
840
841 if (compatibility) {
842 ADD_VARYING(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord");
843 ADD_VARYING(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord");
844 if (state->target == fragment_shader) {
845 ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_Color");
846 ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor");
847 } else {
848 ADD_VARYING(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex");
849 ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor");
850 ADD_VARYING(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor");
851 ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor");
852 ADD_VARYING(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor");
853 }
854 }
855
856 if (state->target == geometry_shader) {
857 const glsl_type *per_vertex_in_type =
858 this->per_vertex_in.construct_interface_instance();
859 ir_variable *var = add_variable("gl_in", array(per_vertex_in_type, 0),
860 ir_var_shader_in, -1);
861 var->init_interface_type(per_vertex_in_type);
862 }
863 if (state->target == vertex_shader || state->target == geometry_shader) {
864 const glsl_type *per_vertex_out_type =
865 this->per_vertex_out.construct_interface_instance();
866 const glsl_struct_field *fields = per_vertex_out_type->fields.structure;
867 for (unsigned i = 0; i < per_vertex_out_type->length; i++) {
868 ir_variable *var =
869 add_variable(fields[i].name, fields[i].type, ir_var_shader_out,
870 fields[i].location);
871 var->init_interface_type(per_vertex_out_type);
872 }
873 }
874 }
875
876
877 }; /* Anonymous namespace */
878
879
880 void
881 _mesa_glsl_initialize_variables(exec_list *instructions,
882 struct _mesa_glsl_parse_state *state)
883 {
884 builtin_variable_generator gen(instructions, state);
885
886 gen.generate_constants();
887 gen.generate_uniforms();
888
889 gen.generate_varyings();
890
891 switch (state->target) {
892 case vertex_shader:
893 gen.generate_vs_special_vars();
894 break;
895 case geometry_shader:
896 gen.generate_gs_special_vars();
897 break;
898 case fragment_shader:
899 gen.generate_fs_special_vars();
900 break;
901 }
902 }