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