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