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