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