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"
34 using namespace ir_builder
;
36 static const struct gl_builtin_uniform_element gl_NumSamples_elements
[] = {
37 {NULL
, {STATE_NUM_SAMPLES
, 0, 0}, SWIZZLE_XXXX
}
41 static const struct gl_builtin_uniform_element gl_PatchVerticesIn_elements
[] = {
42 {NULL
, {STATE_INTERNAL
, STATE_TCS_PATCH_VERTICES_IN
}, SWIZZLE_XXXX
}
45 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
46 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
47 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
48 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
51 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
52 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
55 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
56 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
57 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
58 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
59 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
60 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
61 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
62 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
65 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
66 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
67 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
68 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
69 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
70 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
73 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
74 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
75 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
76 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
77 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
78 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
81 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
82 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
83 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
84 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
85 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
86 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
87 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
88 MAKE_SWIZZLE4(SWIZZLE_X
,
92 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
93 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
94 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
95 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
96 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
97 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
100 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
101 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
104 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
105 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
108 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
109 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
112 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
113 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
114 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
115 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
118 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
119 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
120 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
121 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
124 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
125 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
128 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
129 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
132 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
133 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
136 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
137 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
140 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
141 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
144 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
145 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
148 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
149 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
152 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
153 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
156 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
157 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
160 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
161 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
162 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
163 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
164 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
165 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
168 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
169 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
172 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
173 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
176 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
177 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
180 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
181 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
184 #define MATRIX(name, statevar, modifier) \
185 static const struct gl_builtin_uniform_element name ## _elements[] = { \
186 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
187 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
188 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
189 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
192 MATRIX(gl_ModelViewMatrix
,
193 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
194 MATRIX(gl_ModelViewMatrixInverse
,
195 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
196 MATRIX(gl_ModelViewMatrixTranspose
,
197 STATE_MODELVIEW_MATRIX
, 0);
198 MATRIX(gl_ModelViewMatrixInverseTranspose
,
199 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
201 MATRIX(gl_ProjectionMatrix
,
202 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
203 MATRIX(gl_ProjectionMatrixInverse
,
204 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
205 MATRIX(gl_ProjectionMatrixTranspose
,
206 STATE_PROJECTION_MATRIX
, 0);
207 MATRIX(gl_ProjectionMatrixInverseTranspose
,
208 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
210 MATRIX(gl_ModelViewProjectionMatrix
,
211 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
212 MATRIX(gl_ModelViewProjectionMatrixInverse
,
213 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
214 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
215 STATE_MVP_MATRIX
, 0);
216 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
217 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
219 MATRIX(gl_TextureMatrix
,
220 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
221 MATRIX(gl_TextureMatrixInverse
,
222 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
223 MATRIX(gl_TextureMatrixTranspose
,
224 STATE_TEXTURE_MATRIX
, 0);
225 MATRIX(gl_TextureMatrixInverseTranspose
,
226 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
228 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
229 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
230 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
231 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
232 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
233 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
234 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
239 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
241 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
242 STATEVAR(gl_PatchVerticesIn
),
243 STATEVAR(gl_NumSamples
),
244 STATEVAR(gl_DepthRange
),
245 STATEVAR(gl_ClipPlane
),
247 STATEVAR(gl_FrontMaterial
),
248 STATEVAR(gl_BackMaterial
),
249 STATEVAR(gl_LightSource
),
250 STATEVAR(gl_LightModel
),
251 STATEVAR(gl_FrontLightModelProduct
),
252 STATEVAR(gl_BackLightModelProduct
),
253 STATEVAR(gl_FrontLightProduct
),
254 STATEVAR(gl_BackLightProduct
),
255 STATEVAR(gl_TextureEnvColor
),
256 STATEVAR(gl_EyePlaneS
),
257 STATEVAR(gl_EyePlaneT
),
258 STATEVAR(gl_EyePlaneR
),
259 STATEVAR(gl_EyePlaneQ
),
260 STATEVAR(gl_ObjectPlaneS
),
261 STATEVAR(gl_ObjectPlaneT
),
262 STATEVAR(gl_ObjectPlaneR
),
263 STATEVAR(gl_ObjectPlaneQ
),
266 STATEVAR(gl_ModelViewMatrix
),
267 STATEVAR(gl_ModelViewMatrixInverse
),
268 STATEVAR(gl_ModelViewMatrixTranspose
),
269 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
271 STATEVAR(gl_ProjectionMatrix
),
272 STATEVAR(gl_ProjectionMatrixInverse
),
273 STATEVAR(gl_ProjectionMatrixTranspose
),
274 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
276 STATEVAR(gl_ModelViewProjectionMatrix
),
277 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
278 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
279 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
281 STATEVAR(gl_TextureMatrix
),
282 STATEVAR(gl_TextureMatrixInverse
),
283 STATEVAR(gl_TextureMatrixTranspose
),
284 STATEVAR(gl_TextureMatrixInverseTranspose
),
286 STATEVAR(gl_NormalMatrix
),
287 STATEVAR(gl_NormalScale
),
289 STATEVAR(gl_FogParamsOptimizedMESA
),
290 STATEVAR(gl_CurrentAttribVertMESA
),
291 STATEVAR(gl_CurrentAttribFragMESA
),
300 * Data structure that accumulates fields for the gl_PerVertex interface
303 class per_vertex_accumulator
306 per_vertex_accumulator();
307 void add_field(int slot
, const glsl_type
*type
, const char *name
);
308 const glsl_type
*construct_interface_instance() const;
311 glsl_struct_field fields
[11];
316 per_vertex_accumulator::per_vertex_accumulator()
324 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
327 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
328 this->fields
[this->num_fields
].type
= type
;
329 this->fields
[this->num_fields
].name
= name
;
330 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
331 this->fields
[this->num_fields
].location
= slot
;
332 this->fields
[this->num_fields
].offset
= -1;
333 this->fields
[this->num_fields
].interpolation
= INTERP_MODE_NONE
;
334 this->fields
[this->num_fields
].centroid
= 0;
335 this->fields
[this->num_fields
].sample
= 0;
336 this->fields
[this->num_fields
].patch
= 0;
337 this->fields
[this->num_fields
].precision
= GLSL_PRECISION_NONE
;
338 this->fields
[this->num_fields
].image_read_only
= 0;
339 this->fields
[this->num_fields
].image_write_only
= 0;
340 this->fields
[this->num_fields
].image_coherent
= 0;
341 this->fields
[this->num_fields
].image_volatile
= 0;
342 this->fields
[this->num_fields
].image_restrict
= 0;
343 this->fields
[this->num_fields
].explicit_xfb_buffer
= 0;
344 this->fields
[this->num_fields
].xfb_buffer
= -1;
345 this->fields
[this->num_fields
].xfb_stride
= -1;
351 per_vertex_accumulator::construct_interface_instance() const
353 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
354 GLSL_INTERFACE_PACKING_STD140
,
360 class builtin_variable_generator
363 builtin_variable_generator(exec_list
*instructions
,
364 struct _mesa_glsl_parse_state
*state
);
365 void generate_constants();
366 void generate_uniforms();
367 void generate_vs_special_vars();
368 void generate_tcs_special_vars();
369 void generate_tes_special_vars();
370 void generate_gs_special_vars();
371 void generate_fs_special_vars();
372 void generate_cs_special_vars();
373 void generate_varyings();
376 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
378 return glsl_type::get_array_instance(base
, elements
);
381 const glsl_type
*type(const char *name
)
383 return symtab
->get_type(name
);
386 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
388 return add_variable(name
, type
, ir_var_shader_in
, slot
);
391 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
393 return add_variable(name
, type
, ir_var_shader_out
, slot
);
396 ir_variable
*add_index_output(int slot
, int index
, const glsl_type
*type
, const char *name
)
398 return add_index_variable(name
, type
, ir_var_shader_out
, slot
, index
);
401 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
404 return add_variable(name
, type
, ir_var_system_value
, slot
);
407 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
408 enum ir_variable_mode mode
, int slot
);
409 ir_variable
*add_index_variable(const char *name
, const glsl_type
*type
,
410 enum ir_variable_mode mode
, int slot
, int index
);
411 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
412 ir_variable
*add_const(const char *name
, int value
);
413 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
414 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
416 exec_list
* const instructions
;
417 struct _mesa_glsl_parse_state
* const state
;
418 glsl_symbol_table
* const symtab
;
421 * True if compatibility-profile-only variables should be included. (In
422 * desktop GL, these are always included when the GLSL version is 1.30 and
425 const bool compatibility
;
427 const glsl_type
* const bool_t
;
428 const glsl_type
* const int_t
;
429 const glsl_type
* const uint_t
;
430 const glsl_type
* const float_t
;
431 const glsl_type
* const vec2_t
;
432 const glsl_type
* const vec3_t
;
433 const glsl_type
* const vec4_t
;
434 const glsl_type
* const uvec3_t
;
435 const glsl_type
* const mat3_t
;
436 const glsl_type
* const mat4_t
;
438 per_vertex_accumulator per_vertex_in
;
439 per_vertex_accumulator per_vertex_out
;
443 builtin_variable_generator::builtin_variable_generator(
444 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
445 : instructions(instructions
), state(state
), symtab(state
->symbols
),
446 compatibility(!state
->is_version(140, 100)),
447 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
448 uint_t(glsl_type::uint_type
),
449 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
450 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
451 uvec3_t(glsl_type::uvec3_type
),
452 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
457 builtin_variable_generator::add_index_variable(const char *name
,
458 const glsl_type
*type
,
459 enum ir_variable_mode mode
, int slot
, int index
)
461 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
462 var
->data
.how_declared
= ir_var_declared_implicitly
;
464 switch (var
->data
.mode
) {
466 case ir_var_shader_in
:
468 case ir_var_system_value
:
469 var
->data
.read_only
= true;
471 case ir_var_shader_out
:
472 case ir_var_shader_storage
:
475 /* The only variables that are added using this function should be
476 * uniforms, shader storage, shader inputs, and shader outputs, constants
477 * (which use ir_var_auto), and system values.
483 var
->data
.location
= slot
;
484 var
->data
.explicit_location
= (slot
>= 0);
485 var
->data
.explicit_index
= 1;
486 var
->data
.index
= index
;
488 /* Once the variable is created an initialized, add it to the symbol table
489 * and add the declaration to the IR stream.
491 instructions
->push_tail(var
);
493 symtab
->add_variable(var
);
498 builtin_variable_generator::add_variable(const char *name
,
499 const glsl_type
*type
,
500 enum ir_variable_mode mode
, int slot
)
502 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
503 var
->data
.how_declared
= ir_var_declared_implicitly
;
505 switch (var
->data
.mode
) {
507 case ir_var_shader_in
:
509 case ir_var_system_value
:
510 var
->data
.read_only
= true;
512 case ir_var_shader_out
:
513 case ir_var_shader_storage
:
516 /* The only variables that are added using this function should be
517 * uniforms, shader storage, shader inputs, and shader outputs, constants
518 * (which use ir_var_auto), and system values.
524 var
->data
.location
= slot
;
525 var
->data
.explicit_location
= (slot
>= 0);
526 var
->data
.explicit_index
= 0;
528 /* Once the variable is created an initialized, add it to the symbol table
529 * and add the declaration to the IR stream.
531 instructions
->push_tail(var
);
533 symtab
->add_variable(var
);
537 extern "C" const struct gl_builtin_uniform_desc
*
538 _mesa_glsl_get_builtin_uniform_desc(const char *name
)
540 for (unsigned i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
541 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
542 return &_mesa_builtin_uniform_desc
[i
];
549 builtin_variable_generator::add_uniform(const glsl_type
*type
,
552 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
554 const struct gl_builtin_uniform_desc
* const statevar
=
555 _mesa_glsl_get_builtin_uniform_desc(name
);
556 assert(statevar
!= NULL
);
558 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
560 ir_state_slot
*slots
=
561 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
563 for (unsigned a
= 0; a
< array_count
; a
++) {
564 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
565 const struct gl_builtin_uniform_element
*element
=
566 &statevar
->elements
[j
];
568 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
569 if (type
->is_array()) {
570 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
571 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
572 slots
->tokens
[2] = a
;
574 slots
->tokens
[1] = a
;
578 slots
->swizzle
= element
->swizzle
;
588 builtin_variable_generator::add_const(const char *name
, int value
)
590 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
592 var
->constant_value
= new(var
) ir_constant(value
);
593 var
->constant_initializer
= new(var
) ir_constant(value
);
594 var
->data
.has_initializer
= true;
600 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
603 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
605 ir_constant_data data
;
606 memset(&data
, 0, sizeof(data
));
610 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
611 var
->constant_initializer
=
612 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
613 var
->data
.has_initializer
= true;
619 builtin_variable_generator::generate_constants()
621 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
622 add_const("gl_MaxVertexTextureImageUnits",
623 state
->Const
.MaxVertexTextureImageUnits
);
624 add_const("gl_MaxCombinedTextureImageUnits",
625 state
->Const
.MaxCombinedTextureImageUnits
);
626 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
627 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
629 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
630 * GL counts them in units of "components" or "floats".
632 if (state
->is_version(410, 100)) {
633 add_const("gl_MaxVertexUniformVectors",
634 state
->Const
.MaxVertexUniformComponents
/ 4);
635 add_const("gl_MaxFragmentUniformVectors",
636 state
->Const
.MaxFragmentUniformComponents
/ 4);
638 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
639 * vertex and fragment shader constants.
641 if (state
->is_version(0, 300)) {
642 add_const("gl_MaxVertexOutputVectors",
643 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
644 add_const("gl_MaxFragmentInputVectors",
645 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
647 add_const("gl_MaxVaryingVectors",
648 state
->ctx
->Const
.MaxVarying
);
651 /* EXT_blend_func_extended brings a built in constant
652 * for determining number of dual source draw buffers
654 if (state
->EXT_blend_func_extended_enable
) {
655 add_const("gl_MaxDualSourceDrawBuffersEXT",
656 state
->Const
.MaxDualSourceDrawBuffers
);
659 add_const("gl_MaxVertexUniformComponents",
660 state
->Const
.MaxVertexUniformComponents
);
662 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
665 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
667 add_const("gl_MaxFragmentUniformComponents",
668 state
->Const
.MaxFragmentUniformComponents
);
671 /* Texel offsets were introduced in ARB_shading_language_420pack (which
672 * requires desktop GLSL version 130), and adopted into desktop GLSL
673 * version 4.20 and GLSL ES version 3.00.
675 if ((state
->is_version(130, 0) &&
676 state
->ARB_shading_language_420pack_enable
) ||
677 state
->is_version(420, 300)) {
678 add_const("gl_MinProgramTexelOffset",
679 state
->Const
.MinProgramTexelOffset
);
680 add_const("gl_MaxProgramTexelOffset",
681 state
->Const
.MaxProgramTexelOffset
);
684 if (state
->has_clip_distance()) {
685 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
687 if (state
->is_version(130, 0)) {
688 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
690 if (state
->has_cull_distance()) {
691 add_const("gl_MaxCullDistances", state
->Const
.MaxClipPlanes
);
692 add_const("gl_MaxCombinedClipAndCullDistances",
693 state
->Const
.MaxClipPlanes
);
696 if (state
->has_geometry_shader()) {
697 add_const("gl_MaxVertexOutputComponents",
698 state
->Const
.MaxVertexOutputComponents
);
699 add_const("gl_MaxGeometryInputComponents",
700 state
->Const
.MaxGeometryInputComponents
);
701 add_const("gl_MaxGeometryOutputComponents",
702 state
->Const
.MaxGeometryOutputComponents
);
703 add_const("gl_MaxFragmentInputComponents",
704 state
->Const
.MaxFragmentInputComponents
);
705 add_const("gl_MaxGeometryTextureImageUnits",
706 state
->Const
.MaxGeometryTextureImageUnits
);
707 add_const("gl_MaxGeometryOutputVertices",
708 state
->Const
.MaxGeometryOutputVertices
);
709 add_const("gl_MaxGeometryTotalOutputComponents",
710 state
->Const
.MaxGeometryTotalOutputComponents
);
711 add_const("gl_MaxGeometryUniformComponents",
712 state
->Const
.MaxGeometryUniformComponents
);
714 /* Note: the GLSL 1.50-4.40 specs require
715 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
716 * But they do not define what it means (and there does not appear to be
717 * any corresponding constant in the GL specs). However,
718 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
719 * be the maximum number of components available for use as geometry
720 * outputs. So we assume this is a synonym for
721 * gl_MaxGeometryOutputComponents.
723 add_const("gl_MaxGeometryVaryingComponents",
724 state
->Const
.MaxGeometryOutputComponents
);
728 /* Note: gl_MaxLights stopped being listed as an explicit constant in
729 * GLSL 1.30, however it continues to be referred to (as a minimum size
730 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
731 * this seems like it was probably an oversight.
733 add_const("gl_MaxLights", state
->Const
.MaxLights
);
735 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
737 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
738 * 1.50, however this seems like it was probably an oversight.
740 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
742 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
743 * re-introduced in GLSL 1.50, so this seems like it was probably an
746 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
749 if (state
->has_atomic_counters()) {
750 add_const("gl_MaxVertexAtomicCounters",
751 state
->Const
.MaxVertexAtomicCounters
);
752 add_const("gl_MaxFragmentAtomicCounters",
753 state
->Const
.MaxFragmentAtomicCounters
);
754 add_const("gl_MaxCombinedAtomicCounters",
755 state
->Const
.MaxCombinedAtomicCounters
);
756 add_const("gl_MaxAtomicCounterBindings",
757 state
->Const
.MaxAtomicBufferBindings
);
759 if (state
->has_geometry_shader()) {
760 add_const("gl_MaxGeometryAtomicCounters",
761 state
->Const
.MaxGeometryAtomicCounters
);
763 if (state
->is_version(110, 320)) {
764 add_const("gl_MaxTessControlAtomicCounters",
765 state
->Const
.MaxTessControlAtomicCounters
);
766 add_const("gl_MaxTessEvaluationAtomicCounters",
767 state
->Const
.MaxTessEvaluationAtomicCounters
);
771 if (state
->is_version(420, 310)) {
772 add_const("gl_MaxVertexAtomicCounterBuffers",
773 state
->Const
.MaxVertexAtomicCounterBuffers
);
774 add_const("gl_MaxFragmentAtomicCounterBuffers",
775 state
->Const
.MaxFragmentAtomicCounterBuffers
);
776 add_const("gl_MaxCombinedAtomicCounterBuffers",
777 state
->Const
.MaxCombinedAtomicCounterBuffers
);
778 add_const("gl_MaxAtomicCounterBufferSize",
779 state
->Const
.MaxAtomicCounterBufferSize
);
781 if (state
->has_geometry_shader()) {
782 add_const("gl_MaxGeometryAtomicCounterBuffers",
783 state
->Const
.MaxGeometryAtomicCounterBuffers
);
785 if (state
->is_version(110, 320)) {
786 add_const("gl_MaxTessControlAtomicCounterBuffers",
787 state
->Const
.MaxTessControlAtomicCounterBuffers
);
788 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
789 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
793 if (state
->is_version(430, 310) || state
->ARB_compute_shader_enable
) {
794 add_const("gl_MaxComputeAtomicCounterBuffers",
795 state
->Const
.MaxComputeAtomicCounterBuffers
);
796 add_const("gl_MaxComputeAtomicCounters",
797 state
->Const
.MaxComputeAtomicCounters
);
798 add_const("gl_MaxComputeImageUniforms",
799 state
->Const
.MaxComputeImageUniforms
);
800 add_const("gl_MaxComputeTextureImageUnits",
801 state
->Const
.MaxComputeTextureImageUnits
);
802 add_const("gl_MaxComputeUniformComponents",
803 state
->Const
.MaxComputeUniformComponents
);
805 add_const_ivec3("gl_MaxComputeWorkGroupCount",
806 state
->Const
.MaxComputeWorkGroupCount
[0],
807 state
->Const
.MaxComputeWorkGroupCount
[1],
808 state
->Const
.MaxComputeWorkGroupCount
[2]);
809 add_const_ivec3("gl_MaxComputeWorkGroupSize",
810 state
->Const
.MaxComputeWorkGroupSize
[0],
811 state
->Const
.MaxComputeWorkGroupSize
[1],
812 state
->Const
.MaxComputeWorkGroupSize
[2]);
814 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
816 * The built-in constant gl_WorkGroupSize is a compute-shader
817 * constant containing the local work-group size of the shader. The
818 * size of the work group in the X, Y, and Z dimensions is stored in
819 * the x, y, and z components. The constants values in
820 * gl_WorkGroupSize will match those specified in the required
821 * local_size_x, local_size_y, and local_size_z layout qualifiers
822 * for the current shader. This is a constant so that it can be
823 * used to size arrays of memory that can be shared within the local
824 * work group. It is a compile-time error to use gl_WorkGroupSize
825 * in a shader that does not declare a fixed local group size, or
826 * before that shader has declared a fixed local group size, using
827 * local_size_x, local_size_y, and local_size_z.
829 * To prevent the shader from trying to refer to gl_WorkGroupSize before
830 * the layout declaration, we don't define it here. Intead we define it
831 * in ast_cs_input_layout::hir().
835 if (state
->has_enhanced_layouts()) {
836 add_const("gl_MaxTransformFeedbackBuffers",
837 state
->Const
.MaxTransformFeedbackBuffers
);
838 add_const("gl_MaxTransformFeedbackInterleavedComponents",
839 state
->Const
.MaxTransformFeedbackInterleavedComponents
);
842 if (state
->is_version(420, 310) ||
843 state
->ARB_shader_image_load_store_enable
) {
844 add_const("gl_MaxImageUnits",
845 state
->Const
.MaxImageUnits
);
846 add_const("gl_MaxVertexImageUniforms",
847 state
->Const
.MaxVertexImageUniforms
);
848 add_const("gl_MaxFragmentImageUniforms",
849 state
->Const
.MaxFragmentImageUniforms
);
850 add_const("gl_MaxCombinedImageUniforms",
851 state
->Const
.MaxCombinedImageUniforms
);
853 if (state
->has_geometry_shader()) {
854 add_const("gl_MaxGeometryImageUniforms",
855 state
->Const
.MaxGeometryImageUniforms
);
858 if (!state
->es_shader
) {
859 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
860 state
->Const
.MaxCombinedShaderOutputResources
);
861 add_const("gl_MaxImageSamples",
862 state
->Const
.MaxImageSamples
);
865 if (state
->has_tessellation_shader()) {
866 add_const("gl_MaxTessControlImageUniforms",
867 state
->Const
.MaxTessControlImageUniforms
);
868 add_const("gl_MaxTessEvaluationImageUniforms",
869 state
->Const
.MaxTessEvaluationImageUniforms
);
873 if (state
->is_version(440, 310) ||
874 state
->ARB_ES3_1_compatibility_enable
) {
875 add_const("gl_MaxCombinedShaderOutputResources",
876 state
->Const
.MaxCombinedShaderOutputResources
);
879 if (state
->is_version(410, 0) ||
880 state
->ARB_viewport_array_enable
||
881 state
->OES_viewport_array_enable
)
882 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
884 if (state
->has_tessellation_shader()) {
885 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
886 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
887 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
888 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
889 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
890 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
891 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
892 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
893 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
894 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
895 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
896 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
899 if (state
->is_version(450, 320) ||
900 state
->OES_sample_variables_enable
||
901 state
->ARB_ES3_1_compatibility_enable
)
902 add_const("gl_MaxSamples", state
->Const
.MaxSamples
);
907 * Generate uniform variables (which exist in all types of shaders).
910 builtin_variable_generator::generate_uniforms()
912 if (state
->is_version(400, 320) ||
913 state
->ARB_sample_shading_enable
||
914 state
->OES_sample_variables_enable
)
915 add_uniform(int_t
, "gl_NumSamples");
916 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
917 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
918 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
921 add_uniform(mat4_t
, "gl_ModelViewMatrix");
922 add_uniform(mat4_t
, "gl_ProjectionMatrix");
923 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
924 add_uniform(mat3_t
, "gl_NormalMatrix");
925 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
926 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
927 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
928 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
929 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
930 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
931 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
932 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
933 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
934 add_uniform(float_t
, "gl_NormalScale");
935 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
936 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
938 const glsl_type
*const mat4_array_type
=
939 array(mat4_t
, state
->Const
.MaxTextureCoords
);
940 add_uniform(mat4_array_type
, "gl_TextureMatrix");
941 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
942 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
943 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
945 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
946 add_uniform(type("gl_PointParameters"), "gl_Point");
948 const glsl_type
*const material_parameters_type
=
949 type("gl_MaterialParameters");
950 add_uniform(material_parameters_type
, "gl_FrontMaterial");
951 add_uniform(material_parameters_type
, "gl_BackMaterial");
953 add_uniform(array(type("gl_LightSourceParameters"),
954 state
->Const
.MaxLights
),
957 const glsl_type
*const light_model_products_type
=
958 type("gl_LightModelProducts");
959 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
960 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
962 const glsl_type
*const light_products_type
=
963 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
964 add_uniform(light_products_type
, "gl_FrontLightProduct");
965 add_uniform(light_products_type
, "gl_BackLightProduct");
967 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
968 "gl_TextureEnvColor");
970 const glsl_type
*const texcoords_vec4
=
971 array(vec4_t
, state
->Const
.MaxTextureCoords
);
972 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
973 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
974 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
975 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
976 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
977 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
978 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
979 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
981 add_uniform(type("gl_FogParameters"), "gl_Fog");
987 * Generate variables which only exist in vertex shaders.
990 builtin_variable_generator::generate_vs_special_vars()
994 if (state
->is_version(130, 300))
995 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
996 if (state
->ARB_draw_instanced_enable
)
997 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
998 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
999 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
1000 if (state
->ARB_shader_draw_parameters_enable
) {
1001 add_system_value(SYSTEM_VALUE_BASE_VERTEX
, int_t
, "gl_BaseVertexARB");
1002 add_system_value(SYSTEM_VALUE_BASE_INSTANCE
, int_t
, "gl_BaseInstanceARB");
1003 add_system_value(SYSTEM_VALUE_DRAW_ID
, int_t
, "gl_DrawIDARB");
1005 if (state
->AMD_vertex_shader_layer_enable
||
1006 state
->ARB_shader_viewport_layer_array_enable
) {
1007 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1008 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1010 if (state
->AMD_vertex_shader_viewport_index_enable
||
1011 state
->ARB_shader_viewport_layer_array_enable
) {
1012 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1013 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1015 if (compatibility
) {
1016 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
1017 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
1018 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
1019 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
1020 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
1021 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
1022 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
1023 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
1024 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
1025 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
1026 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
1027 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
1028 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
1034 * Generate variables which only exist in tessellation control shaders.
1037 builtin_variable_generator::generate_tcs_special_vars()
1039 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1040 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1042 if (state
->ctx
->Const
.LowerTCSPatchVerticesIn
) {
1043 add_uniform(int_t
, "gl_PatchVerticesIn");
1045 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1048 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1049 "gl_TessLevelOuter")->data
.patch
= 1;
1050 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1051 "gl_TessLevelInner")->data
.patch
= 1;
1052 /* XXX What to do if multiple are flipped on? */
1053 int bbox_slot
= state
->ctx
->Const
.NoPrimitiveBoundingBoxOutput
? -1 :
1054 VARYING_SLOT_BOUNDING_BOX0
;
1055 if (state
->EXT_primitive_bounding_box_enable
)
1056 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxEXT")
1058 if (state
->OES_primitive_bounding_box_enable
)
1059 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxOES")
1061 if (state
->is_version(0, 320) || state
->ARB_ES3_2_compatibility_enable
)
1062 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBox")
1068 * Generate variables which only exist in tessellation evaluation shaders.
1071 builtin_variable_generator::generate_tes_special_vars()
1075 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1076 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1077 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
1078 if (this->state
->ctx
->Const
.GLSLTessLevelsAsInputs
) {
1079 add_input(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1080 "gl_TessLevelOuter")->data
.patch
= 1;
1081 add_input(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1082 "gl_TessLevelInner")->data
.patch
= 1;
1084 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
1085 "gl_TessLevelOuter");
1086 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
1087 "gl_TessLevelInner");
1089 if (state
->ARB_shader_viewport_layer_array_enable
) {
1090 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1091 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1092 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1093 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1099 * Generate variables which only exist in geometry shaders.
1102 builtin_variable_generator::generate_gs_special_vars()
1106 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1107 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1108 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
||
1109 state
->OES_viewport_array_enable
) {
1110 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1111 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1113 if (state
->is_version(400, 320) || state
->ARB_gpu_shader5_enable
||
1114 state
->OES_geometry_shader_enable
|| state
->EXT_geometry_shader_enable
) {
1115 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1118 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1119 * evaluation shaders, it has a different function there than it has in
1120 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1121 * as special geometry shader variables.
1123 * Note that although the general convention of suffixing geometry shader
1124 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1125 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1126 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1128 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
1129 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1130 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1131 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1136 * Generate variables which only exist in fragment shaders.
1139 builtin_variable_generator::generate_fs_special_vars()
1143 if (this->state
->ctx
->Const
.GLSLFragCoordIsSysVal
)
1144 add_system_value(SYSTEM_VALUE_FRAG_COORD
, vec4_t
, "gl_FragCoord");
1146 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
1148 if (this->state
->ctx
->Const
.GLSLFrontFacingIsSysVal
)
1149 add_system_value(SYSTEM_VALUE_FRONT_FACE
, bool_t
, "gl_FrontFacing");
1151 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
1153 if (state
->is_version(120, 100))
1154 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
1156 if (state
->has_geometry_shader()) {
1157 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1158 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1161 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1162 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1163 * They were removed from GLSL ES 3.00.
1165 if (compatibility
|| !state
->is_version(420, 300)) {
1166 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1167 add_output(FRAG_RESULT_DATA0
,
1168 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1171 if (state
->has_framebuffer_fetch() && !state
->is_version(130, 300)) {
1172 ir_variable
*const var
=
1173 add_output(FRAG_RESULT_DATA0
,
1174 array(vec4_t
, state
->Const
.MaxDrawBuffers
),
1176 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
1177 var
->data
.read_only
= 1;
1178 var
->data
.fb_fetch_output
= 1;
1181 if (state
->es_shader
&& state
->language_version
== 100 && state
->EXT_blend_func_extended_enable
) {
1182 add_index_output(FRAG_RESULT_COLOR
, 1, vec4_t
,
1183 "gl_SecondaryFragColorEXT");
1184 add_index_output(FRAG_RESULT_DATA0
, 1,
1185 array(vec4_t
, state
->Const
.MaxDualSourceDrawBuffers
),
1186 "gl_SecondaryFragDataEXT");
1189 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1192 if (state
->is_version(110, 300))
1193 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1195 if (state
->ARB_shader_stencil_export_enable
) {
1196 ir_variable
*const var
=
1197 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1198 if (state
->ARB_shader_stencil_export_warn
)
1199 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1202 if (state
->AMD_shader_stencil_export_enable
) {
1203 ir_variable
*const var
=
1204 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1205 if (state
->AMD_shader_stencil_export_warn
)
1206 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1209 if (state
->is_version(400, 320) ||
1210 state
->ARB_sample_shading_enable
||
1211 state
->OES_sample_variables_enable
) {
1212 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1213 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1214 /* From the ARB_sample_shading specification:
1215 * "The number of elements in the array is ceil(<s>/32), where
1216 * <s> is the maximum number of color samples supported by the
1218 * Since no drivers expose more than 32x MSAA, we can simply set
1219 * the array size to 1 rather than computing it.
1221 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1224 if (state
->is_version(400, 320) ||
1225 state
->ARB_gpu_shader5_enable
||
1226 state
->OES_sample_variables_enable
) {
1227 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1230 if (state
->is_version(430, 320) ||
1231 state
->ARB_fragment_layer_viewport_enable
||
1232 state
->OES_geometry_shader_enable
||
1233 state
->EXT_geometry_shader_enable
) {
1234 var
= add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1235 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1238 if (state
->is_version(430, 0) ||
1239 state
->ARB_fragment_layer_viewport_enable
||
1240 state
->OES_viewport_array_enable
) {
1241 var
= add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1242 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1245 if (state
->is_version(450, 310) || state
->ARB_ES3_1_compatibility_enable
)
1246 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION
, bool_t
, "gl_HelperInvocation");
1251 * Generate variables which only exist in compute shaders.
1254 builtin_variable_generator::generate_cs_special_vars()
1256 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1257 "gl_LocalInvocationID");
1258 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1259 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS
, uvec3_t
, "gl_NumWorkGroups");
1261 if (state
->ARB_compute_variable_group_size_enable
) {
1262 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE
,
1263 uvec3_t
, "gl_LocalGroupSizeARB");
1266 if (state
->ctx
->Const
.LowerCsDerivedVariables
) {
1267 add_variable("gl_GlobalInvocationID", uvec3_t
, ir_var_auto
, 0);
1268 add_variable("gl_LocalInvocationIndex", uint_t
, ir_var_auto
, 0);
1270 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID
,
1271 uvec3_t
, "gl_GlobalInvocationID");
1272 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
,
1273 uint_t
, "gl_LocalInvocationIndex");
1279 * Add a single "varying" variable. The variable's type and direction (input
1280 * or output) are adjusted as appropriate for the type of shader being
1284 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1287 switch (state
->stage
) {
1288 case MESA_SHADER_TESS_CTRL
:
1289 case MESA_SHADER_TESS_EVAL
:
1290 case MESA_SHADER_GEOMETRY
:
1291 this->per_vertex_in
.add_field(slot
, type
, name
);
1293 case MESA_SHADER_VERTEX
:
1294 this->per_vertex_out
.add_field(slot
, type
, name
);
1296 case MESA_SHADER_FRAGMENT
:
1297 add_input(slot
, type
, name
);
1299 case MESA_SHADER_COMPUTE
:
1300 /* Compute shaders don't have varyings. */
1307 * Generate variables that are used to communicate data from one shader stage
1308 * to the next ("varyings").
1311 builtin_variable_generator::generate_varyings()
1313 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1314 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1315 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1316 if (!state
->es_shader
||
1317 state
->stage
== MESA_SHADER_VERTEX
||
1318 (state
->stage
== MESA_SHADER_GEOMETRY
&&
1319 (state
->OES_geometry_point_size_enable
||
1320 state
->EXT_geometry_point_size_enable
)) ||
1321 ((state
->stage
== MESA_SHADER_TESS_CTRL
||
1322 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
1323 (state
->OES_tessellation_point_size_enable
||
1324 state
->EXT_tessellation_point_size_enable
))) {
1325 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1329 if (state
->has_clip_distance()) {
1330 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1333 if (state
->has_cull_distance()) {
1334 add_varying(VARYING_SLOT_CULL_DIST0
, array(float_t
, 0),
1338 if (compatibility
) {
1339 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1340 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1341 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1342 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1343 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1345 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1346 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1347 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1348 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1349 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1353 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1356 * "In the tessellation control language, built-in variables are
1357 * intrinsically declared as:
1361 * float gl_PointSize;
1362 * float gl_ClipDistance[];
1363 * } gl_in[gl_MaxPatchVertices];"
1365 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1366 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1367 const glsl_type
*per_vertex_in_type
=
1368 this->per_vertex_in
.construct_interface_instance();
1369 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1370 ir_var_shader_in
, -1);
1372 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1373 const glsl_type
*per_vertex_in_type
=
1374 this->per_vertex_in
.construct_interface_instance();
1375 add_variable("gl_in", array(per_vertex_in_type
, 0),
1376 ir_var_shader_in
, -1);
1378 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1379 const glsl_type
*per_vertex_out_type
=
1380 this->per_vertex_out
.construct_interface_instance();
1381 add_variable("gl_out", array(per_vertex_out_type
, 0),
1382 ir_var_shader_out
, -1);
1384 if (state
->stage
== MESA_SHADER_VERTEX
||
1385 state
->stage
== MESA_SHADER_TESS_EVAL
||
1386 state
->stage
== MESA_SHADER_GEOMETRY
) {
1387 const glsl_type
*per_vertex_out_type
=
1388 this->per_vertex_out
.construct_interface_instance();
1389 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1390 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1392 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1393 fields
[i
].location
);
1394 var
->data
.interpolation
= fields
[i
].interpolation
;
1395 var
->data
.centroid
= fields
[i
].centroid
;
1396 var
->data
.sample
= fields
[i
].sample
;
1397 var
->data
.patch
= fields
[i
].patch
;
1398 var
->data
.precision
= fields
[i
].precision
;
1399 var
->init_interface_type(per_vertex_out_type
);
1405 }; /* Anonymous namespace */
1409 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1410 struct _mesa_glsl_parse_state
*state
)
1412 builtin_variable_generator
gen(instructions
, state
);
1414 gen
.generate_constants();
1415 gen
.generate_uniforms();
1417 gen
.generate_varyings();
1419 switch (state
->stage
) {
1420 case MESA_SHADER_VERTEX
:
1421 gen
.generate_vs_special_vars();
1423 case MESA_SHADER_TESS_CTRL
:
1424 gen
.generate_tcs_special_vars();
1426 case MESA_SHADER_TESS_EVAL
:
1427 gen
.generate_tes_special_vars();
1429 case MESA_SHADER_GEOMETRY
:
1430 gen
.generate_gs_special_vars();
1432 case MESA_SHADER_FRAGMENT
:
1433 gen
.generate_fs_special_vars();
1435 case MESA_SHADER_COMPUTE
:
1436 gen
.generate_cs_special_vars();
1443 * Initialize compute shader variables with values that are derived from other
1444 * compute shader variable.
1447 initialize_cs_derived_variables(gl_shader
*shader
,
1448 ir_function_signature
*const main_sig
)
1450 assert(shader
->Stage
== MESA_SHADER_COMPUTE
);
1452 ir_variable
*gl_GlobalInvocationID
=
1453 shader
->symbols
->get_variable("gl_GlobalInvocationID");
1454 assert(gl_GlobalInvocationID
);
1455 ir_variable
*gl_WorkGroupID
=
1456 shader
->symbols
->get_variable("gl_WorkGroupID");
1457 assert(gl_WorkGroupID
);
1458 ir_variable
*gl_WorkGroupSize
=
1459 shader
->symbols
->get_variable("gl_WorkGroupSize");
1460 if (gl_WorkGroupSize
== NULL
) {
1461 void *const mem_ctx
= ralloc_parent(shader
->ir
);
1462 gl_WorkGroupSize
= new(mem_ctx
) ir_variable(glsl_type::uvec3_type
,
1465 gl_WorkGroupSize
->data
.how_declared
= ir_var_declared_implicitly
;
1466 gl_WorkGroupSize
->data
.read_only
= true;
1467 shader
->ir
->push_head(gl_WorkGroupSize
);
1469 ir_variable
*gl_LocalInvocationID
=
1470 shader
->symbols
->get_variable("gl_LocalInvocationID");
1471 assert(gl_LocalInvocationID
);
1473 /* gl_GlobalInvocationID =
1474 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1476 ir_instruction
*inst
=
1477 assign(gl_GlobalInvocationID
,
1478 add(mul(gl_WorkGroupID
, gl_WorkGroupSize
),
1479 gl_LocalInvocationID
));
1480 main_sig
->body
.push_head(inst
);
1482 /* gl_LocalInvocationIndex =
1483 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1484 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1485 * gl_LocalInvocationID.x;
1487 ir_expression
*index_z
=
1488 mul(mul(swizzle_z(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
)),
1489 swizzle_y(gl_WorkGroupSize
));
1490 ir_expression
*index_y
=
1491 mul(swizzle_y(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
));
1492 ir_expression
*index_y_plus_z
= add(index_y
, index_z
);
1493 operand
index_x(swizzle_x(gl_LocalInvocationID
));
1494 ir_expression
*index_x_plus_y_plus_z
= add(index_y_plus_z
, index_x
);
1495 ir_variable
*gl_LocalInvocationIndex
=
1496 shader
->symbols
->get_variable("gl_LocalInvocationIndex");
1497 assert(gl_LocalInvocationIndex
);
1498 inst
= assign(gl_LocalInvocationIndex
, index_x_plus_y_plus_z
);
1499 main_sig
->body
.push_head(inst
);
1504 * Initialize builtin variables with values based on other builtin variables.
1505 * These are initialized in the main function.
1508 _mesa_glsl_initialize_derived_variables(struct gl_context
*ctx
,
1511 /* We only need to set CS variables currently. */
1512 if (shader
->Stage
== MESA_SHADER_COMPUTE
&&
1513 ctx
->Const
.LowerCsDerivedVariables
) {
1514 ir_function_signature
*const main_sig
=
1515 _mesa_get_main_function_signature(shader
->symbols
);
1517 if (main_sig
!= NULL
)
1518 initialize_cs_derived_variables(shader
, main_sig
);