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