2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 #include "ir_builder.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_symbol_table.h"
29 #include "main/core.h"
30 #include "main/uniforms.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_instruction.h"
33 #include "builtin_functions.h"
35 using namespace ir_builder
;
37 static const struct gl_builtin_uniform_element gl_NumSamples_elements
[] = {
38 {NULL
, {STATE_NUM_SAMPLES
, 0, 0}, SWIZZLE_XXXX
}
42 static const struct gl_builtin_uniform_element gl_PatchVerticesIn_elements
[] = {
43 {NULL
, {STATE_INTERNAL
, STATE_TCS_PATCH_VERTICES_IN
}, SWIZZLE_XXXX
}
46 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
47 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
48 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
49 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
52 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
53 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
56 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
57 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
58 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
59 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
60 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
61 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
62 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
63 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
66 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
67 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
68 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
69 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
70 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
71 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
74 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
75 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
76 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
77 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
78 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
79 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
82 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
83 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
84 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
85 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
86 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
87 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
88 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
89 MAKE_SWIZZLE4(SWIZZLE_X
,
93 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
94 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
95 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
96 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
97 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
98 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
101 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
102 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
105 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
106 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
109 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
110 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
113 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
114 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
115 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
116 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
119 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
120 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
121 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
122 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
125 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
126 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
129 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
130 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
133 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
134 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
137 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
138 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
141 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
142 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
145 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
146 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
149 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
150 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
153 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
154 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
157 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
158 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
161 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
162 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
163 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
164 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
165 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
166 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
169 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
170 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
173 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
174 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
177 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
178 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
181 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
182 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
185 #define MATRIX(name, statevar, modifier) \
186 static const struct gl_builtin_uniform_element name ## _elements[] = { \
187 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
188 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
189 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
190 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
193 MATRIX(gl_ModelViewMatrix
,
194 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
195 MATRIX(gl_ModelViewMatrixInverse
,
196 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
197 MATRIX(gl_ModelViewMatrixTranspose
,
198 STATE_MODELVIEW_MATRIX
, 0);
199 MATRIX(gl_ModelViewMatrixInverseTranspose
,
200 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
202 MATRIX(gl_ProjectionMatrix
,
203 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
204 MATRIX(gl_ProjectionMatrixInverse
,
205 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
206 MATRIX(gl_ProjectionMatrixTranspose
,
207 STATE_PROJECTION_MATRIX
, 0);
208 MATRIX(gl_ProjectionMatrixInverseTranspose
,
209 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
211 MATRIX(gl_ModelViewProjectionMatrix
,
212 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
213 MATRIX(gl_ModelViewProjectionMatrixInverse
,
214 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
215 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
216 STATE_MVP_MATRIX
, 0);
217 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
218 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
220 MATRIX(gl_TextureMatrix
,
221 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
222 MATRIX(gl_TextureMatrixInverse
,
223 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
224 MATRIX(gl_TextureMatrixTranspose
,
225 STATE_TEXTURE_MATRIX
, 0);
226 MATRIX(gl_TextureMatrixInverseTranspose
,
227 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
229 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
230 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
231 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
232 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
233 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
234 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
235 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
240 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
242 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
243 STATEVAR(gl_PatchVerticesIn
),
244 STATEVAR(gl_NumSamples
),
245 STATEVAR(gl_DepthRange
),
246 STATEVAR(gl_ClipPlane
),
248 STATEVAR(gl_FrontMaterial
),
249 STATEVAR(gl_BackMaterial
),
250 STATEVAR(gl_LightSource
),
251 STATEVAR(gl_LightModel
),
252 STATEVAR(gl_FrontLightModelProduct
),
253 STATEVAR(gl_BackLightModelProduct
),
254 STATEVAR(gl_FrontLightProduct
),
255 STATEVAR(gl_BackLightProduct
),
256 STATEVAR(gl_TextureEnvColor
),
257 STATEVAR(gl_EyePlaneS
),
258 STATEVAR(gl_EyePlaneT
),
259 STATEVAR(gl_EyePlaneR
),
260 STATEVAR(gl_EyePlaneQ
),
261 STATEVAR(gl_ObjectPlaneS
),
262 STATEVAR(gl_ObjectPlaneT
),
263 STATEVAR(gl_ObjectPlaneR
),
264 STATEVAR(gl_ObjectPlaneQ
),
267 STATEVAR(gl_ModelViewMatrix
),
268 STATEVAR(gl_ModelViewMatrixInverse
),
269 STATEVAR(gl_ModelViewMatrixTranspose
),
270 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
272 STATEVAR(gl_ProjectionMatrix
),
273 STATEVAR(gl_ProjectionMatrixInverse
),
274 STATEVAR(gl_ProjectionMatrixTranspose
),
275 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
277 STATEVAR(gl_ModelViewProjectionMatrix
),
278 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
279 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
280 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
282 STATEVAR(gl_TextureMatrix
),
283 STATEVAR(gl_TextureMatrixInverse
),
284 STATEVAR(gl_TextureMatrixTranspose
),
285 STATEVAR(gl_TextureMatrixInverseTranspose
),
287 STATEVAR(gl_NormalMatrix
),
288 STATEVAR(gl_NormalScale
),
290 STATEVAR(gl_FogParamsOptimizedMESA
),
291 STATEVAR(gl_CurrentAttribVertMESA
),
292 STATEVAR(gl_CurrentAttribFragMESA
),
301 * Data structure that accumulates fields for the gl_PerVertex interface
304 class per_vertex_accumulator
307 per_vertex_accumulator();
308 void add_field(int slot
, const glsl_type
*type
, const char *name
);
309 const glsl_type
*construct_interface_instance() const;
312 glsl_struct_field fields
[11];
317 per_vertex_accumulator::per_vertex_accumulator()
325 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
328 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
329 this->fields
[this->num_fields
].type
= type
;
330 this->fields
[this->num_fields
].name
= name
;
331 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
332 this->fields
[this->num_fields
].location
= slot
;
333 this->fields
[this->num_fields
].offset
= -1;
334 this->fields
[this->num_fields
].interpolation
= INTERP_MODE_NONE
;
335 this->fields
[this->num_fields
].centroid
= 0;
336 this->fields
[this->num_fields
].sample
= 0;
337 this->fields
[this->num_fields
].patch
= 0;
338 this->fields
[this->num_fields
].precision
= GLSL_PRECISION_NONE
;
339 this->fields
[this->num_fields
].memory_read_only
= 0;
340 this->fields
[this->num_fields
].memory_write_only
= 0;
341 this->fields
[this->num_fields
].memory_coherent
= 0;
342 this->fields
[this->num_fields
].memory_volatile
= 0;
343 this->fields
[this->num_fields
].memory_restrict
= 0;
344 this->fields
[this->num_fields
].image_format
= 0;
345 this->fields
[this->num_fields
].explicit_xfb_buffer
= 0;
346 this->fields
[this->num_fields
].xfb_buffer
= -1;
347 this->fields
[this->num_fields
].xfb_stride
= -1;
353 per_vertex_accumulator::construct_interface_instance() const
355 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
356 GLSL_INTERFACE_PACKING_STD140
,
362 class builtin_variable_generator
365 builtin_variable_generator(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
);
367 void generate_constants();
368 void generate_uniforms();
369 void generate_special_vars();
370 void generate_vs_special_vars();
371 void generate_tcs_special_vars();
372 void generate_tes_special_vars();
373 void generate_gs_special_vars();
374 void generate_fs_special_vars();
375 void generate_cs_special_vars();
376 void generate_varyings();
379 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
381 return glsl_type::get_array_instance(base
, elements
);
384 const glsl_type
*type(const char *name
)
386 return symtab
->get_type(name
);
389 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
391 return add_variable(name
, type
, ir_var_shader_in
, slot
);
394 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
396 return add_variable(name
, type
, ir_var_shader_out
, slot
);
399 ir_variable
*add_index_output(int slot
, int index
, const glsl_type
*type
, const char *name
)
401 return add_index_variable(name
, type
, ir_var_shader_out
, slot
, index
);
404 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
407 return add_variable(name
, type
, ir_var_system_value
, slot
);
410 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
411 enum ir_variable_mode mode
, int slot
);
412 ir_variable
*add_index_variable(const char *name
, const glsl_type
*type
,
413 enum ir_variable_mode mode
, int slot
, int index
);
414 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
415 ir_variable
*add_const(const char *name
, int value
);
416 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
417 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
419 exec_list
* const instructions
;
420 struct _mesa_glsl_parse_state
* const state
;
421 glsl_symbol_table
* const symtab
;
424 * True if compatibility-profile-only variables should be included. (In
425 * desktop GL, these are always included when the GLSL version is 1.30 and
428 const bool compatibility
;
430 const glsl_type
* const bool_t
;
431 const glsl_type
* const int_t
;
432 const glsl_type
* const uint_t
;
433 const glsl_type
* const uint64_t;
434 const glsl_type
* const float_t
;
435 const glsl_type
* const vec2_t
;
436 const glsl_type
* const vec3_t
;
437 const glsl_type
* const vec4_t
;
438 const glsl_type
* const uvec3_t
;
439 const glsl_type
* const mat3_t
;
440 const glsl_type
* const mat4_t
;
442 per_vertex_accumulator per_vertex_in
;
443 per_vertex_accumulator per_vertex_out
;
447 builtin_variable_generator::builtin_variable_generator(
448 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
449 : instructions(instructions
), state(state
), symtab(state
->symbols
),
450 compatibility(state
->compat_shader
|| !state
->is_version(140, 100)),
451 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
452 uint_t(glsl_type::uint_type
),
453 uint64_t(glsl_type::uint64_t_type
),
454 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
455 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
456 uvec3_t(glsl_type::uvec3_type
),
457 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
462 builtin_variable_generator::add_index_variable(const char *name
,
463 const glsl_type
*type
,
464 enum ir_variable_mode mode
, int slot
, int index
)
466 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
467 var
->data
.how_declared
= ir_var_declared_implicitly
;
469 switch (var
->data
.mode
) {
471 case ir_var_shader_in
:
473 case ir_var_system_value
:
474 var
->data
.read_only
= true;
476 case ir_var_shader_out
:
477 case ir_var_shader_storage
:
480 /* The only variables that are added using this function should be
481 * uniforms, shader storage, shader inputs, and shader outputs, constants
482 * (which use ir_var_auto), and system values.
488 var
->data
.location
= slot
;
489 var
->data
.explicit_location
= (slot
>= 0);
490 var
->data
.explicit_index
= 1;
491 var
->data
.index
= index
;
493 /* Once the variable is created an initialized, add it to the symbol table
494 * and add the declaration to the IR stream.
496 instructions
->push_tail(var
);
498 symtab
->add_variable(var
);
503 builtin_variable_generator::add_variable(const char *name
,
504 const glsl_type
*type
,
505 enum ir_variable_mode mode
, int slot
)
507 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
508 var
->data
.how_declared
= ir_var_declared_implicitly
;
510 switch (var
->data
.mode
) {
512 case ir_var_shader_in
:
514 case ir_var_system_value
:
515 var
->data
.read_only
= true;
517 case ir_var_shader_out
:
518 case ir_var_shader_storage
:
521 /* The only variables that are added using this function should be
522 * uniforms, shader storage, shader inputs, and shader outputs, constants
523 * (which use ir_var_auto), and system values.
529 var
->data
.location
= slot
;
530 var
->data
.explicit_location
= (slot
>= 0);
531 var
->data
.explicit_index
= 0;
533 /* Once the variable is created an initialized, add it to the symbol table
534 * and add the declaration to the IR stream.
536 instructions
->push_tail(var
);
538 symtab
->add_variable(var
);
542 extern "C" const struct gl_builtin_uniform_desc
*
543 _mesa_glsl_get_builtin_uniform_desc(const char *name
)
545 for (unsigned i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
546 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
547 return &_mesa_builtin_uniform_desc
[i
];
554 builtin_variable_generator::add_uniform(const glsl_type
*type
,
557 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
559 const struct gl_builtin_uniform_desc
* const statevar
=
560 _mesa_glsl_get_builtin_uniform_desc(name
);
561 assert(statevar
!= NULL
);
563 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
565 ir_state_slot
*slots
=
566 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
568 for (unsigned a
= 0; a
< array_count
; a
++) {
569 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
570 const struct gl_builtin_uniform_element
*element
=
571 &statevar
->elements
[j
];
573 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
574 if (type
->is_array()) {
575 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
576 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
577 slots
->tokens
[2] = a
;
579 slots
->tokens
[1] = a
;
583 slots
->swizzle
= element
->swizzle
;
593 builtin_variable_generator::add_const(const char *name
, int value
)
595 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
597 var
->constant_value
= new(var
) ir_constant(value
);
598 var
->constant_initializer
= new(var
) ir_constant(value
);
599 var
->data
.has_initializer
= true;
605 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
608 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
610 ir_constant_data data
;
611 memset(&data
, 0, sizeof(data
));
615 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
616 var
->constant_initializer
=
617 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
618 var
->data
.has_initializer
= true;
624 builtin_variable_generator::generate_constants()
626 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
627 add_const("gl_MaxVertexTextureImageUnits",
628 state
->Const
.MaxVertexTextureImageUnits
);
629 add_const("gl_MaxCombinedTextureImageUnits",
630 state
->Const
.MaxCombinedTextureImageUnits
);
631 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
632 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
634 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
635 * GL counts them in units of "components" or "floats".
637 if (state
->is_version(410, 100)) {
638 add_const("gl_MaxVertexUniformVectors",
639 state
->Const
.MaxVertexUniformComponents
/ 4);
640 add_const("gl_MaxFragmentUniformVectors",
641 state
->Const
.MaxFragmentUniformComponents
/ 4);
643 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
644 * vertex and fragment shader constants.
646 if (state
->is_version(0, 300)) {
647 add_const("gl_MaxVertexOutputVectors",
648 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
649 add_const("gl_MaxFragmentInputVectors",
650 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
652 add_const("gl_MaxVaryingVectors",
653 state
->ctx
->Const
.MaxVarying
);
656 /* EXT_blend_func_extended brings a built in constant
657 * for determining number of dual source draw buffers
659 if (state
->EXT_blend_func_extended_enable
) {
660 add_const("gl_MaxDualSourceDrawBuffersEXT",
661 state
->Const
.MaxDualSourceDrawBuffers
);
664 add_const("gl_MaxVertexUniformComponents",
665 state
->Const
.MaxVertexUniformComponents
);
667 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
670 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
672 add_const("gl_MaxFragmentUniformComponents",
673 state
->Const
.MaxFragmentUniformComponents
);
676 /* Texel offsets were introduced in ARB_shading_language_420pack (which
677 * requires desktop GLSL version 130), and adopted into desktop GLSL
678 * version 4.20 and GLSL ES version 3.00.
680 if ((state
->is_version(130, 0) &&
681 state
->ARB_shading_language_420pack_enable
) ||
682 state
->is_version(420, 300)) {
683 add_const("gl_MinProgramTexelOffset",
684 state
->Const
.MinProgramTexelOffset
);
685 add_const("gl_MaxProgramTexelOffset",
686 state
->Const
.MaxProgramTexelOffset
);
689 if (state
->has_clip_distance()) {
690 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
692 if (state
->is_version(130, 0)) {
693 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
695 if (state
->has_cull_distance()) {
696 add_const("gl_MaxCullDistances", state
->Const
.MaxClipPlanes
);
697 add_const("gl_MaxCombinedClipAndCullDistances",
698 state
->Const
.MaxClipPlanes
);
701 if (state
->has_geometry_shader()) {
702 add_const("gl_MaxVertexOutputComponents",
703 state
->Const
.MaxVertexOutputComponents
);
704 add_const("gl_MaxGeometryInputComponents",
705 state
->Const
.MaxGeometryInputComponents
);
706 add_const("gl_MaxGeometryOutputComponents",
707 state
->Const
.MaxGeometryOutputComponents
);
708 add_const("gl_MaxFragmentInputComponents",
709 state
->Const
.MaxFragmentInputComponents
);
710 add_const("gl_MaxGeometryTextureImageUnits",
711 state
->Const
.MaxGeometryTextureImageUnits
);
712 add_const("gl_MaxGeometryOutputVertices",
713 state
->Const
.MaxGeometryOutputVertices
);
714 add_const("gl_MaxGeometryTotalOutputComponents",
715 state
->Const
.MaxGeometryTotalOutputComponents
);
716 add_const("gl_MaxGeometryUniformComponents",
717 state
->Const
.MaxGeometryUniformComponents
);
719 /* Note: the GLSL 1.50-4.40 specs require
720 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
721 * But they do not define what it means (and there does not appear to be
722 * any corresponding constant in the GL specs). However,
723 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
724 * be the maximum number of components available for use as geometry
725 * outputs. So we assume this is a synonym for
726 * gl_MaxGeometryOutputComponents.
728 add_const("gl_MaxGeometryVaryingComponents",
729 state
->Const
.MaxGeometryOutputComponents
);
733 /* Note: gl_MaxLights stopped being listed as an explicit constant in
734 * GLSL 1.30, however it continues to be referred to (as a minimum size
735 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
736 * this seems like it was probably an oversight.
738 add_const("gl_MaxLights", state
->Const
.MaxLights
);
740 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
742 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
743 * 1.50, however this seems like it was probably an oversight.
745 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
747 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
748 * re-introduced in GLSL 1.50, so this seems like it was probably an
751 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
754 if (state
->has_atomic_counters()) {
755 add_const("gl_MaxVertexAtomicCounters",
756 state
->Const
.MaxVertexAtomicCounters
);
757 add_const("gl_MaxFragmentAtomicCounters",
758 state
->Const
.MaxFragmentAtomicCounters
);
759 add_const("gl_MaxCombinedAtomicCounters",
760 state
->Const
.MaxCombinedAtomicCounters
);
761 add_const("gl_MaxAtomicCounterBindings",
762 state
->Const
.MaxAtomicBufferBindings
);
764 if (state
->has_geometry_shader()) {
765 add_const("gl_MaxGeometryAtomicCounters",
766 state
->Const
.MaxGeometryAtomicCounters
);
768 if (state
->is_version(110, 320)) {
769 add_const("gl_MaxTessControlAtomicCounters",
770 state
->Const
.MaxTessControlAtomicCounters
);
771 add_const("gl_MaxTessEvaluationAtomicCounters",
772 state
->Const
.MaxTessEvaluationAtomicCounters
);
776 if (state
->is_version(420, 310)) {
777 add_const("gl_MaxVertexAtomicCounterBuffers",
778 state
->Const
.MaxVertexAtomicCounterBuffers
);
779 add_const("gl_MaxFragmentAtomicCounterBuffers",
780 state
->Const
.MaxFragmentAtomicCounterBuffers
);
781 add_const("gl_MaxCombinedAtomicCounterBuffers",
782 state
->Const
.MaxCombinedAtomicCounterBuffers
);
783 add_const("gl_MaxAtomicCounterBufferSize",
784 state
->Const
.MaxAtomicCounterBufferSize
);
786 if (state
->has_geometry_shader()) {
787 add_const("gl_MaxGeometryAtomicCounterBuffers",
788 state
->Const
.MaxGeometryAtomicCounterBuffers
);
790 if (state
->is_version(110, 320)) {
791 add_const("gl_MaxTessControlAtomicCounterBuffers",
792 state
->Const
.MaxTessControlAtomicCounterBuffers
);
793 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
794 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
798 if (state
->is_version(430, 310) || state
->ARB_compute_shader_enable
) {
799 add_const("gl_MaxComputeAtomicCounterBuffers",
800 state
->Const
.MaxComputeAtomicCounterBuffers
);
801 add_const("gl_MaxComputeAtomicCounters",
802 state
->Const
.MaxComputeAtomicCounters
);
803 add_const("gl_MaxComputeImageUniforms",
804 state
->Const
.MaxComputeImageUniforms
);
805 add_const("gl_MaxComputeTextureImageUnits",
806 state
->Const
.MaxComputeTextureImageUnits
);
807 add_const("gl_MaxComputeUniformComponents",
808 state
->Const
.MaxComputeUniformComponents
);
810 add_const_ivec3("gl_MaxComputeWorkGroupCount",
811 state
->Const
.MaxComputeWorkGroupCount
[0],
812 state
->Const
.MaxComputeWorkGroupCount
[1],
813 state
->Const
.MaxComputeWorkGroupCount
[2]);
814 add_const_ivec3("gl_MaxComputeWorkGroupSize",
815 state
->Const
.MaxComputeWorkGroupSize
[0],
816 state
->Const
.MaxComputeWorkGroupSize
[1],
817 state
->Const
.MaxComputeWorkGroupSize
[2]);
819 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
821 * The built-in constant gl_WorkGroupSize is a compute-shader
822 * constant containing the local work-group size of the shader. The
823 * size of the work group in the X, Y, and Z dimensions is stored in
824 * the x, y, and z components. The constants values in
825 * gl_WorkGroupSize will match those specified in the required
826 * local_size_x, local_size_y, and local_size_z layout qualifiers
827 * for the current shader. This is a constant so that it can be
828 * used to size arrays of memory that can be shared within the local
829 * work group. It is a compile-time error to use gl_WorkGroupSize
830 * in a shader that does not declare a fixed local group size, or
831 * before that shader has declared a fixed local group size, using
832 * local_size_x, local_size_y, and local_size_z.
834 * To prevent the shader from trying to refer to gl_WorkGroupSize before
835 * the layout declaration, we don't define it here. Intead we define it
836 * in ast_cs_input_layout::hir().
840 if (state
->has_enhanced_layouts()) {
841 add_const("gl_MaxTransformFeedbackBuffers",
842 state
->Const
.MaxTransformFeedbackBuffers
);
843 add_const("gl_MaxTransformFeedbackInterleavedComponents",
844 state
->Const
.MaxTransformFeedbackInterleavedComponents
);
847 if (state
->has_shader_image_load_store()) {
848 add_const("gl_MaxImageUnits",
849 state
->Const
.MaxImageUnits
);
850 add_const("gl_MaxVertexImageUniforms",
851 state
->Const
.MaxVertexImageUniforms
);
852 add_const("gl_MaxFragmentImageUniforms",
853 state
->Const
.MaxFragmentImageUniforms
);
854 add_const("gl_MaxCombinedImageUniforms",
855 state
->Const
.MaxCombinedImageUniforms
);
857 if (state
->has_geometry_shader()) {
858 add_const("gl_MaxGeometryImageUniforms",
859 state
->Const
.MaxGeometryImageUniforms
);
862 if (!state
->es_shader
) {
863 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
864 state
->Const
.MaxCombinedShaderOutputResources
);
865 add_const("gl_MaxImageSamples",
866 state
->Const
.MaxImageSamples
);
869 if (state
->has_tessellation_shader()) {
870 add_const("gl_MaxTessControlImageUniforms",
871 state
->Const
.MaxTessControlImageUniforms
);
872 add_const("gl_MaxTessEvaluationImageUniforms",
873 state
->Const
.MaxTessEvaluationImageUniforms
);
877 if (state
->is_version(440, 310) ||
878 state
->ARB_ES3_1_compatibility_enable
) {
879 add_const("gl_MaxCombinedShaderOutputResources",
880 state
->Const
.MaxCombinedShaderOutputResources
);
883 if (state
->is_version(410, 0) ||
884 state
->ARB_viewport_array_enable
||
885 state
->OES_viewport_array_enable
)
886 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
888 if (state
->has_tessellation_shader()) {
889 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
890 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
891 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
892 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
893 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
894 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
895 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
896 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
897 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
898 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
899 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
900 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
903 if (state
->is_version(450, 320) ||
904 state
->OES_sample_variables_enable
||
905 state
->ARB_ES3_1_compatibility_enable
)
906 add_const("gl_MaxSamples", state
->Const
.MaxSamples
);
911 * Generate uniform variables (which exist in all types of shaders).
914 builtin_variable_generator::generate_uniforms()
916 if (state
->is_version(400, 320) ||
917 state
->ARB_sample_shading_enable
||
918 state
->OES_sample_variables_enable
)
919 add_uniform(int_t
, "gl_NumSamples");
920 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
921 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
922 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
925 add_uniform(mat4_t
, "gl_ModelViewMatrix");
926 add_uniform(mat4_t
, "gl_ProjectionMatrix");
927 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
928 add_uniform(mat3_t
, "gl_NormalMatrix");
929 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
930 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
931 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
932 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
933 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
934 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
935 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
936 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
937 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
938 add_uniform(float_t
, "gl_NormalScale");
939 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
940 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
942 const glsl_type
*const mat4_array_type
=
943 array(mat4_t
, state
->Const
.MaxTextureCoords
);
944 add_uniform(mat4_array_type
, "gl_TextureMatrix");
945 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
946 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
947 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
949 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
950 add_uniform(type("gl_PointParameters"), "gl_Point");
952 const glsl_type
*const material_parameters_type
=
953 type("gl_MaterialParameters");
954 add_uniform(material_parameters_type
, "gl_FrontMaterial");
955 add_uniform(material_parameters_type
, "gl_BackMaterial");
957 add_uniform(array(type("gl_LightSourceParameters"),
958 state
->Const
.MaxLights
),
961 const glsl_type
*const light_model_products_type
=
962 type("gl_LightModelProducts");
963 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
964 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
966 const glsl_type
*const light_products_type
=
967 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
968 add_uniform(light_products_type
, "gl_FrontLightProduct");
969 add_uniform(light_products_type
, "gl_BackLightProduct");
971 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
972 "gl_TextureEnvColor");
974 const glsl_type
*const texcoords_vec4
=
975 array(vec4_t
, state
->Const
.MaxTextureCoords
);
976 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
977 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
978 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
979 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
980 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
981 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
982 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
983 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
985 add_uniform(type("gl_FogParameters"), "gl_Fog");
991 * Generate special variables which exist in all shaders.
994 builtin_variable_generator::generate_special_vars()
996 if (state
->ARB_shader_ballot_enable
) {
997 add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE
, uint_t
, "gl_SubGroupSizeARB");
998 add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION
, uint_t
, "gl_SubGroupInvocationARB");
999 add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK
, uint64_t, "gl_SubGroupEqMaskARB");
1000 add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK
, uint64_t, "gl_SubGroupGeMaskARB");
1001 add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK
, uint64_t, "gl_SubGroupGtMaskARB");
1002 add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK
, uint64_t, "gl_SubGroupLeMaskARB");
1003 add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK
, uint64_t, "gl_SubGroupLtMaskARB");
1009 * Generate variables which only exist in vertex shaders.
1012 builtin_variable_generator::generate_vs_special_vars()
1016 if (state
->is_version(130, 300))
1017 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
1018 if (state
->ARB_draw_instanced_enable
)
1019 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
1020 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
1021 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
1022 if (state
->ARB_shader_draw_parameters_enable
) {
1023 add_system_value(SYSTEM_VALUE_BASE_VERTEX
, int_t
, "gl_BaseVertexARB");
1024 add_system_value(SYSTEM_VALUE_BASE_INSTANCE
, int_t
, "gl_BaseInstanceARB");
1025 add_system_value(SYSTEM_VALUE_DRAW_ID
, int_t
, "gl_DrawIDARB");
1027 if (state
->AMD_vertex_shader_layer_enable
||
1028 state
->ARB_shader_viewport_layer_array_enable
) {
1029 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1030 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1032 if (state
->AMD_vertex_shader_viewport_index_enable
||
1033 state
->ARB_shader_viewport_layer_array_enable
) {
1034 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1035 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1037 if (compatibility
) {
1038 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
1039 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
1040 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
1041 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
1042 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
1043 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
1044 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
1045 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
1046 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
1047 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
1048 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
1049 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
1050 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
1056 * Generate variables which only exist in tessellation control shaders.
1059 builtin_variable_generator::generate_tcs_special_vars()
1061 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1062 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1064 if (state
->ctx
->Const
.LowerTCSPatchVerticesIn
) {
1065 add_uniform(int_t
, "gl_PatchVerticesIn");
1067 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1070 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1071 "gl_TessLevelOuter")->data
.patch
= 1;
1072 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1073 "gl_TessLevelInner")->data
.patch
= 1;
1074 /* XXX What to do if multiple are flipped on? */
1075 int bbox_slot
= state
->ctx
->Const
.NoPrimitiveBoundingBoxOutput
? -1 :
1076 VARYING_SLOT_BOUNDING_BOX0
;
1077 if (state
->EXT_primitive_bounding_box_enable
)
1078 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxEXT")
1080 if (state
->OES_primitive_bounding_box_enable
)
1081 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxOES")
1083 if (state
->is_version(0, 320) || state
->ARB_ES3_2_compatibility_enable
)
1084 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBox")
1090 * Generate variables which only exist in tessellation evaluation shaders.
1093 builtin_variable_generator::generate_tes_special_vars()
1097 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1098 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1099 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
1100 if (this->state
->ctx
->Const
.GLSLTessLevelsAsInputs
) {
1101 add_input(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1102 "gl_TessLevelOuter")->data
.patch
= 1;
1103 add_input(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1104 "gl_TessLevelInner")->data
.patch
= 1;
1106 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
1107 "gl_TessLevelOuter");
1108 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
1109 "gl_TessLevelInner");
1111 if (state
->ARB_shader_viewport_layer_array_enable
) {
1112 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1113 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1114 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1115 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1121 * Generate variables which only exist in geometry shaders.
1124 builtin_variable_generator::generate_gs_special_vars()
1128 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1129 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1130 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
||
1131 state
->OES_viewport_array_enable
) {
1132 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1133 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1135 if (state
->is_version(400, 320) || state
->ARB_gpu_shader5_enable
||
1136 state
->OES_geometry_shader_enable
|| state
->EXT_geometry_shader_enable
) {
1137 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1140 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1141 * evaluation shaders, it has a different function there than it has in
1142 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1143 * as special geometry shader variables.
1145 * Note that although the general convention of suffixing geometry shader
1146 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1147 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1148 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1150 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
1151 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1152 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1153 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1158 * Generate variables which only exist in fragment shaders.
1161 builtin_variable_generator::generate_fs_special_vars()
1165 if (this->state
->ctx
->Const
.GLSLFragCoordIsSysVal
)
1166 add_system_value(SYSTEM_VALUE_FRAG_COORD
, vec4_t
, "gl_FragCoord");
1168 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
1170 if (this->state
->ctx
->Const
.GLSLFrontFacingIsSysVal
)
1171 add_system_value(SYSTEM_VALUE_FRONT_FACE
, bool_t
, "gl_FrontFacing");
1173 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
1175 if (state
->is_version(120, 100))
1176 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
1178 if (state
->has_geometry_shader()) {
1179 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1180 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1183 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1184 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1185 * They were removed from GLSL ES 3.00.
1187 if (compatibility
|| !state
->is_version(420, 300)) {
1188 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1189 add_output(FRAG_RESULT_DATA0
,
1190 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1193 if (state
->has_framebuffer_fetch() && !state
->is_version(130, 300)) {
1194 ir_variable
*const var
=
1195 add_output(FRAG_RESULT_DATA0
,
1196 array(vec4_t
, state
->Const
.MaxDrawBuffers
),
1198 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
1199 var
->data
.read_only
= 1;
1200 var
->data
.fb_fetch_output
= 1;
1203 if (state
->es_shader
&& state
->language_version
== 100 && state
->EXT_blend_func_extended_enable
) {
1204 add_index_output(FRAG_RESULT_COLOR
, 1, vec4_t
,
1205 "gl_SecondaryFragColorEXT");
1206 add_index_output(FRAG_RESULT_DATA0
, 1,
1207 array(vec4_t
, state
->Const
.MaxDualSourceDrawBuffers
),
1208 "gl_SecondaryFragDataEXT");
1211 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1214 if (state
->is_version(110, 300))
1215 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1217 if (state
->EXT_frag_depth_enable
)
1218 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepthEXT");
1220 if (state
->ARB_shader_stencil_export_enable
) {
1221 ir_variable
*const var
=
1222 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1223 if (state
->ARB_shader_stencil_export_warn
)
1224 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1227 if (state
->AMD_shader_stencil_export_enable
) {
1228 ir_variable
*const var
=
1229 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1230 if (state
->AMD_shader_stencil_export_warn
)
1231 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1234 if (state
->is_version(400, 320) ||
1235 state
->ARB_sample_shading_enable
||
1236 state
->OES_sample_variables_enable
) {
1237 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1238 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1239 /* From the ARB_sample_shading specification:
1240 * "The number of elements in the array is ceil(<s>/32), where
1241 * <s> is the maximum number of color samples supported by the
1243 * Since no drivers expose more than 32x MSAA, we can simply set
1244 * the array size to 1 rather than computing it.
1246 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1249 if (state
->is_version(400, 320) ||
1250 state
->ARB_gpu_shader5_enable
||
1251 state
->OES_sample_variables_enable
) {
1252 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1255 if (state
->is_version(430, 320) ||
1256 state
->ARB_fragment_layer_viewport_enable
||
1257 state
->OES_geometry_shader_enable
||
1258 state
->EXT_geometry_shader_enable
) {
1259 var
= add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1260 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1263 if (state
->is_version(430, 0) ||
1264 state
->ARB_fragment_layer_viewport_enable
||
1265 state
->OES_viewport_array_enable
) {
1266 var
= add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1267 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1270 if (state
->is_version(450, 310) || state
->ARB_ES3_1_compatibility_enable
)
1271 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION
, bool_t
, "gl_HelperInvocation");
1276 * Generate variables which only exist in compute shaders.
1279 builtin_variable_generator::generate_cs_special_vars()
1281 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1282 "gl_LocalInvocationID");
1283 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1284 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS
, uvec3_t
, "gl_NumWorkGroups");
1286 if (state
->ARB_compute_variable_group_size_enable
) {
1287 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE
,
1288 uvec3_t
, "gl_LocalGroupSizeARB");
1291 if (state
->ctx
->Const
.LowerCsDerivedVariables
) {
1292 add_variable("gl_GlobalInvocationID", uvec3_t
, ir_var_auto
, 0);
1293 add_variable("gl_LocalInvocationIndex", uint_t
, ir_var_auto
, 0);
1295 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID
,
1296 uvec3_t
, "gl_GlobalInvocationID");
1297 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
,
1298 uint_t
, "gl_LocalInvocationIndex");
1304 * Add a single "varying" variable. The variable's type and direction (input
1305 * or output) are adjusted as appropriate for the type of shader being
1309 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1312 switch (state
->stage
) {
1313 case MESA_SHADER_TESS_CTRL
:
1314 case MESA_SHADER_TESS_EVAL
:
1315 case MESA_SHADER_GEOMETRY
:
1316 this->per_vertex_in
.add_field(slot
, type
, name
);
1318 case MESA_SHADER_VERTEX
:
1319 this->per_vertex_out
.add_field(slot
, type
, name
);
1321 case MESA_SHADER_FRAGMENT
:
1322 add_input(slot
, type
, name
);
1324 case MESA_SHADER_COMPUTE
:
1325 /* Compute shaders don't have varyings. */
1334 * Generate variables that are used to communicate data from one shader stage
1335 * to the next ("varyings").
1338 builtin_variable_generator::generate_varyings()
1340 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1341 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1342 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1343 if (!state
->es_shader
||
1344 state
->stage
== MESA_SHADER_VERTEX
||
1345 (state
->stage
== MESA_SHADER_GEOMETRY
&&
1346 (state
->OES_geometry_point_size_enable
||
1347 state
->EXT_geometry_point_size_enable
)) ||
1348 ((state
->stage
== MESA_SHADER_TESS_CTRL
||
1349 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
1350 (state
->OES_tessellation_point_size_enable
||
1351 state
->EXT_tessellation_point_size_enable
))) {
1352 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1356 if (state
->has_clip_distance()) {
1357 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1360 if (state
->has_cull_distance()) {
1361 add_varying(VARYING_SLOT_CULL_DIST0
, array(float_t
, 0),
1365 if (compatibility
) {
1366 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1367 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1368 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1369 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1370 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1372 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1373 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1374 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1375 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1376 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1380 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1383 * "In the tessellation control language, built-in variables are
1384 * intrinsically declared as:
1388 * float gl_PointSize;
1389 * float gl_ClipDistance[];
1390 * } gl_in[gl_MaxPatchVertices];"
1392 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1393 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1394 const glsl_type
*per_vertex_in_type
=
1395 this->per_vertex_in
.construct_interface_instance();
1396 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1397 ir_var_shader_in
, -1);
1399 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1400 const glsl_type
*per_vertex_in_type
=
1401 this->per_vertex_in
.construct_interface_instance();
1402 add_variable("gl_in", array(per_vertex_in_type
, 0),
1403 ir_var_shader_in
, -1);
1405 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1406 const glsl_type
*per_vertex_out_type
=
1407 this->per_vertex_out
.construct_interface_instance();
1408 add_variable("gl_out", array(per_vertex_out_type
, 0),
1409 ir_var_shader_out
, -1);
1411 if (state
->stage
== MESA_SHADER_VERTEX
||
1412 state
->stage
== MESA_SHADER_TESS_EVAL
||
1413 state
->stage
== MESA_SHADER_GEOMETRY
) {
1414 const glsl_type
*per_vertex_out_type
=
1415 this->per_vertex_out
.construct_interface_instance();
1416 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1417 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1419 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1420 fields
[i
].location
);
1421 var
->data
.interpolation
= fields
[i
].interpolation
;
1422 var
->data
.centroid
= fields
[i
].centroid
;
1423 var
->data
.sample
= fields
[i
].sample
;
1424 var
->data
.patch
= fields
[i
].patch
;
1425 var
->data
.precision
= fields
[i
].precision
;
1426 var
->init_interface_type(per_vertex_out_type
);
1432 }; /* Anonymous namespace */
1436 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1437 struct _mesa_glsl_parse_state
*state
)
1439 builtin_variable_generator
gen(instructions
, state
);
1441 gen
.generate_constants();
1442 gen
.generate_uniforms();
1443 gen
.generate_special_vars();
1445 gen
.generate_varyings();
1447 switch (state
->stage
) {
1448 case MESA_SHADER_VERTEX
:
1449 gen
.generate_vs_special_vars();
1451 case MESA_SHADER_TESS_CTRL
:
1452 gen
.generate_tcs_special_vars();
1454 case MESA_SHADER_TESS_EVAL
:
1455 gen
.generate_tes_special_vars();
1457 case MESA_SHADER_GEOMETRY
:
1458 gen
.generate_gs_special_vars();
1460 case MESA_SHADER_FRAGMENT
:
1461 gen
.generate_fs_special_vars();
1463 case MESA_SHADER_COMPUTE
:
1464 gen
.generate_cs_special_vars();
1473 * Initialize compute shader variables with values that are derived from other
1474 * compute shader variable.
1477 initialize_cs_derived_variables(gl_shader
*shader
,
1478 ir_function_signature
*const main_sig
)
1480 assert(shader
->Stage
== MESA_SHADER_COMPUTE
);
1482 ir_variable
*gl_GlobalInvocationID
=
1483 shader
->symbols
->get_variable("gl_GlobalInvocationID");
1484 assert(gl_GlobalInvocationID
);
1485 ir_variable
*gl_WorkGroupID
=
1486 shader
->symbols
->get_variable("gl_WorkGroupID");
1487 assert(gl_WorkGroupID
);
1488 ir_variable
*gl_WorkGroupSize
=
1489 shader
->symbols
->get_variable("gl_WorkGroupSize");
1490 if (gl_WorkGroupSize
== NULL
) {
1491 void *const mem_ctx
= ralloc_parent(shader
->ir
);
1492 gl_WorkGroupSize
= new(mem_ctx
) ir_variable(glsl_type::uvec3_type
,
1495 gl_WorkGroupSize
->data
.how_declared
= ir_var_declared_implicitly
;
1496 gl_WorkGroupSize
->data
.read_only
= true;
1497 shader
->ir
->push_head(gl_WorkGroupSize
);
1499 ir_variable
*gl_LocalInvocationID
=
1500 shader
->symbols
->get_variable("gl_LocalInvocationID");
1501 assert(gl_LocalInvocationID
);
1503 /* gl_GlobalInvocationID =
1504 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1506 ir_instruction
*inst
=
1507 assign(gl_GlobalInvocationID
,
1508 add(mul(gl_WorkGroupID
, gl_WorkGroupSize
),
1509 gl_LocalInvocationID
));
1510 main_sig
->body
.push_head(inst
);
1512 /* gl_LocalInvocationIndex =
1513 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1514 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1515 * gl_LocalInvocationID.x;
1517 ir_expression
*index_z
=
1518 mul(mul(swizzle_z(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
)),
1519 swizzle_y(gl_WorkGroupSize
));
1520 ir_expression
*index_y
=
1521 mul(swizzle_y(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
));
1522 ir_expression
*index_y_plus_z
= add(index_y
, index_z
);
1523 operand
index_x(swizzle_x(gl_LocalInvocationID
));
1524 ir_expression
*index_x_plus_y_plus_z
= add(index_y_plus_z
, index_x
);
1525 ir_variable
*gl_LocalInvocationIndex
=
1526 shader
->symbols
->get_variable("gl_LocalInvocationIndex");
1527 assert(gl_LocalInvocationIndex
);
1528 inst
= assign(gl_LocalInvocationIndex
, index_x_plus_y_plus_z
);
1529 main_sig
->body
.push_head(inst
);
1534 * Initialize builtin variables with values based on other builtin variables.
1535 * These are initialized in the main function.
1538 _mesa_glsl_initialize_derived_variables(struct gl_context
*ctx
,
1541 /* We only need to set CS variables currently. */
1542 if (shader
->Stage
== MESA_SHADER_COMPUTE
&&
1543 ctx
->Const
.LowerCsDerivedVariables
) {
1544 ir_function_signature
*const main_sig
=
1545 _mesa_get_main_function_signature(shader
->symbols
);
1547 if (main_sig
!= NULL
)
1548 initialize_cs_derived_variables(shader
, main_sig
);