glsl: add gl_LocalGroupSizeARB as a system value
[mesa.git] / src / compiler / 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 "ir_builder.h"
26 #include "linker.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_symbol_table.h"
29 #include "main/core.h"
30 #include "main/uniforms.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_instruction.h"
33
34 using namespace ir_builder;
35
36 static const struct gl_builtin_uniform_element gl_NumSamples_elements[] = {
37 {NULL, {STATE_NUM_SAMPLES, 0, 0}, SWIZZLE_XXXX}
38 };
39
40 /* only for TCS */
41 static const struct gl_builtin_uniform_element gl_PatchVerticesIn_elements[] = {
42 {NULL, {STATE_INTERNAL, STATE_TCS_PATCH_VERTICES_IN}, SWIZZLE_XXXX}
43 };
44
45 static const struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
46 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
47 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
48 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
49 };
50
51 static const struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
52 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
53 };
54
55 static const struct gl_builtin_uniform_element gl_Point_elements[] = {
56 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
57 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
58 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
59 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
60 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
61 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
62 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
63 };
64
65 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
66 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
67 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
68 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
69 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
70 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
71 };
72
73 static const struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
74 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
75 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
76 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
77 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
78 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
79 };
80
81 static const struct gl_builtin_uniform_element gl_LightSource_elements[] = {
82 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
83 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
84 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
85 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
86 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
87 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
88 MAKE_SWIZZLE4(SWIZZLE_X,
89 SWIZZLE_Y,
90 SWIZZLE_Z,
91 SWIZZLE_Z)},
92 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
93 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
94 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
95 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
96 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
97 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
98 };
99
100 static const struct gl_builtin_uniform_element gl_LightModel_elements[] = {
101 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
102 };
103
104 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
105 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
106 };
107
108 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
109 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
110 };
111
112 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
113 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
114 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
115 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
116 };
117
118 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
119 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
120 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
121 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
122 };
123
124 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
125 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
126 };
127
128 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
129 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
130 };
131
132 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
133 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
134 };
135
136 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
137 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
138 };
139
140 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
141 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
142 };
143
144 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
145 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
146 };
147
148 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
149 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
150 };
151
152 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
153 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
154 };
155
156 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
157 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
158 };
159
160 static const struct gl_builtin_uniform_element gl_Fog_elements[] = {
161 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
162 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
163 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
164 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
165 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
166 };
167
168 static const struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
169 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
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, ARRAY_SIZE(name ## _elements)}
240
241 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
242 STATEVAR(gl_PatchVerticesIn),
243 STATEVAR(gl_NumSamples),
244 STATEVAR(gl_DepthRange),
245 STATEVAR(gl_ClipPlane),
246 STATEVAR(gl_Point),
247 STATEVAR(gl_FrontMaterial),
248 STATEVAR(gl_BackMaterial),
249 STATEVAR(gl_LightSource),
250 STATEVAR(gl_LightModel),
251 STATEVAR(gl_FrontLightModelProduct),
252 STATEVAR(gl_BackLightModelProduct),
253 STATEVAR(gl_FrontLightProduct),
254 STATEVAR(gl_BackLightProduct),
255 STATEVAR(gl_TextureEnvColor),
256 STATEVAR(gl_EyePlaneS),
257 STATEVAR(gl_EyePlaneT),
258 STATEVAR(gl_EyePlaneR),
259 STATEVAR(gl_EyePlaneQ),
260 STATEVAR(gl_ObjectPlaneS),
261 STATEVAR(gl_ObjectPlaneT),
262 STATEVAR(gl_ObjectPlaneR),
263 STATEVAR(gl_ObjectPlaneQ),
264 STATEVAR(gl_Fog),
265
266 STATEVAR(gl_ModelViewMatrix),
267 STATEVAR(gl_ModelViewMatrixInverse),
268 STATEVAR(gl_ModelViewMatrixTranspose),
269 STATEVAR(gl_ModelViewMatrixInverseTranspose),
270
271 STATEVAR(gl_ProjectionMatrix),
272 STATEVAR(gl_ProjectionMatrixInverse),
273 STATEVAR(gl_ProjectionMatrixTranspose),
274 STATEVAR(gl_ProjectionMatrixInverseTranspose),
275
276 STATEVAR(gl_ModelViewProjectionMatrix),
277 STATEVAR(gl_ModelViewProjectionMatrixInverse),
278 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
279 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
280
281 STATEVAR(gl_TextureMatrix),
282 STATEVAR(gl_TextureMatrixInverse),
283 STATEVAR(gl_TextureMatrixTranspose),
284 STATEVAR(gl_TextureMatrixInverseTranspose),
285
286 STATEVAR(gl_NormalMatrix),
287 STATEVAR(gl_NormalScale),
288
289 STATEVAR(gl_FogParamsOptimizedMESA),
290 STATEVAR(gl_CurrentAttribVertMESA),
291 STATEVAR(gl_CurrentAttribFragMESA),
292
293 {NULL, NULL, 0}
294 };
295
296
297 namespace {
298
299 /**
300 * Data structure that accumulates fields for the gl_PerVertex interface
301 * block.
302 */
303 class per_vertex_accumulator
304 {
305 public:
306 per_vertex_accumulator();
307 void add_field(int slot, const glsl_type *type, const char *name);
308 const glsl_type *construct_interface_instance() const;
309
310 private:
311 glsl_struct_field fields[11];
312 unsigned num_fields;
313 };
314
315
316 per_vertex_accumulator::per_vertex_accumulator()
317 : fields(),
318 num_fields(0)
319 {
320 }
321
322
323 void
324 per_vertex_accumulator::add_field(int slot, const glsl_type *type,
325 const char *name)
326 {
327 assert(this->num_fields < ARRAY_SIZE(this->fields));
328 this->fields[this->num_fields].type = type;
329 this->fields[this->num_fields].name = name;
330 this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
331 this->fields[this->num_fields].location = slot;
332 this->fields[this->num_fields].offset = -1;
333 this->fields[this->num_fields].interpolation = INTERP_MODE_NONE;
334 this->fields[this->num_fields].centroid = 0;
335 this->fields[this->num_fields].sample = 0;
336 this->fields[this->num_fields].patch = 0;
337 this->fields[this->num_fields].precision = GLSL_PRECISION_NONE;
338 this->fields[this->num_fields].image_read_only = 0;
339 this->fields[this->num_fields].image_write_only = 0;
340 this->fields[this->num_fields].image_coherent = 0;
341 this->fields[this->num_fields].image_volatile = 0;
342 this->fields[this->num_fields].image_restrict = 0;
343 this->fields[this->num_fields].explicit_xfb_buffer = 0;
344 this->fields[this->num_fields].xfb_buffer = -1;
345 this->fields[this->num_fields].xfb_stride = -1;
346 this->num_fields++;
347 }
348
349
350 const glsl_type *
351 per_vertex_accumulator::construct_interface_instance() const
352 {
353 return glsl_type::get_interface_instance(this->fields, this->num_fields,
354 GLSL_INTERFACE_PACKING_STD140,
355 "gl_PerVertex");
356 }
357
358
359 class builtin_variable_generator
360 {
361 public:
362 builtin_variable_generator(exec_list *instructions,
363 struct _mesa_glsl_parse_state *state);
364 void generate_constants();
365 void generate_uniforms();
366 void generate_vs_special_vars();
367 void generate_tcs_special_vars();
368 void generate_tes_special_vars();
369 void generate_gs_special_vars();
370 void generate_fs_special_vars();
371 void generate_cs_special_vars();
372 void generate_varyings();
373
374 private:
375 const glsl_type *array(const glsl_type *base, unsigned elements)
376 {
377 return glsl_type::get_array_instance(base, elements);
378 }
379
380 const glsl_type *type(const char *name)
381 {
382 return symtab->get_type(name);
383 }
384
385 ir_variable *add_input(int slot, const glsl_type *type, const char *name)
386 {
387 return add_variable(name, type, ir_var_shader_in, slot);
388 }
389
390 ir_variable *add_output(int slot, const glsl_type *type, const char *name)
391 {
392 return add_variable(name, type, ir_var_shader_out, slot);
393 }
394
395 ir_variable *add_index_output(int slot, int index, const glsl_type *type, const char *name)
396 {
397 return add_index_variable(name, type, ir_var_shader_out, slot, index);
398 }
399
400 ir_variable *add_system_value(int slot, const glsl_type *type,
401 const char *name)
402 {
403 return add_variable(name, type, ir_var_system_value, slot);
404 }
405
406 ir_variable *add_variable(const char *name, const glsl_type *type,
407 enum ir_variable_mode mode, int slot);
408 ir_variable *add_index_variable(const char *name, const glsl_type *type,
409 enum ir_variable_mode mode, int slot, int index);
410 ir_variable *add_uniform(const glsl_type *type, const char *name);
411 ir_variable *add_const(const char *name, int value);
412 ir_variable *add_const_ivec3(const char *name, int x, int y, int z);
413 void add_varying(int slot, const glsl_type *type, const char *name);
414
415 exec_list * const instructions;
416 struct _mesa_glsl_parse_state * const state;
417 glsl_symbol_table * const symtab;
418
419 /**
420 * True if compatibility-profile-only variables should be included. (In
421 * desktop GL, these are always included when the GLSL version is 1.30 and
422 * or below).
423 */
424 const bool compatibility;
425
426 const glsl_type * const bool_t;
427 const glsl_type * const int_t;
428 const glsl_type * const uint_t;
429 const glsl_type * const float_t;
430 const glsl_type * const vec2_t;
431 const glsl_type * const vec3_t;
432 const glsl_type * const vec4_t;
433 const glsl_type * const uvec3_t;
434 const glsl_type * const mat3_t;
435 const glsl_type * const mat4_t;
436
437 per_vertex_accumulator per_vertex_in;
438 per_vertex_accumulator per_vertex_out;
439 };
440
441
442 builtin_variable_generator::builtin_variable_generator(
443 exec_list *instructions, struct _mesa_glsl_parse_state *state)
444 : instructions(instructions), state(state), symtab(state->symbols),
445 compatibility(!state->is_version(140, 100)),
446 bool_t(glsl_type::bool_type), int_t(glsl_type::int_type),
447 uint_t(glsl_type::uint_type),
448 float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type),
449 vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type),
450 uvec3_t(glsl_type::uvec3_type),
451 mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type)
452 {
453 }
454
455 ir_variable *
456 builtin_variable_generator::add_index_variable(const char *name,
457 const glsl_type *type,
458 enum ir_variable_mode mode, int slot, int index)
459 {
460 ir_variable *var = new(symtab) ir_variable(type, name, mode);
461 var->data.how_declared = ir_var_declared_implicitly;
462
463 switch (var->data.mode) {
464 case ir_var_auto:
465 case ir_var_shader_in:
466 case ir_var_uniform:
467 case ir_var_system_value:
468 var->data.read_only = true;
469 break;
470 case ir_var_shader_out:
471 case ir_var_shader_storage:
472 break;
473 default:
474 /* The only variables that are added using this function should be
475 * uniforms, shader storage, shader inputs, and shader outputs, constants
476 * (which use ir_var_auto), and system values.
477 */
478 assert(0);
479 break;
480 }
481
482 var->data.location = slot;
483 var->data.explicit_location = (slot >= 0);
484 var->data.explicit_index = 1;
485 var->data.index = index;
486
487 /* Once the variable is created an initialized, add it to the symbol table
488 * and add the declaration to the IR stream.
489 */
490 instructions->push_tail(var);
491
492 symtab->add_variable(var);
493 return var;
494 }
495
496 ir_variable *
497 builtin_variable_generator::add_variable(const char *name,
498 const glsl_type *type,
499 enum ir_variable_mode mode, int slot)
500 {
501 ir_variable *var = new(symtab) ir_variable(type, name, mode);
502 var->data.how_declared = ir_var_declared_implicitly;
503
504 switch (var->data.mode) {
505 case ir_var_auto:
506 case ir_var_shader_in:
507 case ir_var_uniform:
508 case ir_var_system_value:
509 var->data.read_only = true;
510 break;
511 case ir_var_shader_out:
512 case ir_var_shader_storage:
513 break;
514 default:
515 /* The only variables that are added using this function should be
516 * uniforms, shader storage, shader inputs, and shader outputs, constants
517 * (which use ir_var_auto), and system values.
518 */
519 assert(0);
520 break;
521 }
522
523 var->data.location = slot;
524 var->data.explicit_location = (slot >= 0);
525 var->data.explicit_index = 0;
526
527 /* Once the variable is created an initialized, add it to the symbol table
528 * and add the declaration to the IR stream.
529 */
530 instructions->push_tail(var);
531
532 symtab->add_variable(var);
533 return var;
534 }
535
536 extern "C" const struct gl_builtin_uniform_desc *
537 _mesa_glsl_get_builtin_uniform_desc(const char *name)
538 {
539 for (unsigned i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
540 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
541 return &_mesa_builtin_uniform_desc[i];
542 }
543 }
544 return NULL;
545 }
546
547 ir_variable *
548 builtin_variable_generator::add_uniform(const glsl_type *type,
549 const char *name)
550 {
551 ir_variable *const uni = add_variable(name, type, ir_var_uniform, -1);
552
553 const struct gl_builtin_uniform_desc* const statevar =
554 _mesa_glsl_get_builtin_uniform_desc(name);
555 assert(statevar != NULL);
556
557 const unsigned array_count = type->is_array() ? type->length : 1;
558
559 ir_state_slot *slots =
560 uni->allocate_state_slots(array_count * statevar->num_elements);
561
562 for (unsigned a = 0; a < array_count; a++) {
563 for (unsigned j = 0; j < statevar->num_elements; j++) {
564 const struct gl_builtin_uniform_element *element =
565 &statevar->elements[j];
566
567 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
568 if (type->is_array()) {
569 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
570 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
571 slots->tokens[2] = a;
572 } else {
573 slots->tokens[1] = a;
574 }
575 }
576
577 slots->swizzle = element->swizzle;
578 slots++;
579 }
580 }
581
582 return uni;
583 }
584
585
586 ir_variable *
587 builtin_variable_generator::add_const(const char *name, int value)
588 {
589 ir_variable *const var = add_variable(name, glsl_type::int_type,
590 ir_var_auto, -1);
591 var->constant_value = new(var) ir_constant(value);
592 var->constant_initializer = new(var) ir_constant(value);
593 var->data.has_initializer = true;
594 return var;
595 }
596
597
598 ir_variable *
599 builtin_variable_generator::add_const_ivec3(const char *name, int x, int y,
600 int z)
601 {
602 ir_variable *const var = add_variable(name, glsl_type::ivec3_type,
603 ir_var_auto, -1);
604 ir_constant_data data;
605 memset(&data, 0, sizeof(data));
606 data.i[0] = x;
607 data.i[1] = y;
608 data.i[2] = z;
609 var->constant_value = new(var) ir_constant(glsl_type::ivec3_type, &data);
610 var->constant_initializer =
611 new(var) ir_constant(glsl_type::ivec3_type, &data);
612 var->data.has_initializer = true;
613 return var;
614 }
615
616
617 void
618 builtin_variable_generator::generate_constants()
619 {
620 add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs);
621 add_const("gl_MaxVertexTextureImageUnits",
622 state->Const.MaxVertexTextureImageUnits);
623 add_const("gl_MaxCombinedTextureImageUnits",
624 state->Const.MaxCombinedTextureImageUnits);
625 add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits);
626 add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers);
627
628 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
629 * GL counts them in units of "components" or "floats".
630 */
631 if (state->is_version(410, 100)) {
632 add_const("gl_MaxVertexUniformVectors",
633 state->Const.MaxVertexUniformComponents / 4);
634 add_const("gl_MaxFragmentUniformVectors",
635 state->Const.MaxFragmentUniformComponents / 4);
636
637 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
638 * vertex and fragment shader constants.
639 */
640 if (state->is_version(0, 300)) {
641 add_const("gl_MaxVertexOutputVectors",
642 state->ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4);
643 add_const("gl_MaxFragmentInputVectors",
644 state->ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4);
645 } else {
646 add_const("gl_MaxVaryingVectors",
647 state->ctx->Const.MaxVarying);
648 }
649
650 /* EXT_blend_func_extended brings a built in constant
651 * for determining number of dual source draw buffers
652 */
653 if (state->EXT_blend_func_extended_enable) {
654 add_const("gl_MaxDualSourceDrawBuffersEXT",
655 state->Const.MaxDualSourceDrawBuffers);
656 }
657 } else {
658 add_const("gl_MaxVertexUniformComponents",
659 state->Const.MaxVertexUniformComponents);
660
661 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
662 * removed
663 */
664 add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4);
665
666 add_const("gl_MaxFragmentUniformComponents",
667 state->Const.MaxFragmentUniformComponents);
668 }
669
670 /* Texel offsets were introduced in ARB_shading_language_420pack (which
671 * requires desktop GLSL version 130), and adopted into desktop GLSL
672 * version 4.20 and GLSL ES version 3.00.
673 */
674 if ((state->is_version(130, 0) &&
675 state->ARB_shading_language_420pack_enable) ||
676 state->is_version(420, 300)) {
677 add_const("gl_MinProgramTexelOffset",
678 state->Const.MinProgramTexelOffset);
679 add_const("gl_MaxProgramTexelOffset",
680 state->Const.MaxProgramTexelOffset);
681 }
682
683 if (state->has_clip_distance()) {
684 add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes);
685 }
686 if (state->is_version(130, 0)) {
687 add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4);
688 }
689 if (state->has_cull_distance()) {
690 add_const("gl_MaxCullDistances", state->Const.MaxClipPlanes);
691 add_const("gl_MaxCombinedClipAndCullDistances",
692 state->Const.MaxClipPlanes);
693 }
694
695 if (state->has_geometry_shader()) {
696 add_const("gl_MaxVertexOutputComponents",
697 state->Const.MaxVertexOutputComponents);
698 add_const("gl_MaxGeometryInputComponents",
699 state->Const.MaxGeometryInputComponents);
700 add_const("gl_MaxGeometryOutputComponents",
701 state->Const.MaxGeometryOutputComponents);
702 add_const("gl_MaxFragmentInputComponents",
703 state->Const.MaxFragmentInputComponents);
704 add_const("gl_MaxGeometryTextureImageUnits",
705 state->Const.MaxGeometryTextureImageUnits);
706 add_const("gl_MaxGeometryOutputVertices",
707 state->Const.MaxGeometryOutputVertices);
708 add_const("gl_MaxGeometryTotalOutputComponents",
709 state->Const.MaxGeometryTotalOutputComponents);
710 add_const("gl_MaxGeometryUniformComponents",
711 state->Const.MaxGeometryUniformComponents);
712
713 /* Note: the GLSL 1.50-4.40 specs require
714 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
715 * But they do not define what it means (and there does not appear to be
716 * any corresponding constant in the GL specs). However,
717 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
718 * be the maximum number of components available for use as geometry
719 * outputs. So we assume this is a synonym for
720 * gl_MaxGeometryOutputComponents.
721 */
722 add_const("gl_MaxGeometryVaryingComponents",
723 state->Const.MaxGeometryOutputComponents);
724 }
725
726 if (compatibility) {
727 /* Note: gl_MaxLights stopped being listed as an explicit constant in
728 * GLSL 1.30, however it continues to be referred to (as a minimum size
729 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
730 * this seems like it was probably an oversight.
731 */
732 add_const("gl_MaxLights", state->Const.MaxLights);
733
734 add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes);
735
736 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
737 * 1.50, however this seems like it was probably an oversight.
738 */
739 add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits);
740
741 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
742 * re-introduced in GLSL 1.50, so this seems like it was probably an
743 * oversight.
744 */
745 add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords);
746 }
747
748 if (state->has_atomic_counters()) {
749 add_const("gl_MaxVertexAtomicCounters",
750 state->Const.MaxVertexAtomicCounters);
751 add_const("gl_MaxFragmentAtomicCounters",
752 state->Const.MaxFragmentAtomicCounters);
753 add_const("gl_MaxCombinedAtomicCounters",
754 state->Const.MaxCombinedAtomicCounters);
755 add_const("gl_MaxAtomicCounterBindings",
756 state->Const.MaxAtomicBufferBindings);
757
758 if (state->has_geometry_shader()) {
759 add_const("gl_MaxGeometryAtomicCounters",
760 state->Const.MaxGeometryAtomicCounters);
761 }
762 if (state->is_version(110, 320)) {
763 add_const("gl_MaxTessControlAtomicCounters",
764 state->Const.MaxTessControlAtomicCounters);
765 add_const("gl_MaxTessEvaluationAtomicCounters",
766 state->Const.MaxTessEvaluationAtomicCounters);
767 }
768 }
769
770 if (state->is_version(420, 310)) {
771 add_const("gl_MaxVertexAtomicCounterBuffers",
772 state->Const.MaxVertexAtomicCounterBuffers);
773 add_const("gl_MaxFragmentAtomicCounterBuffers",
774 state->Const.MaxFragmentAtomicCounterBuffers);
775 add_const("gl_MaxCombinedAtomicCounterBuffers",
776 state->Const.MaxCombinedAtomicCounterBuffers);
777 add_const("gl_MaxAtomicCounterBufferSize",
778 state->Const.MaxAtomicCounterBufferSize);
779
780 if (state->has_geometry_shader()) {
781 add_const("gl_MaxGeometryAtomicCounterBuffers",
782 state->Const.MaxGeometryAtomicCounterBuffers);
783 }
784 if (state->is_version(110, 320)) {
785 add_const("gl_MaxTessControlAtomicCounterBuffers",
786 state->Const.MaxTessControlAtomicCounterBuffers);
787 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
788 state->Const.MaxTessEvaluationAtomicCounterBuffers);
789 }
790 }
791
792 if (state->is_version(430, 310) || state->ARB_compute_shader_enable) {
793 add_const("gl_MaxComputeAtomicCounterBuffers",
794 state->Const.MaxComputeAtomicCounterBuffers);
795 add_const("gl_MaxComputeAtomicCounters",
796 state->Const.MaxComputeAtomicCounters);
797 add_const("gl_MaxComputeImageUniforms",
798 state->Const.MaxComputeImageUniforms);
799 add_const("gl_MaxComputeTextureImageUnits",
800 state->Const.MaxComputeTextureImageUnits);
801 add_const("gl_MaxComputeUniformComponents",
802 state->Const.MaxComputeUniformComponents);
803
804 add_const_ivec3("gl_MaxComputeWorkGroupCount",
805 state->Const.MaxComputeWorkGroupCount[0],
806 state->Const.MaxComputeWorkGroupCount[1],
807 state->Const.MaxComputeWorkGroupCount[2]);
808 add_const_ivec3("gl_MaxComputeWorkGroupSize",
809 state->Const.MaxComputeWorkGroupSize[0],
810 state->Const.MaxComputeWorkGroupSize[1],
811 state->Const.MaxComputeWorkGroupSize[2]);
812
813 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
814 *
815 * The built-in constant gl_WorkGroupSize is a compute-shader
816 * constant containing the local work-group size of the shader. The
817 * size of the work group in the X, Y, and Z dimensions is stored in
818 * the x, y, and z components. The constants values in
819 * gl_WorkGroupSize will match those specified in the required
820 * local_size_x, local_size_y, and local_size_z layout qualifiers
821 * for the current shader. This is a constant so that it can be
822 * used to size arrays of memory that can be shared within the local
823 * work group. It is a compile-time error to use gl_WorkGroupSize
824 * in a shader that does not declare a fixed local group size, or
825 * before that shader has declared a fixed local group size, using
826 * local_size_x, local_size_y, and local_size_z.
827 *
828 * To prevent the shader from trying to refer to gl_WorkGroupSize before
829 * the layout declaration, we don't define it here. Intead we define it
830 * in ast_cs_input_layout::hir().
831 */
832 }
833
834 if (state->has_enhanced_layouts()) {
835 add_const("gl_MaxTransformFeedbackBuffers",
836 state->Const.MaxTransformFeedbackBuffers);
837 add_const("gl_MaxTransformFeedbackInterleavedComponents",
838 state->Const.MaxTransformFeedbackInterleavedComponents);
839 }
840
841 if (state->is_version(420, 310) ||
842 state->ARB_shader_image_load_store_enable) {
843 add_const("gl_MaxImageUnits",
844 state->Const.MaxImageUnits);
845 add_const("gl_MaxVertexImageUniforms",
846 state->Const.MaxVertexImageUniforms);
847 add_const("gl_MaxFragmentImageUniforms",
848 state->Const.MaxFragmentImageUniforms);
849 add_const("gl_MaxCombinedImageUniforms",
850 state->Const.MaxCombinedImageUniforms);
851
852 if (state->has_geometry_shader()) {
853 add_const("gl_MaxGeometryImageUniforms",
854 state->Const.MaxGeometryImageUniforms);
855 }
856
857 if (!state->es_shader) {
858 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
859 state->Const.MaxCombinedShaderOutputResources);
860 add_const("gl_MaxImageSamples",
861 state->Const.MaxImageSamples);
862 }
863
864 if (state->has_tessellation_shader()) {
865 add_const("gl_MaxTessControlImageUniforms",
866 state->Const.MaxTessControlImageUniforms);
867 add_const("gl_MaxTessEvaluationImageUniforms",
868 state->Const.MaxTessEvaluationImageUniforms);
869 }
870 }
871
872 if (state->is_version(440, 310) ||
873 state->ARB_ES3_1_compatibility_enable) {
874 add_const("gl_MaxCombinedShaderOutputResources",
875 state->Const.MaxCombinedShaderOutputResources);
876 }
877
878 if (state->is_version(410, 0) ||
879 state->ARB_viewport_array_enable ||
880 state->OES_viewport_array_enable)
881 add_const("gl_MaxViewports", state->Const.MaxViewports);
882
883 if (state->has_tessellation_shader()) {
884 add_const("gl_MaxPatchVertices", state->Const.MaxPatchVertices);
885 add_const("gl_MaxTessGenLevel", state->Const.MaxTessGenLevel);
886 add_const("gl_MaxTessControlInputComponents", state->Const.MaxTessControlInputComponents);
887 add_const("gl_MaxTessControlOutputComponents", state->Const.MaxTessControlOutputComponents);
888 add_const("gl_MaxTessControlTextureImageUnits", state->Const.MaxTessControlTextureImageUnits);
889 add_const("gl_MaxTessEvaluationInputComponents", state->Const.MaxTessEvaluationInputComponents);
890 add_const("gl_MaxTessEvaluationOutputComponents", state->Const.MaxTessEvaluationOutputComponents);
891 add_const("gl_MaxTessEvaluationTextureImageUnits", state->Const.MaxTessEvaluationTextureImageUnits);
892 add_const("gl_MaxTessPatchComponents", state->Const.MaxTessPatchComponents);
893 add_const("gl_MaxTessControlTotalOutputComponents", state->Const.MaxTessControlTotalOutputComponents);
894 add_const("gl_MaxTessControlUniformComponents", state->Const.MaxTessControlUniformComponents);
895 add_const("gl_MaxTessEvaluationUniformComponents", state->Const.MaxTessEvaluationUniformComponents);
896 }
897
898 if (state->is_version(450, 320) ||
899 state->OES_sample_variables_enable ||
900 state->ARB_ES3_1_compatibility_enable)
901 add_const("gl_MaxSamples", state->Const.MaxSamples);
902 }
903
904
905 /**
906 * Generate uniform variables (which exist in all types of shaders).
907 */
908 void
909 builtin_variable_generator::generate_uniforms()
910 {
911 if (state->is_version(400, 320) ||
912 state->ARB_sample_shading_enable ||
913 state->OES_sample_variables_enable)
914 add_uniform(int_t, "gl_NumSamples");
915 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
916 add_uniform(array(vec4_t, VERT_ATTRIB_MAX), "gl_CurrentAttribVertMESA");
917 add_uniform(array(vec4_t, VARYING_SLOT_MAX), "gl_CurrentAttribFragMESA");
918
919 if (compatibility) {
920 add_uniform(mat4_t, "gl_ModelViewMatrix");
921 add_uniform(mat4_t, "gl_ProjectionMatrix");
922 add_uniform(mat4_t, "gl_ModelViewProjectionMatrix");
923 add_uniform(mat3_t, "gl_NormalMatrix");
924 add_uniform(mat4_t, "gl_ModelViewMatrixInverse");
925 add_uniform(mat4_t, "gl_ProjectionMatrixInverse");
926 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse");
927 add_uniform(mat4_t, "gl_ModelViewMatrixTranspose");
928 add_uniform(mat4_t, "gl_ProjectionMatrixTranspose");
929 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose");
930 add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose");
931 add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose");
932 add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose");
933 add_uniform(float_t, "gl_NormalScale");
934 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
935 add_uniform(vec4_t, "gl_FogParamsOptimizedMESA");
936
937 const glsl_type *const mat4_array_type =
938 array(mat4_t, state->Const.MaxTextureCoords);
939 add_uniform(mat4_array_type, "gl_TextureMatrix");
940 add_uniform(mat4_array_type, "gl_TextureMatrixInverse");
941 add_uniform(mat4_array_type, "gl_TextureMatrixTranspose");
942 add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose");
943
944 add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane");
945 add_uniform(type("gl_PointParameters"), "gl_Point");
946
947 const glsl_type *const material_parameters_type =
948 type("gl_MaterialParameters");
949 add_uniform(material_parameters_type, "gl_FrontMaterial");
950 add_uniform(material_parameters_type, "gl_BackMaterial");
951
952 add_uniform(array(type("gl_LightSourceParameters"),
953 state->Const.MaxLights),
954 "gl_LightSource");
955
956 const glsl_type *const light_model_products_type =
957 type("gl_LightModelProducts");
958 add_uniform(light_model_products_type, "gl_FrontLightModelProduct");
959 add_uniform(light_model_products_type, "gl_BackLightModelProduct");
960
961 const glsl_type *const light_products_type =
962 array(type("gl_LightProducts"), state->Const.MaxLights);
963 add_uniform(light_products_type, "gl_FrontLightProduct");
964 add_uniform(light_products_type, "gl_BackLightProduct");
965
966 add_uniform(array(vec4_t, state->Const.MaxTextureUnits),
967 "gl_TextureEnvColor");
968
969 const glsl_type *const texcoords_vec4 =
970 array(vec4_t, state->Const.MaxTextureCoords);
971 add_uniform(texcoords_vec4, "gl_EyePlaneS");
972 add_uniform(texcoords_vec4, "gl_EyePlaneT");
973 add_uniform(texcoords_vec4, "gl_EyePlaneR");
974 add_uniform(texcoords_vec4, "gl_EyePlaneQ");
975 add_uniform(texcoords_vec4, "gl_ObjectPlaneS");
976 add_uniform(texcoords_vec4, "gl_ObjectPlaneT");
977 add_uniform(texcoords_vec4, "gl_ObjectPlaneR");
978 add_uniform(texcoords_vec4, "gl_ObjectPlaneQ");
979
980 add_uniform(type("gl_FogParameters"), "gl_Fog");
981 }
982 }
983
984
985 /**
986 * Generate variables which only exist in vertex shaders.
987 */
988 void
989 builtin_variable_generator::generate_vs_special_vars()
990 {
991 ir_variable *var;
992
993 if (state->is_version(130, 300))
994 add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID");
995 if (state->ARB_draw_instanced_enable)
996 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB");
997 if (state->ARB_draw_instanced_enable || state->is_version(140, 300))
998 add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceID");
999 if (state->ARB_shader_draw_parameters_enable) {
1000 add_system_value(SYSTEM_VALUE_BASE_VERTEX, int_t, "gl_BaseVertexARB");
1001 add_system_value(SYSTEM_VALUE_BASE_INSTANCE, int_t, "gl_BaseInstanceARB");
1002 add_system_value(SYSTEM_VALUE_DRAW_ID, int_t, "gl_DrawIDARB");
1003 }
1004 if (state->AMD_vertex_shader_layer_enable ||
1005 state->ARB_shader_viewport_layer_array_enable) {
1006 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
1007 var->data.interpolation = INTERP_MODE_FLAT;
1008 }
1009 if (state->AMD_vertex_shader_viewport_index_enable ||
1010 state->ARB_shader_viewport_layer_array_enable) {
1011 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
1012 var->data.interpolation = INTERP_MODE_FLAT;
1013 }
1014 if (compatibility) {
1015 add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex");
1016 add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal");
1017 add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color");
1018 add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor");
1019 add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0");
1020 add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1");
1021 add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2");
1022 add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3");
1023 add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4");
1024 add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5");
1025 add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6");
1026 add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7");
1027 add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord");
1028 }
1029 }
1030
1031
1032 /**
1033 * Generate variables which only exist in tessellation control shaders.
1034 */
1035 void
1036 builtin_variable_generator::generate_tcs_special_vars()
1037 {
1038 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
1039 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID");
1040
1041 if (state->ctx->Const.LowerTCSPatchVerticesIn) {
1042 add_uniform(int_t, "gl_PatchVerticesIn");
1043 } else {
1044 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, "gl_PatchVerticesIn");
1045 }
1046
1047 add_output(VARYING_SLOT_TESS_LEVEL_OUTER, array(float_t, 4),
1048 "gl_TessLevelOuter")->data.patch = 1;
1049 add_output(VARYING_SLOT_TESS_LEVEL_INNER, array(float_t, 2),
1050 "gl_TessLevelInner")->data.patch = 1;
1051 /* XXX What to do if multiple are flipped on? */
1052 int bbox_slot = state->ctx->Const.NoPrimitiveBoundingBoxOutput ? -1 :
1053 VARYING_SLOT_BOUNDING_BOX0;
1054 if (state->EXT_primitive_bounding_box_enable)
1055 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBoxEXT")
1056 ->data.patch = 1;
1057 if (state->OES_primitive_bounding_box_enable)
1058 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBoxOES")
1059 ->data.patch = 1;
1060 if (state->is_version(0, 320) || state->ARB_ES3_2_compatibility_enable)
1061 add_output(bbox_slot, array(vec4_t, 2), "gl_BoundingBox")
1062 ->data.patch = 1;
1063 }
1064
1065
1066 /**
1067 * Generate variables which only exist in tessellation evaluation shaders.
1068 */
1069 void
1070 builtin_variable_generator::generate_tes_special_vars()
1071 {
1072 ir_variable *var;
1073
1074 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
1075 add_system_value(SYSTEM_VALUE_VERTICES_IN, int_t, "gl_PatchVerticesIn");
1076 add_system_value(SYSTEM_VALUE_TESS_COORD, vec3_t, "gl_TessCoord");
1077 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER, array(float_t, 4),
1078 "gl_TessLevelOuter");
1079 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER, array(float_t, 2),
1080 "gl_TessLevelInner");
1081 if (state->ARB_shader_viewport_layer_array_enable) {
1082 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
1083 var->data.interpolation = INTERP_MODE_FLAT;
1084 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
1085 var->data.interpolation = INTERP_MODE_FLAT;
1086 }
1087 }
1088
1089
1090 /**
1091 * Generate variables which only exist in geometry shaders.
1092 */
1093 void
1094 builtin_variable_generator::generate_gs_special_vars()
1095 {
1096 ir_variable *var;
1097
1098 var = add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
1099 var->data.interpolation = INTERP_MODE_FLAT;
1100 if (state->is_version(410, 0) || state->ARB_viewport_array_enable ||
1101 state->OES_viewport_array_enable) {
1102 var = add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
1103 var->data.interpolation = INTERP_MODE_FLAT;
1104 }
1105 if (state->is_version(400, 320) || state->ARB_gpu_shader5_enable ||
1106 state->OES_geometry_shader_enable || state->EXT_geometry_shader_enable) {
1107 add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID");
1108 }
1109
1110 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1111 * evaluation shaders, it has a different function there than it has in
1112 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1113 * as special geometry shader variables.
1114 *
1115 * Note that although the general convention of suffixing geometry shader
1116 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1117 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1118 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1119 */
1120 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveIDIn");
1121 var->data.interpolation = INTERP_MODE_FLAT;
1122 var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
1123 var->data.interpolation = INTERP_MODE_FLAT;
1124 }
1125
1126
1127 /**
1128 * Generate variables which only exist in fragment shaders.
1129 */
1130 void
1131 builtin_variable_generator::generate_fs_special_vars()
1132 {
1133 ir_variable *var;
1134
1135 if (this->state->ctx->Const.GLSLFragCoordIsSysVal)
1136 add_system_value(SYSTEM_VALUE_FRAG_COORD, vec4_t, "gl_FragCoord");
1137 else
1138 add_input(VARYING_SLOT_POS, vec4_t, "gl_FragCoord");
1139
1140 if (this->state->ctx->Const.GLSLFrontFacingIsSysVal)
1141 add_system_value(SYSTEM_VALUE_FRONT_FACE, bool_t, "gl_FrontFacing");
1142 else
1143 add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing");
1144
1145 if (state->is_version(120, 100))
1146 add_input(VARYING_SLOT_PNTC, vec2_t, "gl_PointCoord");
1147
1148 if (state->has_geometry_shader()) {
1149 var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
1150 var->data.interpolation = INTERP_MODE_FLAT;
1151 }
1152
1153 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1154 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1155 * They were removed from GLSL ES 3.00.
1156 */
1157 if (compatibility || !state->is_version(420, 300)) {
1158 add_output(FRAG_RESULT_COLOR, vec4_t, "gl_FragColor");
1159 add_output(FRAG_RESULT_DATA0,
1160 array(vec4_t, state->Const.MaxDrawBuffers), "gl_FragData");
1161 }
1162
1163 if (state->has_framebuffer_fetch() && !state->is_version(130, 300)) {
1164 ir_variable *const var =
1165 add_output(FRAG_RESULT_DATA0,
1166 array(vec4_t, state->Const.MaxDrawBuffers),
1167 "gl_LastFragData");
1168 var->data.precision = GLSL_PRECISION_MEDIUM;
1169 var->data.read_only = 1;
1170 var->data.fb_fetch_output = 1;
1171 }
1172
1173 if (state->es_shader && state->language_version == 100 && state->EXT_blend_func_extended_enable) {
1174 add_index_output(FRAG_RESULT_COLOR, 1, vec4_t,
1175 "gl_SecondaryFragColorEXT");
1176 add_index_output(FRAG_RESULT_DATA0, 1,
1177 array(vec4_t, state->Const.MaxDualSourceDrawBuffers),
1178 "gl_SecondaryFragDataEXT");
1179 }
1180
1181 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1182 * ES 1.00.
1183 */
1184 if (state->is_version(110, 300))
1185 add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepth");
1186
1187 if (state->ARB_shader_stencil_export_enable) {
1188 ir_variable *const var =
1189 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB");
1190 if (state->ARB_shader_stencil_export_warn)
1191 var->enable_extension_warning("GL_ARB_shader_stencil_export");
1192 }
1193
1194 if (state->AMD_shader_stencil_export_enable) {
1195 ir_variable *const var =
1196 add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD");
1197 if (state->AMD_shader_stencil_export_warn)
1198 var->enable_extension_warning("GL_AMD_shader_stencil_export");
1199 }
1200
1201 if (state->is_version(400, 320) ||
1202 state->ARB_sample_shading_enable ||
1203 state->OES_sample_variables_enable) {
1204 add_system_value(SYSTEM_VALUE_SAMPLE_ID, int_t, "gl_SampleID");
1205 add_system_value(SYSTEM_VALUE_SAMPLE_POS, vec2_t, "gl_SamplePosition");
1206 /* From the ARB_sample_shading specification:
1207 * "The number of elements in the array is ceil(<s>/32), where
1208 * <s> is the maximum number of color samples supported by the
1209 * implementation."
1210 * Since no drivers expose more than 32x MSAA, we can simply set
1211 * the array size to 1 rather than computing it.
1212 */
1213 add_output(FRAG_RESULT_SAMPLE_MASK, array(int_t, 1), "gl_SampleMask");
1214 }
1215
1216 if (state->is_version(400, 320) ||
1217 state->ARB_gpu_shader5_enable ||
1218 state->OES_sample_variables_enable) {
1219 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN, array(int_t, 1), "gl_SampleMaskIn");
1220 }
1221
1222 if (state->is_version(430, 320) ||
1223 state->ARB_fragment_layer_viewport_enable ||
1224 state->OES_geometry_shader_enable ||
1225 state->EXT_geometry_shader_enable) {
1226 var = add_input(VARYING_SLOT_LAYER, int_t, "gl_Layer");
1227 var->data.interpolation = INTERP_MODE_FLAT;
1228 }
1229
1230 if (state->is_version(430, 0) ||
1231 state->ARB_fragment_layer_viewport_enable ||
1232 state->OES_viewport_array_enable) {
1233 var = add_input(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
1234 var->data.interpolation = INTERP_MODE_FLAT;
1235 }
1236
1237 if (state->is_version(450, 310) || state->ARB_ES3_1_compatibility_enable)
1238 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION, bool_t, "gl_HelperInvocation");
1239 }
1240
1241
1242 /**
1243 * Generate variables which only exist in compute shaders.
1244 */
1245 void
1246 builtin_variable_generator::generate_cs_special_vars()
1247 {
1248 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID, uvec3_t,
1249 "gl_LocalInvocationID");
1250 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID, uvec3_t, "gl_WorkGroupID");
1251 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS, uvec3_t, "gl_NumWorkGroups");
1252
1253 if (state->ARB_compute_variable_group_size_enable) {
1254 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE,
1255 uvec3_t, "gl_LocalGroupSizeARB");
1256 }
1257
1258 if (state->ctx->Const.LowerCsDerivedVariables) {
1259 add_variable("gl_GlobalInvocationID", uvec3_t, ir_var_auto, 0);
1260 add_variable("gl_LocalInvocationIndex", uint_t, ir_var_auto, 0);
1261 } else {
1262 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID,
1263 uvec3_t, "gl_GlobalInvocationID");
1264 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX,
1265 uint_t, "gl_LocalInvocationIndex");
1266 }
1267 }
1268
1269
1270 /**
1271 * Add a single "varying" variable. The variable's type and direction (input
1272 * or output) are adjusted as appropriate for the type of shader being
1273 * compiled.
1274 */
1275 void
1276 builtin_variable_generator::add_varying(int slot, const glsl_type *type,
1277 const char *name)
1278 {
1279 switch (state->stage) {
1280 case MESA_SHADER_TESS_CTRL:
1281 case MESA_SHADER_TESS_EVAL:
1282 case MESA_SHADER_GEOMETRY:
1283 this->per_vertex_in.add_field(slot, type, name);
1284 /* FALLTHROUGH */
1285 case MESA_SHADER_VERTEX:
1286 this->per_vertex_out.add_field(slot, type, name);
1287 break;
1288 case MESA_SHADER_FRAGMENT:
1289 add_input(slot, type, name);
1290 break;
1291 case MESA_SHADER_COMPUTE:
1292 /* Compute shaders don't have varyings. */
1293 break;
1294 }
1295 }
1296
1297
1298 /**
1299 * Generate variables that are used to communicate data from one shader stage
1300 * to the next ("varyings").
1301 */
1302 void
1303 builtin_variable_generator::generate_varyings()
1304 {
1305 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1306 if (state->stage != MESA_SHADER_FRAGMENT) {
1307 add_varying(VARYING_SLOT_POS, vec4_t, "gl_Position");
1308 if (!state->es_shader ||
1309 state->stage == MESA_SHADER_VERTEX ||
1310 (state->stage == MESA_SHADER_GEOMETRY &&
1311 (state->OES_geometry_point_size_enable ||
1312 state->EXT_geometry_point_size_enable)) ||
1313 ((state->stage == MESA_SHADER_TESS_CTRL ||
1314 state->stage == MESA_SHADER_TESS_EVAL) &&
1315 (state->OES_tessellation_point_size_enable ||
1316 state->EXT_tessellation_point_size_enable))) {
1317 add_varying(VARYING_SLOT_PSIZ, float_t, "gl_PointSize");
1318 }
1319 }
1320
1321 if (state->has_clip_distance()) {
1322 add_varying(VARYING_SLOT_CLIP_DIST0, array(float_t, 0),
1323 "gl_ClipDistance");
1324 }
1325 if (state->has_cull_distance()) {
1326 add_varying(VARYING_SLOT_CULL_DIST0, array(float_t, 0),
1327 "gl_CullDistance");
1328 }
1329
1330 if (compatibility) {
1331 add_varying(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord");
1332 add_varying(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord");
1333 if (state->stage == MESA_SHADER_FRAGMENT) {
1334 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_Color");
1335 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor");
1336 } else {
1337 add_varying(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex");
1338 add_varying(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor");
1339 add_varying(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor");
1340 add_varying(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor");
1341 add_varying(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor");
1342 }
1343 }
1344
1345 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1346 * says:
1347 *
1348 * "In the tessellation control language, built-in variables are
1349 * intrinsically declared as:
1350 *
1351 * in gl_PerVertex {
1352 * vec4 gl_Position;
1353 * float gl_PointSize;
1354 * float gl_ClipDistance[];
1355 * } gl_in[gl_MaxPatchVertices];"
1356 */
1357 if (state->stage == MESA_SHADER_TESS_CTRL ||
1358 state->stage == MESA_SHADER_TESS_EVAL) {
1359 const glsl_type *per_vertex_in_type =
1360 this->per_vertex_in.construct_interface_instance();
1361 add_variable("gl_in", array(per_vertex_in_type, state->Const.MaxPatchVertices),
1362 ir_var_shader_in, -1);
1363 }
1364 if (state->stage == MESA_SHADER_GEOMETRY) {
1365 const glsl_type *per_vertex_in_type =
1366 this->per_vertex_in.construct_interface_instance();
1367 add_variable("gl_in", array(per_vertex_in_type, 0),
1368 ir_var_shader_in, -1);
1369 }
1370 if (state->stage == MESA_SHADER_TESS_CTRL) {
1371 const glsl_type *per_vertex_out_type =
1372 this->per_vertex_out.construct_interface_instance();
1373 add_variable("gl_out", array(per_vertex_out_type, 0),
1374 ir_var_shader_out, -1);
1375 }
1376 if (state->stage == MESA_SHADER_VERTEX ||
1377 state->stage == MESA_SHADER_TESS_EVAL ||
1378 state->stage == MESA_SHADER_GEOMETRY) {
1379 const glsl_type *per_vertex_out_type =
1380 this->per_vertex_out.construct_interface_instance();
1381 const glsl_struct_field *fields = per_vertex_out_type->fields.structure;
1382 for (unsigned i = 0; i < per_vertex_out_type->length; i++) {
1383 ir_variable *var =
1384 add_variable(fields[i].name, fields[i].type, ir_var_shader_out,
1385 fields[i].location);
1386 var->data.interpolation = fields[i].interpolation;
1387 var->data.centroid = fields[i].centroid;
1388 var->data.sample = fields[i].sample;
1389 var->data.patch = fields[i].patch;
1390 var->data.precision = fields[i].precision;
1391 var->init_interface_type(per_vertex_out_type);
1392 }
1393 }
1394 }
1395
1396
1397 }; /* Anonymous namespace */
1398
1399
1400 void
1401 _mesa_glsl_initialize_variables(exec_list *instructions,
1402 struct _mesa_glsl_parse_state *state)
1403 {
1404 builtin_variable_generator gen(instructions, state);
1405
1406 gen.generate_constants();
1407 gen.generate_uniforms();
1408
1409 gen.generate_varyings();
1410
1411 switch (state->stage) {
1412 case MESA_SHADER_VERTEX:
1413 gen.generate_vs_special_vars();
1414 break;
1415 case MESA_SHADER_TESS_CTRL:
1416 gen.generate_tcs_special_vars();
1417 break;
1418 case MESA_SHADER_TESS_EVAL:
1419 gen.generate_tes_special_vars();
1420 break;
1421 case MESA_SHADER_GEOMETRY:
1422 gen.generate_gs_special_vars();
1423 break;
1424 case MESA_SHADER_FRAGMENT:
1425 gen.generate_fs_special_vars();
1426 break;
1427 case MESA_SHADER_COMPUTE:
1428 gen.generate_cs_special_vars();
1429 break;
1430 }
1431 }
1432
1433
1434 /**
1435 * Initialize compute shader variables with values that are derived from other
1436 * compute shader variable.
1437 */
1438 static void
1439 initialize_cs_derived_variables(gl_shader *shader,
1440 ir_function_signature *const main_sig)
1441 {
1442 assert(shader->Stage == MESA_SHADER_COMPUTE);
1443
1444 ir_variable *gl_GlobalInvocationID =
1445 shader->symbols->get_variable("gl_GlobalInvocationID");
1446 assert(gl_GlobalInvocationID);
1447 ir_variable *gl_WorkGroupID =
1448 shader->symbols->get_variable("gl_WorkGroupID");
1449 assert(gl_WorkGroupID);
1450 ir_variable *gl_WorkGroupSize =
1451 shader->symbols->get_variable("gl_WorkGroupSize");
1452 if (gl_WorkGroupSize == NULL) {
1453 void *const mem_ctx = ralloc_parent(shader->ir);
1454 gl_WorkGroupSize = new(mem_ctx) ir_variable(glsl_type::uvec3_type,
1455 "gl_WorkGroupSize",
1456 ir_var_auto);
1457 gl_WorkGroupSize->data.how_declared = ir_var_declared_implicitly;
1458 gl_WorkGroupSize->data.read_only = true;
1459 shader->ir->push_head(gl_WorkGroupSize);
1460 }
1461 ir_variable *gl_LocalInvocationID =
1462 shader->symbols->get_variable("gl_LocalInvocationID");
1463 assert(gl_LocalInvocationID);
1464
1465 /* gl_GlobalInvocationID =
1466 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1467 */
1468 ir_instruction *inst =
1469 assign(gl_GlobalInvocationID,
1470 add(mul(gl_WorkGroupID, gl_WorkGroupSize),
1471 gl_LocalInvocationID));
1472 main_sig->body.push_head(inst);
1473
1474 /* gl_LocalInvocationIndex =
1475 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1476 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1477 * gl_LocalInvocationID.x;
1478 */
1479 ir_expression *index_z =
1480 mul(mul(swizzle_z(gl_LocalInvocationID), swizzle_x(gl_WorkGroupSize)),
1481 swizzle_y(gl_WorkGroupSize));
1482 ir_expression *index_y =
1483 mul(swizzle_y(gl_LocalInvocationID), swizzle_x(gl_WorkGroupSize));
1484 ir_expression *index_y_plus_z = add(index_y, index_z);
1485 operand index_x(swizzle_x(gl_LocalInvocationID));
1486 ir_expression *index_x_plus_y_plus_z = add(index_y_plus_z, index_x);
1487 ir_variable *gl_LocalInvocationIndex =
1488 shader->symbols->get_variable("gl_LocalInvocationIndex");
1489 assert(gl_LocalInvocationIndex);
1490 inst = assign(gl_LocalInvocationIndex, index_x_plus_y_plus_z);
1491 main_sig->body.push_head(inst);
1492 }
1493
1494
1495 /**
1496 * Initialize builtin variables with values based on other builtin variables.
1497 * These are initialized in the main function.
1498 */
1499 void
1500 _mesa_glsl_initialize_derived_variables(struct gl_context *ctx,
1501 gl_shader *shader)
1502 {
1503 /* We only need to set CS variables currently. */
1504 if (shader->Stage == MESA_SHADER_COMPUTE &&
1505 ctx->Const.LowerCsDerivedVariables) {
1506 ir_function_signature *const main_sig =
1507 _mesa_get_main_function_signature(shader->symbols);
1508
1509 if (main_sig != NULL)
1510 initialize_cs_derived_variables(shader, main_sig);
1511 }
1512 }