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