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/mtypes.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
}
41 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
42 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
43 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
44 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
47 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
48 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
51 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
52 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
53 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
54 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
55 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
56 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
57 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
58 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
61 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
62 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
63 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
64 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
65 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
66 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
69 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
70 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
71 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
72 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
73 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
74 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
77 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
78 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
79 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
80 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
81 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
82 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
83 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
84 MAKE_SWIZZLE4(SWIZZLE_X
,
88 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
89 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
90 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
91 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
92 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
93 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
96 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
97 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
100 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
101 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
104 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
105 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
108 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
109 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
110 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
111 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
114 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
115 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
116 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
117 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
120 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
121 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
124 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
125 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
128 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
129 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
132 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
133 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
136 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
137 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
140 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
141 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
144 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
145 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
148 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
149 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
152 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
153 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
156 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
157 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
158 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
159 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
160 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
161 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
164 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
165 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
168 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
169 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
172 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
173 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
176 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
177 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
180 #define MATRIX(name, statevar, modifier) \
181 static const struct gl_builtin_uniform_element name ## _elements[] = { \
182 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
183 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
184 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
185 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
188 MATRIX(gl_ModelViewMatrix
,
189 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
190 MATRIX(gl_ModelViewMatrixInverse
,
191 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
192 MATRIX(gl_ModelViewMatrixTranspose
,
193 STATE_MODELVIEW_MATRIX
, 0);
194 MATRIX(gl_ModelViewMatrixInverseTranspose
,
195 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
197 MATRIX(gl_ProjectionMatrix
,
198 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
199 MATRIX(gl_ProjectionMatrixInverse
,
200 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
201 MATRIX(gl_ProjectionMatrixTranspose
,
202 STATE_PROJECTION_MATRIX
, 0);
203 MATRIX(gl_ProjectionMatrixInverseTranspose
,
204 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
206 MATRIX(gl_ModelViewProjectionMatrix
,
207 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
208 MATRIX(gl_ModelViewProjectionMatrixInverse
,
209 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
210 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
211 STATE_MVP_MATRIX
, 0);
212 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
213 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
215 MATRIX(gl_TextureMatrix
,
216 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
217 MATRIX(gl_TextureMatrixInverse
,
218 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
219 MATRIX(gl_TextureMatrixTranspose
,
220 STATE_TEXTURE_MATRIX
, 0);
221 MATRIX(gl_TextureMatrixInverseTranspose
,
222 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
224 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
225 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
226 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
227 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
228 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
229 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
230 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
235 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
237 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
238 STATEVAR(gl_NumSamples
),
239 STATEVAR(gl_DepthRange
),
240 STATEVAR(gl_ClipPlane
),
242 STATEVAR(gl_FrontMaterial
),
243 STATEVAR(gl_BackMaterial
),
244 STATEVAR(gl_LightSource
),
245 STATEVAR(gl_LightModel
),
246 STATEVAR(gl_FrontLightModelProduct
),
247 STATEVAR(gl_BackLightModelProduct
),
248 STATEVAR(gl_FrontLightProduct
),
249 STATEVAR(gl_BackLightProduct
),
250 STATEVAR(gl_TextureEnvColor
),
251 STATEVAR(gl_EyePlaneS
),
252 STATEVAR(gl_EyePlaneT
),
253 STATEVAR(gl_EyePlaneR
),
254 STATEVAR(gl_EyePlaneQ
),
255 STATEVAR(gl_ObjectPlaneS
),
256 STATEVAR(gl_ObjectPlaneT
),
257 STATEVAR(gl_ObjectPlaneR
),
258 STATEVAR(gl_ObjectPlaneQ
),
261 STATEVAR(gl_ModelViewMatrix
),
262 STATEVAR(gl_ModelViewMatrixInverse
),
263 STATEVAR(gl_ModelViewMatrixTranspose
),
264 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
266 STATEVAR(gl_ProjectionMatrix
),
267 STATEVAR(gl_ProjectionMatrixInverse
),
268 STATEVAR(gl_ProjectionMatrixTranspose
),
269 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
271 STATEVAR(gl_ModelViewProjectionMatrix
),
272 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
273 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
274 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
276 STATEVAR(gl_TextureMatrix
),
277 STATEVAR(gl_TextureMatrixInverse
),
278 STATEVAR(gl_TextureMatrixTranspose
),
279 STATEVAR(gl_TextureMatrixInverseTranspose
),
281 STATEVAR(gl_NormalMatrix
),
282 STATEVAR(gl_NormalScale
),
284 STATEVAR(gl_FogParamsOptimizedMESA
),
285 STATEVAR(gl_CurrentAttribVertMESA
),
286 STATEVAR(gl_CurrentAttribFragMESA
),
295 * Data structure that accumulates fields for the gl_PerVertex interface
298 class per_vertex_accumulator
301 per_vertex_accumulator();
302 void add_field(int slot
, const glsl_type
*type
, const char *name
);
303 const glsl_type
*construct_interface_instance() const;
306 glsl_struct_field fields
[11];
311 per_vertex_accumulator::per_vertex_accumulator()
319 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
322 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
323 this->fields
[this->num_fields
].type
= type
;
324 this->fields
[this->num_fields
].name
= name
;
325 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
326 this->fields
[this->num_fields
].location
= slot
;
327 this->fields
[this->num_fields
].offset
= -1;
328 this->fields
[this->num_fields
].interpolation
= INTERP_MODE_NONE
;
329 this->fields
[this->num_fields
].centroid
= 0;
330 this->fields
[this->num_fields
].sample
= 0;
331 this->fields
[this->num_fields
].patch
= 0;
332 this->fields
[this->num_fields
].precision
= GLSL_PRECISION_NONE
;
333 this->fields
[this->num_fields
].memory_read_only
= 0;
334 this->fields
[this->num_fields
].memory_write_only
= 0;
335 this->fields
[this->num_fields
].memory_coherent
= 0;
336 this->fields
[this->num_fields
].memory_volatile
= 0;
337 this->fields
[this->num_fields
].memory_restrict
= 0;
338 this->fields
[this->num_fields
].image_format
= 0;
339 this->fields
[this->num_fields
].explicit_xfb_buffer
= 0;
340 this->fields
[this->num_fields
].xfb_buffer
= -1;
341 this->fields
[this->num_fields
].xfb_stride
= -1;
347 per_vertex_accumulator::construct_interface_instance() const
349 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
350 GLSL_INTERFACE_PACKING_STD140
,
356 class builtin_variable_generator
359 builtin_variable_generator(exec_list
*instructions
,
360 struct _mesa_glsl_parse_state
*state
);
361 void generate_constants();
362 void generate_uniforms();
363 void generate_special_vars();
364 void generate_vs_special_vars();
365 void generate_tcs_special_vars();
366 void generate_tes_special_vars();
367 void generate_gs_special_vars();
368 void generate_fs_special_vars();
369 void generate_cs_special_vars();
370 void generate_varyings();
373 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
375 return glsl_type::get_array_instance(base
, elements
);
378 const glsl_type
*type(const char *name
)
380 return symtab
->get_type(name
);
383 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
385 return add_variable(name
, type
, ir_var_shader_in
, slot
);
388 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
390 return add_variable(name
, type
, ir_var_shader_out
, slot
);
393 ir_variable
*add_index_output(int slot
, int index
, const glsl_type
*type
, const char *name
)
395 return add_index_variable(name
, type
, ir_var_shader_out
, slot
, index
);
398 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
401 return add_variable(name
, type
, ir_var_system_value
, slot
);
404 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
405 enum ir_variable_mode mode
, int slot
);
406 ir_variable
*add_index_variable(const char *name
, const glsl_type
*type
,
407 enum ir_variable_mode mode
, int slot
, int index
);
408 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
409 ir_variable
*add_const(const char *name
, int value
);
410 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
411 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
413 exec_list
* const instructions
;
414 struct _mesa_glsl_parse_state
* const state
;
415 glsl_symbol_table
* const symtab
;
418 * True if compatibility-profile-only variables should be included. (In
419 * desktop GL, these are always included when the GLSL version is 1.30 and
422 const bool compatibility
;
424 const glsl_type
* const bool_t
;
425 const glsl_type
* const int_t
;
426 const glsl_type
* const uint_t
;
427 const glsl_type
* const uint64_t;
428 const glsl_type
* const float_t
;
429 const glsl_type
* const vec2_t
;
430 const glsl_type
* const vec3_t
;
431 const glsl_type
* const vec4_t
;
432 const glsl_type
* const uvec3_t
;
433 const glsl_type
* const mat3_t
;
434 const glsl_type
* const mat4_t
;
436 per_vertex_accumulator per_vertex_in
;
437 per_vertex_accumulator per_vertex_out
;
441 builtin_variable_generator::builtin_variable_generator(
442 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
443 : instructions(instructions
), state(state
), symtab(state
->symbols
),
444 compatibility(state
->compat_shader
|| state
->ARB_compatibility_enable
),
445 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
446 uint_t(glsl_type::uint_type
),
447 uint64_t(glsl_type::uint64_t_type
),
448 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
449 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
450 uvec3_t(glsl_type::uvec3_type
),
451 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
456 builtin_variable_generator::add_index_variable(const char *name
,
457 const glsl_type
*type
,
458 enum ir_variable_mode mode
, int slot
, int index
)
460 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
461 var
->data
.how_declared
= ir_var_declared_implicitly
;
463 switch (var
->data
.mode
) {
465 case ir_var_shader_in
:
467 case ir_var_system_value
:
468 var
->data
.read_only
= true;
470 case ir_var_shader_out
:
471 case ir_var_shader_storage
:
474 /* The only variables that are added using this function should be
475 * uniforms, shader storage, shader inputs, and shader outputs, constants
476 * (which use ir_var_auto), and system values.
482 var
->data
.location
= slot
;
483 var
->data
.explicit_location
= (slot
>= 0);
484 var
->data
.explicit_index
= 1;
485 var
->data
.index
= index
;
487 /* Once the variable is created an initialized, add it to the symbol table
488 * and add the declaration to the IR stream.
490 instructions
->push_tail(var
);
492 symtab
->add_variable(var
);
497 builtin_variable_generator::add_variable(const char *name
,
498 const glsl_type
*type
,
499 enum ir_variable_mode mode
, int slot
)
501 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
502 var
->data
.how_declared
= ir_var_declared_implicitly
;
504 switch (var
->data
.mode
) {
506 case ir_var_shader_in
:
508 case ir_var_system_value
:
509 var
->data
.read_only
= true;
511 case ir_var_shader_out
:
512 case ir_var_shader_storage
:
515 /* The only variables that are added using this function should be
516 * uniforms, shader storage, shader inputs, and shader outputs, constants
517 * (which use ir_var_auto), and system values.
523 var
->data
.location
= slot
;
524 var
->data
.explicit_location
= (slot
>= 0);
525 var
->data
.explicit_index
= 0;
527 /* Once the variable is created an initialized, add it to the symbol table
528 * and add the declaration to the IR stream.
530 instructions
->push_tail(var
);
532 symtab
->add_variable(var
);
536 extern "C" const struct gl_builtin_uniform_desc
*
537 _mesa_glsl_get_builtin_uniform_desc(const char *name
)
539 for (unsigned i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
540 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
541 return &_mesa_builtin_uniform_desc
[i
];
548 builtin_variable_generator::add_uniform(const glsl_type
*type
,
551 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
553 const struct gl_builtin_uniform_desc
* const statevar
=
554 _mesa_glsl_get_builtin_uniform_desc(name
);
555 assert(statevar
!= NULL
);
557 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
559 ir_state_slot
*slots
=
560 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
562 for (unsigned a
= 0; a
< array_count
; a
++) {
563 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
564 const struct gl_builtin_uniform_element
*element
=
565 &statevar
->elements
[j
];
567 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
568 if (type
->is_array()) {
569 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
570 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
571 slots
->tokens
[2] = a
;
573 slots
->tokens
[1] = a
;
577 slots
->swizzle
= element
->swizzle
;
587 builtin_variable_generator::add_const(const char *name
, int value
)
589 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
591 var
->constant_value
= new(var
) ir_constant(value
);
592 var
->constant_initializer
= new(var
) ir_constant(value
);
593 var
->data
.has_initializer
= true;
599 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
602 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
604 ir_constant_data data
;
605 memset(&data
, 0, sizeof(data
));
609 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
610 var
->constant_initializer
=
611 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
612 var
->data
.has_initializer
= true;
618 builtin_variable_generator::generate_constants()
620 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
621 add_const("gl_MaxVertexTextureImageUnits",
622 state
->Const
.MaxVertexTextureImageUnits
);
623 add_const("gl_MaxCombinedTextureImageUnits",
624 state
->Const
.MaxCombinedTextureImageUnits
);
625 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
626 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
628 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
629 * GL counts them in units of "components" or "floats" and also in units
630 * of vectors since GL 4.1
632 if (!state
->es_shader
) {
633 add_const("gl_MaxFragmentUniformComponents",
634 state
->Const
.MaxFragmentUniformComponents
);
635 add_const("gl_MaxVertexUniformComponents",
636 state
->Const
.MaxVertexUniformComponents
);
639 if (state
->is_version(410, 100)) {
640 add_const("gl_MaxVertexUniformVectors",
641 state
->Const
.MaxVertexUniformComponents
/ 4);
642 add_const("gl_MaxFragmentUniformVectors",
643 state
->Const
.MaxFragmentUniformComponents
/ 4);
645 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
646 * vertex and fragment shader constants.
648 if (state
->is_version(0, 300)) {
649 add_const("gl_MaxVertexOutputVectors",
650 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
651 add_const("gl_MaxFragmentInputVectors",
652 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
654 add_const("gl_MaxVaryingVectors",
655 state
->ctx
->Const
.MaxVarying
);
658 /* EXT_blend_func_extended brings a built in constant
659 * for determining number of dual source draw buffers
661 if (state
->EXT_blend_func_extended_enable
) {
662 add_const("gl_MaxDualSourceDrawBuffersEXT",
663 state
->Const
.MaxDualSourceDrawBuffers
);
666 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
669 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
672 /* Texel offsets were introduced in ARB_shading_language_420pack (which
673 * requires desktop GLSL version 130), and adopted into desktop GLSL
674 * version 4.20 and GLSL ES version 3.00.
676 if ((state
->is_version(130, 0) &&
677 state
->ARB_shading_language_420pack_enable
) ||
678 state
->is_version(420, 300)) {
679 add_const("gl_MinProgramTexelOffset",
680 state
->Const
.MinProgramTexelOffset
);
681 add_const("gl_MaxProgramTexelOffset",
682 state
->Const
.MaxProgramTexelOffset
);
685 if (state
->has_clip_distance()) {
686 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
688 if (state
->is_version(130, 0)) {
689 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
691 if (state
->has_cull_distance()) {
692 add_const("gl_MaxCullDistances", state
->Const
.MaxClipPlanes
);
693 add_const("gl_MaxCombinedClipAndCullDistances",
694 state
->Const
.MaxClipPlanes
);
697 if (state
->has_geometry_shader()) {
698 add_const("gl_MaxVertexOutputComponents",
699 state
->Const
.MaxVertexOutputComponents
);
700 add_const("gl_MaxGeometryInputComponents",
701 state
->Const
.MaxGeometryInputComponents
);
702 add_const("gl_MaxGeometryOutputComponents",
703 state
->Const
.MaxGeometryOutputComponents
);
704 add_const("gl_MaxFragmentInputComponents",
705 state
->Const
.MaxFragmentInputComponents
);
706 add_const("gl_MaxGeometryTextureImageUnits",
707 state
->Const
.MaxGeometryTextureImageUnits
);
708 add_const("gl_MaxGeometryOutputVertices",
709 state
->Const
.MaxGeometryOutputVertices
);
710 add_const("gl_MaxGeometryTotalOutputComponents",
711 state
->Const
.MaxGeometryTotalOutputComponents
);
712 add_const("gl_MaxGeometryUniformComponents",
713 state
->Const
.MaxGeometryUniformComponents
);
715 /* Note: the GLSL 1.50-4.40 specs require
716 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
717 * But they do not define what it means (and there does not appear to be
718 * any corresponding constant in the GL specs). However,
719 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
720 * be the maximum number of components available for use as geometry
721 * outputs. So we assume this is a synonym for
722 * gl_MaxGeometryOutputComponents.
724 add_const("gl_MaxGeometryVaryingComponents",
725 state
->Const
.MaxGeometryOutputComponents
);
729 /* Note: gl_MaxLights stopped being listed as an explicit constant in
730 * GLSL 1.30, however it continues to be referred to (as a minimum size
731 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
732 * this seems like it was probably an oversight.
734 add_const("gl_MaxLights", state
->Const
.MaxLights
);
736 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
738 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
739 * 1.50, however this seems like it was probably an oversight.
741 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
743 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
744 * re-introduced in GLSL 1.50, so this seems like it was probably an
747 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
750 if (state
->has_atomic_counters()) {
751 add_const("gl_MaxVertexAtomicCounters",
752 state
->Const
.MaxVertexAtomicCounters
);
753 add_const("gl_MaxFragmentAtomicCounters",
754 state
->Const
.MaxFragmentAtomicCounters
);
755 add_const("gl_MaxCombinedAtomicCounters",
756 state
->Const
.MaxCombinedAtomicCounters
);
757 add_const("gl_MaxAtomicCounterBindings",
758 state
->Const
.MaxAtomicBufferBindings
);
760 if (state
->has_geometry_shader()) {
761 add_const("gl_MaxGeometryAtomicCounters",
762 state
->Const
.MaxGeometryAtomicCounters
);
764 if (state
->is_version(110, 320)) {
765 add_const("gl_MaxTessControlAtomicCounters",
766 state
->Const
.MaxTessControlAtomicCounters
);
767 add_const("gl_MaxTessEvaluationAtomicCounters",
768 state
->Const
.MaxTessEvaluationAtomicCounters
);
772 if (state
->is_version(420, 310)) {
773 add_const("gl_MaxVertexAtomicCounterBuffers",
774 state
->Const
.MaxVertexAtomicCounterBuffers
);
775 add_const("gl_MaxFragmentAtomicCounterBuffers",
776 state
->Const
.MaxFragmentAtomicCounterBuffers
);
777 add_const("gl_MaxCombinedAtomicCounterBuffers",
778 state
->Const
.MaxCombinedAtomicCounterBuffers
);
779 add_const("gl_MaxAtomicCounterBufferSize",
780 state
->Const
.MaxAtomicCounterBufferSize
);
782 if (state
->has_geometry_shader()) {
783 add_const("gl_MaxGeometryAtomicCounterBuffers",
784 state
->Const
.MaxGeometryAtomicCounterBuffers
);
786 if (state
->is_version(110, 320)) {
787 add_const("gl_MaxTessControlAtomicCounterBuffers",
788 state
->Const
.MaxTessControlAtomicCounterBuffers
);
789 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
790 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
794 if (state
->is_version(430, 310) || state
->ARB_compute_shader_enable
) {
795 add_const("gl_MaxComputeAtomicCounterBuffers",
796 state
->Const
.MaxComputeAtomicCounterBuffers
);
797 add_const("gl_MaxComputeAtomicCounters",
798 state
->Const
.MaxComputeAtomicCounters
);
799 add_const("gl_MaxComputeImageUniforms",
800 state
->Const
.MaxComputeImageUniforms
);
801 add_const("gl_MaxComputeTextureImageUnits",
802 state
->Const
.MaxComputeTextureImageUnits
);
803 add_const("gl_MaxComputeUniformComponents",
804 state
->Const
.MaxComputeUniformComponents
);
806 add_const_ivec3("gl_MaxComputeWorkGroupCount",
807 state
->Const
.MaxComputeWorkGroupCount
[0],
808 state
->Const
.MaxComputeWorkGroupCount
[1],
809 state
->Const
.MaxComputeWorkGroupCount
[2]);
810 add_const_ivec3("gl_MaxComputeWorkGroupSize",
811 state
->Const
.MaxComputeWorkGroupSize
[0],
812 state
->Const
.MaxComputeWorkGroupSize
[1],
813 state
->Const
.MaxComputeWorkGroupSize
[2]);
815 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
817 * The built-in constant gl_WorkGroupSize is a compute-shader
818 * constant containing the local work-group size of the shader. The
819 * size of the work group in the X, Y, and Z dimensions is stored in
820 * the x, y, and z components. The constants values in
821 * gl_WorkGroupSize will match those specified in the required
822 * local_size_x, local_size_y, and local_size_z layout qualifiers
823 * for the current shader. This is a constant so that it can be
824 * used to size arrays of memory that can be shared within the local
825 * work group. It is a compile-time error to use gl_WorkGroupSize
826 * in a shader that does not declare a fixed local group size, or
827 * before that shader has declared a fixed local group size, using
828 * local_size_x, local_size_y, and local_size_z.
830 * To prevent the shader from trying to refer to gl_WorkGroupSize before
831 * the layout declaration, we don't define it here. Intead we define it
832 * in ast_cs_input_layout::hir().
836 if (state
->has_enhanced_layouts()) {
837 add_const("gl_MaxTransformFeedbackBuffers",
838 state
->Const
.MaxTransformFeedbackBuffers
);
839 add_const("gl_MaxTransformFeedbackInterleavedComponents",
840 state
->Const
.MaxTransformFeedbackInterleavedComponents
);
843 if (state
->has_shader_image_load_store()) {
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 special variables which exist in all shaders.
990 builtin_variable_generator::generate_special_vars()
992 if (state
->ARB_shader_ballot_enable
) {
993 add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE
, uint_t
, "gl_SubGroupSizeARB");
994 add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION
, uint_t
, "gl_SubGroupInvocationARB");
995 add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK
, uint64_t, "gl_SubGroupEqMaskARB");
996 add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK
, uint64_t, "gl_SubGroupGeMaskARB");
997 add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK
, uint64_t, "gl_SubGroupGtMaskARB");
998 add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK
, uint64_t, "gl_SubGroupLeMaskARB");
999 add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK
, uint64_t, "gl_SubGroupLtMaskARB");
1005 * Generate variables which only exist in vertex shaders.
1008 builtin_variable_generator::generate_vs_special_vars()
1012 if (state
->is_version(130, 300) || state
->EXT_gpu_shader4_enable
)
1013 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
1014 if (state
->is_version(460, 0)) {
1015 add_system_value(SYSTEM_VALUE_BASE_VERTEX
, int_t
, "gl_BaseVertex");
1016 add_system_value(SYSTEM_VALUE_BASE_INSTANCE
, int_t
, "gl_BaseInstance");
1017 add_system_value(SYSTEM_VALUE_DRAW_ID
, int_t
, "gl_DrawID");
1019 if (state
->ARB_draw_instanced_enable
)
1020 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
1021 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300) ||
1022 state
->EXT_gpu_shader4_enable
)
1023 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
1024 if (state
->ARB_shader_draw_parameters_enable
) {
1025 add_system_value(SYSTEM_VALUE_BASE_VERTEX
, int_t
, "gl_BaseVertexARB");
1026 add_system_value(SYSTEM_VALUE_BASE_INSTANCE
, int_t
, "gl_BaseInstanceARB");
1027 add_system_value(SYSTEM_VALUE_DRAW_ID
, int_t
, "gl_DrawIDARB");
1029 if (state
->AMD_vertex_shader_layer_enable
||
1030 state
->ARB_shader_viewport_layer_array_enable
) {
1031 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1032 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1034 if (state
->AMD_vertex_shader_viewport_index_enable
||
1035 state
->ARB_shader_viewport_layer_array_enable
) {
1036 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1037 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1039 if (compatibility
) {
1040 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
1041 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
1042 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
1043 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
1044 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
1045 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
1046 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
1047 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
1048 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
1049 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
1050 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
1051 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
1052 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
1058 * Generate variables which only exist in tessellation control shaders.
1061 builtin_variable_generator::generate_tcs_special_vars()
1063 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1064 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1065 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1067 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1068 "gl_TessLevelOuter")->data
.patch
= 1;
1069 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1070 "gl_TessLevelInner")->data
.patch
= 1;
1071 /* XXX What to do if multiple are flipped on? */
1072 int bbox_slot
= state
->ctx
->Const
.NoPrimitiveBoundingBoxOutput
? -1 :
1073 VARYING_SLOT_BOUNDING_BOX0
;
1074 if (state
->EXT_primitive_bounding_box_enable
)
1075 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxEXT")
1077 if (state
->OES_primitive_bounding_box_enable
)
1078 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxOES")
1080 if (state
->is_version(0, 320) || state
->ARB_ES3_2_compatibility_enable
)
1081 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBox")
1087 * Generate variables which only exist in tessellation evaluation shaders.
1090 builtin_variable_generator::generate_tes_special_vars()
1094 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1095 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1096 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
1097 if (this->state
->ctx
->Const
.GLSLTessLevelsAsInputs
) {
1098 add_input(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1099 "gl_TessLevelOuter")->data
.patch
= 1;
1100 add_input(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1101 "gl_TessLevelInner")->data
.patch
= 1;
1103 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
1104 "gl_TessLevelOuter");
1105 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
1106 "gl_TessLevelInner");
1108 if (state
->ARB_shader_viewport_layer_array_enable
) {
1109 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1110 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1111 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1112 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1118 * Generate variables which only exist in geometry shaders.
1121 builtin_variable_generator::generate_gs_special_vars()
1125 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1126 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1127 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
||
1128 state
->OES_viewport_array_enable
) {
1129 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1130 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1132 if (state
->is_version(400, 320) || state
->ARB_gpu_shader5_enable
||
1133 state
->OES_geometry_shader_enable
|| state
->EXT_geometry_shader_enable
) {
1134 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1137 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1138 * evaluation shaders, it has a different function there than it has in
1139 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1140 * as special geometry shader variables.
1142 * Note that although the general convention of suffixing geometry shader
1143 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1144 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1145 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1147 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
1148 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1149 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1150 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1155 * Generate variables which only exist in fragment shaders.
1158 builtin_variable_generator::generate_fs_special_vars()
1162 if (this->state
->ctx
->Const
.GLSLFragCoordIsSysVal
)
1163 add_system_value(SYSTEM_VALUE_FRAG_COORD
, vec4_t
, "gl_FragCoord");
1165 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
1167 if (this->state
->ctx
->Const
.GLSLFrontFacingIsSysVal
)
1168 add_system_value(SYSTEM_VALUE_FRONT_FACE
, bool_t
, "gl_FrontFacing");
1170 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
1172 if (state
->is_version(120, 100))
1173 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
1175 if (state
->has_geometry_shader() || state
->EXT_gpu_shader4_enable
) {
1176 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1177 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1180 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1181 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1182 * They were removed from GLSL ES 3.00.
1184 if (compatibility
|| !state
->is_version(420, 300)) {
1185 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1186 add_output(FRAG_RESULT_DATA0
,
1187 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1190 if (state
->has_framebuffer_fetch() && !state
->is_version(130, 300)) {
1191 ir_variable
*const var
=
1192 add_output(FRAG_RESULT_DATA0
,
1193 array(vec4_t
, state
->Const
.MaxDrawBuffers
),
1195 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
1196 var
->data
.read_only
= 1;
1197 var
->data
.fb_fetch_output
= 1;
1198 var
->data
.memory_coherent
= 1;
1201 if (state
->es_shader
&& state
->language_version
== 100 && state
->EXT_blend_func_extended_enable
) {
1202 add_index_output(FRAG_RESULT_COLOR
, 1, vec4_t
,
1203 "gl_SecondaryFragColorEXT");
1204 add_index_output(FRAG_RESULT_DATA0
, 1,
1205 array(vec4_t
, state
->Const
.MaxDualSourceDrawBuffers
),
1206 "gl_SecondaryFragDataEXT");
1209 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1212 if (state
->is_version(110, 300))
1213 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1215 if (state
->EXT_frag_depth_enable
)
1216 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepthEXT");
1218 if (state
->ARB_shader_stencil_export_enable
) {
1219 ir_variable
*const var
=
1220 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1221 if (state
->ARB_shader_stencil_export_warn
)
1222 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1225 if (state
->AMD_shader_stencil_export_enable
) {
1226 ir_variable
*const var
=
1227 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1228 if (state
->AMD_shader_stencil_export_warn
)
1229 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1232 if (state
->is_version(400, 320) ||
1233 state
->ARB_sample_shading_enable
||
1234 state
->OES_sample_variables_enable
) {
1235 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1236 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1237 /* From the ARB_sample_shading specification:
1238 * "The number of elements in the array is ceil(<s>/32), where
1239 * <s> is the maximum number of color samples supported by the
1241 * Since no drivers expose more than 32x MSAA, we can simply set
1242 * the array size to 1 rather than computing it.
1244 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1247 if (state
->is_version(400, 320) ||
1248 state
->ARB_gpu_shader5_enable
||
1249 state
->OES_sample_variables_enable
) {
1250 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1253 if (state
->is_version(430, 320) ||
1254 state
->ARB_fragment_layer_viewport_enable
||
1255 state
->OES_geometry_shader_enable
||
1256 state
->EXT_geometry_shader_enable
) {
1257 var
= add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1258 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1261 if (state
->is_version(430, 0) ||
1262 state
->ARB_fragment_layer_viewport_enable
||
1263 state
->OES_viewport_array_enable
) {
1264 var
= add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1265 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1268 if (state
->is_version(450, 310) || state
->ARB_ES3_1_compatibility_enable
)
1269 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION
, bool_t
, "gl_HelperInvocation");
1274 * Generate variables which only exist in compute shaders.
1277 builtin_variable_generator::generate_cs_special_vars()
1279 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1280 "gl_LocalInvocationID");
1281 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1282 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS
, uvec3_t
, "gl_NumWorkGroups");
1284 if (state
->ARB_compute_variable_group_size_enable
) {
1285 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE
,
1286 uvec3_t
, "gl_LocalGroupSizeARB");
1289 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID
,
1290 uvec3_t
, "gl_GlobalInvocationID");
1291 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
,
1292 uint_t
, "gl_LocalInvocationIndex");
1297 * Add a single "varying" variable. The variable's type and direction (input
1298 * or output) are adjusted as appropriate for the type of shader being
1302 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1305 switch (state
->stage
) {
1306 case MESA_SHADER_TESS_CTRL
:
1307 case MESA_SHADER_TESS_EVAL
:
1308 case MESA_SHADER_GEOMETRY
:
1309 this->per_vertex_in
.add_field(slot
, type
, name
);
1311 case MESA_SHADER_VERTEX
:
1312 this->per_vertex_out
.add_field(slot
, type
, name
);
1314 case MESA_SHADER_FRAGMENT
:
1315 add_input(slot
, type
, name
);
1317 case MESA_SHADER_COMPUTE
:
1318 /* Compute shaders don't have varyings. */
1327 * Generate variables that are used to communicate data from one shader stage
1328 * to the next ("varyings").
1331 builtin_variable_generator::generate_varyings()
1333 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1334 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1335 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1336 if (!state
->es_shader
||
1337 state
->stage
== MESA_SHADER_VERTEX
||
1338 (state
->stage
== MESA_SHADER_GEOMETRY
&&
1339 (state
->OES_geometry_point_size_enable
||
1340 state
->EXT_geometry_point_size_enable
)) ||
1341 ((state
->stage
== MESA_SHADER_TESS_CTRL
||
1342 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
1343 (state
->OES_tessellation_point_size_enable
||
1344 state
->EXT_tessellation_point_size_enable
))) {
1345 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1349 if (state
->has_clip_distance()) {
1350 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1353 if (state
->has_cull_distance()) {
1354 add_varying(VARYING_SLOT_CULL_DIST0
, array(float_t
, 0),
1358 if (compatibility
) {
1359 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1360 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1361 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1362 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1363 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1365 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1366 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1367 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1368 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1369 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1373 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1376 * "In the tessellation control language, built-in variables are
1377 * intrinsically declared as:
1381 * float gl_PointSize;
1382 * float gl_ClipDistance[];
1383 * } gl_in[gl_MaxPatchVertices];"
1385 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1386 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1387 const glsl_type
*per_vertex_in_type
=
1388 this->per_vertex_in
.construct_interface_instance();
1389 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1390 ir_var_shader_in
, -1);
1392 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1393 const glsl_type
*per_vertex_in_type
=
1394 this->per_vertex_in
.construct_interface_instance();
1395 add_variable("gl_in", array(per_vertex_in_type
, 0),
1396 ir_var_shader_in
, -1);
1398 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1399 const glsl_type
*per_vertex_out_type
=
1400 this->per_vertex_out
.construct_interface_instance();
1401 add_variable("gl_out", array(per_vertex_out_type
, 0),
1402 ir_var_shader_out
, -1);
1404 if (state
->stage
== MESA_SHADER_VERTEX
||
1405 state
->stage
== MESA_SHADER_TESS_EVAL
||
1406 state
->stage
== MESA_SHADER_GEOMETRY
) {
1407 const glsl_type
*per_vertex_out_type
=
1408 this->per_vertex_out
.construct_interface_instance();
1409 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1410 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1412 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1413 fields
[i
].location
);
1414 var
->data
.interpolation
= fields
[i
].interpolation
;
1415 var
->data
.centroid
= fields
[i
].centroid
;
1416 var
->data
.sample
= fields
[i
].sample
;
1417 var
->data
.patch
= fields
[i
].patch
;
1418 var
->data
.precision
= fields
[i
].precision
;
1419 var
->init_interface_type(per_vertex_out_type
);
1425 }; /* Anonymous namespace */
1429 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1430 struct _mesa_glsl_parse_state
*state
)
1432 builtin_variable_generator
gen(instructions
, state
);
1434 gen
.generate_constants();
1435 gen
.generate_uniforms();
1436 gen
.generate_special_vars();
1438 gen
.generate_varyings();
1440 switch (state
->stage
) {
1441 case MESA_SHADER_VERTEX
:
1442 gen
.generate_vs_special_vars();
1444 case MESA_SHADER_TESS_CTRL
:
1445 gen
.generate_tcs_special_vars();
1447 case MESA_SHADER_TESS_EVAL
:
1448 gen
.generate_tes_special_vars();
1450 case MESA_SHADER_GEOMETRY
:
1451 gen
.generate_gs_special_vars();
1453 case MESA_SHADER_FRAGMENT
:
1454 gen
.generate_fs_special_vars();
1456 case MESA_SHADER_COMPUTE
:
1457 gen
.generate_cs_special_vars();