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
}
40 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
41 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
42 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
43 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
46 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
47 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
50 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
51 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
52 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
53 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
54 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
55 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
56 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
57 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
60 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
61 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
62 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
63 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
64 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
65 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
68 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
69 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
70 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
71 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
72 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
73 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
76 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
77 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
78 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
79 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
80 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
81 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
82 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
83 MAKE_SWIZZLE4(SWIZZLE_X
,
87 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
88 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
89 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
90 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
91 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
92 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
95 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
96 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
99 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
100 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
103 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
104 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
107 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
108 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
109 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
110 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
113 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
114 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
115 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
116 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
119 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
120 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
123 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
124 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
127 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
128 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
131 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
132 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
135 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
136 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
139 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
140 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
143 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
144 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
147 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
148 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
151 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
152 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
155 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
156 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
157 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
158 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
159 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
160 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
163 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
164 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
167 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
168 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
171 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
172 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
175 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
176 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
179 #define MATRIX(name, statevar, modifier) \
180 static const struct gl_builtin_uniform_element name ## _elements[] = { \
181 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
182 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
183 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
184 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
187 MATRIX(gl_ModelViewMatrix
,
188 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
189 MATRIX(gl_ModelViewMatrixInverse
,
190 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
191 MATRIX(gl_ModelViewMatrixTranspose
,
192 STATE_MODELVIEW_MATRIX
, 0);
193 MATRIX(gl_ModelViewMatrixInverseTranspose
,
194 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
196 MATRIX(gl_ProjectionMatrix
,
197 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
198 MATRIX(gl_ProjectionMatrixInverse
,
199 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
200 MATRIX(gl_ProjectionMatrixTranspose
,
201 STATE_PROJECTION_MATRIX
, 0);
202 MATRIX(gl_ProjectionMatrixInverseTranspose
,
203 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
205 MATRIX(gl_ModelViewProjectionMatrix
,
206 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
207 MATRIX(gl_ModelViewProjectionMatrixInverse
,
208 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
209 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
210 STATE_MVP_MATRIX
, 0);
211 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
212 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
214 MATRIX(gl_TextureMatrix
,
215 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
216 MATRIX(gl_TextureMatrixInverse
,
217 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
218 MATRIX(gl_TextureMatrixTranspose
,
219 STATE_TEXTURE_MATRIX
, 0);
220 MATRIX(gl_TextureMatrixInverseTranspose
,
221 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
223 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
224 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
225 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
226 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
227 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
228 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
229 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
234 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
236 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
237 STATEVAR(gl_NumSamples
),
238 STATEVAR(gl_DepthRange
),
239 STATEVAR(gl_ClipPlane
),
241 STATEVAR(gl_FrontMaterial
),
242 STATEVAR(gl_BackMaterial
),
243 STATEVAR(gl_LightSource
),
244 STATEVAR(gl_LightModel
),
245 STATEVAR(gl_FrontLightModelProduct
),
246 STATEVAR(gl_BackLightModelProduct
),
247 STATEVAR(gl_FrontLightProduct
),
248 STATEVAR(gl_BackLightProduct
),
249 STATEVAR(gl_TextureEnvColor
),
250 STATEVAR(gl_EyePlaneS
),
251 STATEVAR(gl_EyePlaneT
),
252 STATEVAR(gl_EyePlaneR
),
253 STATEVAR(gl_EyePlaneQ
),
254 STATEVAR(gl_ObjectPlaneS
),
255 STATEVAR(gl_ObjectPlaneT
),
256 STATEVAR(gl_ObjectPlaneR
),
257 STATEVAR(gl_ObjectPlaneQ
),
260 STATEVAR(gl_ModelViewMatrix
),
261 STATEVAR(gl_ModelViewMatrixInverse
),
262 STATEVAR(gl_ModelViewMatrixTranspose
),
263 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
265 STATEVAR(gl_ProjectionMatrix
),
266 STATEVAR(gl_ProjectionMatrixInverse
),
267 STATEVAR(gl_ProjectionMatrixTranspose
),
268 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
270 STATEVAR(gl_ModelViewProjectionMatrix
),
271 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
272 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
273 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
275 STATEVAR(gl_TextureMatrix
),
276 STATEVAR(gl_TextureMatrixInverse
),
277 STATEVAR(gl_TextureMatrixTranspose
),
278 STATEVAR(gl_TextureMatrixInverseTranspose
),
280 STATEVAR(gl_NormalMatrix
),
281 STATEVAR(gl_NormalScale
),
283 STATEVAR(gl_FogParamsOptimizedMESA
),
284 STATEVAR(gl_CurrentAttribVertMESA
),
285 STATEVAR(gl_CurrentAttribFragMESA
),
294 * Data structure that accumulates fields for the gl_PerVertex interface
297 class per_vertex_accumulator
300 per_vertex_accumulator();
301 void add_field(int slot
, const glsl_type
*type
, const char *name
);
302 const glsl_type
*construct_interface_instance() const;
305 glsl_struct_field fields
[11];
310 per_vertex_accumulator::per_vertex_accumulator()
318 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
321 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
322 this->fields
[this->num_fields
].type
= type
;
323 this->fields
[this->num_fields
].name
= name
;
324 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
325 this->fields
[this->num_fields
].location
= slot
;
326 this->fields
[this->num_fields
].offset
= -1;
327 this->fields
[this->num_fields
].interpolation
= INTERP_QUALIFIER_NONE
;
328 this->fields
[this->num_fields
].centroid
= 0;
329 this->fields
[this->num_fields
].sample
= 0;
330 this->fields
[this->num_fields
].patch
= 0;
331 this->fields
[this->num_fields
].precision
= GLSL_PRECISION_NONE
;
332 this->fields
[this->num_fields
].image_read_only
= 0;
333 this->fields
[this->num_fields
].image_write_only
= 0;
334 this->fields
[this->num_fields
].image_coherent
= 0;
335 this->fields
[this->num_fields
].image_volatile
= 0;
336 this->fields
[this->num_fields
].image_restrict
= 0;
337 this->fields
[this->num_fields
].explicit_xfb_buffer
= 0;
338 this->fields
[this->num_fields
].xfb_buffer
= -1;
339 this->fields
[this->num_fields
].xfb_stride
= -1;
345 per_vertex_accumulator::construct_interface_instance() const
347 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
348 GLSL_INTERFACE_PACKING_STD140
,
353 class builtin_variable_generator
356 builtin_variable_generator(exec_list
*instructions
,
357 struct _mesa_glsl_parse_state
*state
);
358 void generate_constants();
359 void generate_uniforms();
360 void generate_vs_special_vars();
361 void generate_tcs_special_vars();
362 void generate_tes_special_vars();
363 void generate_gs_special_vars();
364 void generate_fs_special_vars();
365 void generate_cs_special_vars();
366 void generate_varyings();
369 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
371 return glsl_type::get_array_instance(base
, elements
);
374 const glsl_type
*type(const char *name
)
376 return symtab
->get_type(name
);
379 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
381 return add_variable(name
, type
, ir_var_shader_in
, slot
);
384 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
386 return add_variable(name
, type
, ir_var_shader_out
, slot
);
389 ir_variable
*add_index_output(int slot
, int index
, const glsl_type
*type
, const char *name
)
391 return add_index_variable(name
, type
, ir_var_shader_out
, slot
, index
);
394 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
397 return add_variable(name
, type
, ir_var_system_value
, slot
);
400 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
401 enum ir_variable_mode mode
, int slot
);
402 ir_variable
*add_index_variable(const char *name
, const glsl_type
*type
,
403 enum ir_variable_mode mode
, int slot
, int index
);
404 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
405 ir_variable
*add_const(const char *name
, int value
);
406 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
407 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
409 exec_list
* const instructions
;
410 struct _mesa_glsl_parse_state
* const state
;
411 glsl_symbol_table
* const symtab
;
414 * True if compatibility-profile-only variables should be included. (In
415 * desktop GL, these are always included when the GLSL version is 1.30 and
418 const bool compatibility
;
420 const glsl_type
* const bool_t
;
421 const glsl_type
* const int_t
;
422 const glsl_type
* const uint_t
;
423 const glsl_type
* const float_t
;
424 const glsl_type
* const vec2_t
;
425 const glsl_type
* const vec3_t
;
426 const glsl_type
* const vec4_t
;
427 const glsl_type
* const uvec3_t
;
428 const glsl_type
* const mat3_t
;
429 const glsl_type
* const mat4_t
;
431 per_vertex_accumulator per_vertex_in
;
432 per_vertex_accumulator per_vertex_out
;
436 builtin_variable_generator::builtin_variable_generator(
437 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
438 : instructions(instructions
), state(state
), symtab(state
->symbols
),
439 compatibility(!state
->is_version(140, 100)),
440 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
441 uint_t(glsl_type::uint_type
),
442 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
443 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
444 uvec3_t(glsl_type::uvec3_type
),
445 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
450 builtin_variable_generator::add_index_variable(const char *name
,
451 const glsl_type
*type
,
452 enum ir_variable_mode mode
, int slot
, int index
)
454 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
455 var
->data
.how_declared
= ir_var_declared_implicitly
;
457 switch (var
->data
.mode
) {
459 case ir_var_shader_in
:
461 case ir_var_system_value
:
462 var
->data
.read_only
= true;
464 case ir_var_shader_out
:
465 case ir_var_shader_storage
:
468 /* The only variables that are added using this function should be
469 * uniforms, shader storage, shader inputs, and shader outputs, constants
470 * (which use ir_var_auto), and system values.
476 var
->data
.location
= slot
;
477 var
->data
.explicit_location
= (slot
>= 0);
478 var
->data
.explicit_index
= 1;
479 var
->data
.index
= index
;
481 /* Once the variable is created an initialized, add it to the symbol table
482 * and add the declaration to the IR stream.
484 instructions
->push_tail(var
);
486 symtab
->add_variable(var
);
491 builtin_variable_generator::add_variable(const char *name
,
492 const glsl_type
*type
,
493 enum ir_variable_mode mode
, int slot
)
495 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
496 var
->data
.how_declared
= ir_var_declared_implicitly
;
498 switch (var
->data
.mode
) {
500 case ir_var_shader_in
:
502 case ir_var_system_value
:
503 var
->data
.read_only
= true;
505 case ir_var_shader_out
:
506 case ir_var_shader_storage
:
509 /* The only variables that are added using this function should be
510 * uniforms, shader storage, shader inputs, and shader outputs, constants
511 * (which use ir_var_auto), and system values.
517 var
->data
.location
= slot
;
518 var
->data
.explicit_location
= (slot
>= 0);
519 var
->data
.explicit_index
= 0;
521 /* Once the variable is created an initialized, add it to the symbol table
522 * and add the declaration to the IR stream.
524 instructions
->push_tail(var
);
526 symtab
->add_variable(var
);
530 extern "C" const struct gl_builtin_uniform_desc
*
531 _mesa_glsl_get_builtin_uniform_desc(const char *name
)
533 for (unsigned i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
534 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
535 return &_mesa_builtin_uniform_desc
[i
];
542 builtin_variable_generator::add_uniform(const glsl_type
*type
,
545 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
547 const struct gl_builtin_uniform_desc
* const statevar
=
548 _mesa_glsl_get_builtin_uniform_desc(name
);
549 assert(statevar
!= NULL
);
551 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
553 ir_state_slot
*slots
=
554 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
556 for (unsigned a
= 0; a
< array_count
; a
++) {
557 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
558 const struct gl_builtin_uniform_element
*element
=
559 &statevar
->elements
[j
];
561 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
562 if (type
->is_array()) {
563 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
564 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
565 slots
->tokens
[2] = a
;
567 slots
->tokens
[1] = a
;
571 slots
->swizzle
= element
->swizzle
;
581 builtin_variable_generator::add_const(const char *name
, int value
)
583 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
585 var
->constant_value
= new(var
) ir_constant(value
);
586 var
->constant_initializer
= new(var
) ir_constant(value
);
587 var
->data
.has_initializer
= true;
593 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
596 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
598 ir_constant_data data
;
599 memset(&data
, 0, sizeof(data
));
603 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
604 var
->constant_initializer
=
605 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
606 var
->data
.has_initializer
= true;
612 builtin_variable_generator::generate_constants()
614 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
615 add_const("gl_MaxVertexTextureImageUnits",
616 state
->Const
.MaxVertexTextureImageUnits
);
617 add_const("gl_MaxCombinedTextureImageUnits",
618 state
->Const
.MaxCombinedTextureImageUnits
);
619 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
620 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
622 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
623 * GL counts them in units of "components" or "floats".
625 if (state
->is_version(410, 100)) {
626 add_const("gl_MaxVertexUniformVectors",
627 state
->Const
.MaxVertexUniformComponents
/ 4);
628 add_const("gl_MaxFragmentUniformVectors",
629 state
->Const
.MaxFragmentUniformComponents
/ 4);
631 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
632 * vertex and fragment shader constants.
634 if (state
->is_version(0, 300)) {
635 add_const("gl_MaxVertexOutputVectors",
636 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
637 add_const("gl_MaxFragmentInputVectors",
638 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
640 add_const("gl_MaxVaryingVectors",
641 state
->ctx
->Const
.MaxVarying
);
644 /* EXT_blend_func_extended brings a built in constant
645 * for determining number of dual source draw buffers
647 if (state
->EXT_blend_func_extended_enable
) {
648 add_const("gl_MaxDualSourceDrawBuffersEXT",
649 state
->Const
.MaxDualSourceDrawBuffers
);
652 add_const("gl_MaxVertexUniformComponents",
653 state
->Const
.MaxVertexUniformComponents
);
655 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
658 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
660 add_const("gl_MaxFragmentUniformComponents",
661 state
->Const
.MaxFragmentUniformComponents
);
664 /* Texel offsets were introduced in ARB_shading_language_420pack (which
665 * requires desktop GLSL version 130), and adopted into desktop GLSL
666 * version 4.20 and GLSL ES version 3.00.
668 if ((state
->is_version(130, 0) &&
669 state
->ARB_shading_language_420pack_enable
) ||
670 state
->is_version(420, 300)) {
671 add_const("gl_MinProgramTexelOffset",
672 state
->Const
.MinProgramTexelOffset
);
673 add_const("gl_MaxProgramTexelOffset",
674 state
->Const
.MaxProgramTexelOffset
);
677 if (state
->has_clip_distance()) {
678 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
680 if (state
->is_version(130, 0)) {
681 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
683 if (state
->has_cull_distance()) {
684 add_const("gl_MaxCullDistances", state
->Const
.MaxClipPlanes
);
685 add_const("gl_MaxCombinedClipAndCullDistances",
686 state
->Const
.MaxClipPlanes
);
689 if (state
->has_geometry_shader()) {
690 add_const("gl_MaxVertexOutputComponents",
691 state
->Const
.MaxVertexOutputComponents
);
692 add_const("gl_MaxGeometryInputComponents",
693 state
->Const
.MaxGeometryInputComponents
);
694 add_const("gl_MaxGeometryOutputComponents",
695 state
->Const
.MaxGeometryOutputComponents
);
696 add_const("gl_MaxFragmentInputComponents",
697 state
->Const
.MaxFragmentInputComponents
);
698 add_const("gl_MaxGeometryTextureImageUnits",
699 state
->Const
.MaxGeometryTextureImageUnits
);
700 add_const("gl_MaxGeometryOutputVertices",
701 state
->Const
.MaxGeometryOutputVertices
);
702 add_const("gl_MaxGeometryTotalOutputComponents",
703 state
->Const
.MaxGeometryTotalOutputComponents
);
704 add_const("gl_MaxGeometryUniformComponents",
705 state
->Const
.MaxGeometryUniformComponents
);
707 /* Note: the GLSL 1.50-4.40 specs require
708 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
709 * But they do not define what it means (and there does not appear to be
710 * any corresponding constant in the GL specs). However,
711 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
712 * be the maximum number of components available for use as geometry
713 * outputs. So we assume this is a synonym for
714 * gl_MaxGeometryOutputComponents.
716 add_const("gl_MaxGeometryVaryingComponents",
717 state
->Const
.MaxGeometryOutputComponents
);
721 /* Note: gl_MaxLights stopped being listed as an explicit constant in
722 * GLSL 1.30, however it continues to be referred to (as a minimum size
723 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
724 * this seems like it was probably an oversight.
726 add_const("gl_MaxLights", state
->Const
.MaxLights
);
728 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
730 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
731 * 1.50, however this seems like it was probably an oversight.
733 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
735 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
736 * re-introduced in GLSL 1.50, so this seems like it was probably an
739 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
742 if (state
->has_atomic_counters()) {
743 add_const("gl_MaxVertexAtomicCounters",
744 state
->Const
.MaxVertexAtomicCounters
);
745 add_const("gl_MaxFragmentAtomicCounters",
746 state
->Const
.MaxFragmentAtomicCounters
);
747 add_const("gl_MaxCombinedAtomicCounters",
748 state
->Const
.MaxCombinedAtomicCounters
);
749 add_const("gl_MaxAtomicCounterBindings",
750 state
->Const
.MaxAtomicBufferBindings
);
752 if (state
->has_geometry_shader()) {
753 add_const("gl_MaxGeometryAtomicCounters",
754 state
->Const
.MaxGeometryAtomicCounters
);
756 if (!state
->es_shader
) {
757 add_const("gl_MaxTessControlAtomicCounters",
758 state
->Const
.MaxTessControlAtomicCounters
);
759 add_const("gl_MaxTessEvaluationAtomicCounters",
760 state
->Const
.MaxTessEvaluationAtomicCounters
);
764 if (state
->is_version(420, 310)) {
765 add_const("gl_MaxVertexAtomicCounterBuffers",
766 state
->Const
.MaxVertexAtomicCounterBuffers
);
767 add_const("gl_MaxFragmentAtomicCounterBuffers",
768 state
->Const
.MaxFragmentAtomicCounterBuffers
);
769 add_const("gl_MaxCombinedAtomicCounterBuffers",
770 state
->Const
.MaxCombinedAtomicCounterBuffers
);
771 add_const("gl_MaxAtomicCounterBufferSize",
772 state
->Const
.MaxAtomicCounterBufferSize
);
774 if (state
->has_geometry_shader()) {
775 add_const("gl_MaxGeometryAtomicCounterBuffers",
776 state
->Const
.MaxGeometryAtomicCounterBuffers
);
778 if (!state
->es_shader
) {
779 add_const("gl_MaxTessControlAtomicCounterBuffers",
780 state
->Const
.MaxTessControlAtomicCounterBuffers
);
781 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
782 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
786 if (state
->is_version(430, 310) || state
->ARB_compute_shader_enable
) {
787 add_const("gl_MaxComputeAtomicCounterBuffers",
788 state
->Const
.MaxComputeAtomicCounterBuffers
);
789 add_const("gl_MaxComputeAtomicCounters",
790 state
->Const
.MaxComputeAtomicCounters
);
791 add_const("gl_MaxComputeImageUniforms",
792 state
->Const
.MaxComputeImageUniforms
);
793 add_const("gl_MaxComputeTextureImageUnits",
794 state
->Const
.MaxComputeTextureImageUnits
);
795 add_const("gl_MaxComputeUniformComponents",
796 state
->Const
.MaxComputeUniformComponents
);
798 add_const_ivec3("gl_MaxComputeWorkGroupCount",
799 state
->Const
.MaxComputeWorkGroupCount
[0],
800 state
->Const
.MaxComputeWorkGroupCount
[1],
801 state
->Const
.MaxComputeWorkGroupCount
[2]);
802 add_const_ivec3("gl_MaxComputeWorkGroupSize",
803 state
->Const
.MaxComputeWorkGroupSize
[0],
804 state
->Const
.MaxComputeWorkGroupSize
[1],
805 state
->Const
.MaxComputeWorkGroupSize
[2]);
807 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
809 * The built-in constant gl_WorkGroupSize is a compute-shader
810 * constant containing the local work-group size of the shader. The
811 * size of the work group in the X, Y, and Z dimensions is stored in
812 * the x, y, and z components. The constants values in
813 * gl_WorkGroupSize will match those specified in the required
814 * local_size_x, local_size_y, and local_size_z layout qualifiers
815 * for the current shader. This is a constant so that it can be
816 * used to size arrays of memory that can be shared within the local
817 * work group. It is a compile-time error to use gl_WorkGroupSize
818 * in a shader that does not declare a fixed local group size, or
819 * before that shader has declared a fixed local group size, using
820 * local_size_x, local_size_y, and local_size_z.
822 * To prevent the shader from trying to refer to gl_WorkGroupSize before
823 * the layout declaration, we don't define it here. Intead we define it
824 * in ast_cs_input_layout::hir().
828 if (state
->has_enhanced_layouts()) {
829 add_const("gl_MaxTransformFeedbackBuffers",
830 state
->Const
.MaxTransformFeedbackBuffers
);
831 add_const("gl_MaxTransformFeedbackInterleavedComponents",
832 state
->Const
.MaxTransformFeedbackInterleavedComponents
);
835 if (state
->is_version(420, 310) ||
836 state
->ARB_shader_image_load_store_enable
) {
837 add_const("gl_MaxImageUnits",
838 state
->Const
.MaxImageUnits
);
839 add_const("gl_MaxVertexImageUniforms",
840 state
->Const
.MaxVertexImageUniforms
);
841 add_const("gl_MaxFragmentImageUniforms",
842 state
->Const
.MaxFragmentImageUniforms
);
843 add_const("gl_MaxCombinedImageUniforms",
844 state
->Const
.MaxCombinedImageUniforms
);
846 if (state
->has_geometry_shader()) {
847 add_const("gl_MaxGeometryImageUniforms",
848 state
->Const
.MaxGeometryImageUniforms
);
851 if (!state
->es_shader
) {
852 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
853 state
->Const
.MaxCombinedShaderOutputResources
);
854 add_const("gl_MaxImageSamples",
855 state
->Const
.MaxImageSamples
);
858 if (state
->is_version(400, 0) ||
859 state
->ARB_tessellation_shader_enable
) {
860 add_const("gl_MaxTessControlImageUniforms",
861 state
->Const
.MaxTessControlImageUniforms
);
862 add_const("gl_MaxTessEvaluationImageUniforms",
863 state
->Const
.MaxTessEvaluationImageUniforms
);
867 if (state
->is_version(440, 310) ||
868 state
->ARB_ES3_1_compatibility_enable
) {
869 add_const("gl_MaxCombinedShaderOutputResources",
870 state
->Const
.MaxCombinedShaderOutputResources
);
873 if (state
->is_version(410, 0) ||
874 state
->ARB_viewport_array_enable
)
875 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
877 if (state
->is_version(400, 0) ||
878 state
->ARB_tessellation_shader_enable
) {
879 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
880 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
881 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
882 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
883 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
884 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
885 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
886 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
887 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
888 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
889 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
890 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
893 if (state
->is_version(450, 320) ||
894 state
->OES_sample_variables_enable
||
895 state
->ARB_ES3_1_compatibility_enable
)
896 add_const("gl_MaxSamples", state
->Const
.MaxSamples
);
901 * Generate uniform variables (which exist in all types of shaders).
904 builtin_variable_generator::generate_uniforms()
906 if (state
->is_version(400, 320) ||
907 state
->ARB_sample_shading_enable
||
908 state
->OES_sample_variables_enable
)
909 add_uniform(int_t
, "gl_NumSamples");
910 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
911 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
912 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
915 add_uniform(mat4_t
, "gl_ModelViewMatrix");
916 add_uniform(mat4_t
, "gl_ProjectionMatrix");
917 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
918 add_uniform(mat3_t
, "gl_NormalMatrix");
919 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
920 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
921 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
922 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
923 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
924 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
925 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
926 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
927 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
928 add_uniform(float_t
, "gl_NormalScale");
929 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
930 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
932 const glsl_type
*const mat4_array_type
=
933 array(mat4_t
, state
->Const
.MaxTextureCoords
);
934 add_uniform(mat4_array_type
, "gl_TextureMatrix");
935 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
936 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
937 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
939 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
940 add_uniform(type("gl_PointParameters"), "gl_Point");
942 const glsl_type
*const material_parameters_type
=
943 type("gl_MaterialParameters");
944 add_uniform(material_parameters_type
, "gl_FrontMaterial");
945 add_uniform(material_parameters_type
, "gl_BackMaterial");
947 add_uniform(array(type("gl_LightSourceParameters"),
948 state
->Const
.MaxLights
),
951 const glsl_type
*const light_model_products_type
=
952 type("gl_LightModelProducts");
953 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
954 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
956 const glsl_type
*const light_products_type
=
957 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
958 add_uniform(light_products_type
, "gl_FrontLightProduct");
959 add_uniform(light_products_type
, "gl_BackLightProduct");
961 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
962 "gl_TextureEnvColor");
964 const glsl_type
*const texcoords_vec4
=
965 array(vec4_t
, state
->Const
.MaxTextureCoords
);
966 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
967 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
968 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
969 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
970 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
971 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
972 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
973 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
975 add_uniform(type("gl_FogParameters"), "gl_Fog");
981 * Generate variables which only exist in vertex shaders.
984 builtin_variable_generator::generate_vs_special_vars()
988 if (state
->is_version(130, 300))
989 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
990 if (state
->ARB_draw_instanced_enable
)
991 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
992 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
993 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
994 if (state
->ARB_shader_draw_parameters_enable
) {
995 add_system_value(SYSTEM_VALUE_BASE_VERTEX
, int_t
, "gl_BaseVertexARB");
996 add_system_value(SYSTEM_VALUE_BASE_INSTANCE
, int_t
, "gl_BaseInstanceARB");
997 add_system_value(SYSTEM_VALUE_DRAW_ID
, int_t
, "gl_DrawIDARB");
999 if (state
->AMD_vertex_shader_layer_enable
) {
1000 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1001 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1003 if (state
->AMD_vertex_shader_viewport_index_enable
) {
1004 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1005 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1007 if (compatibility
) {
1008 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
1009 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
1010 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
1011 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
1012 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
1013 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
1014 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
1015 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
1016 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
1017 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
1018 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
1019 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
1020 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
1026 * Generate variables which only exist in tessellation control shaders.
1029 builtin_variable_generator::generate_tcs_special_vars()
1031 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1032 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1033 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1035 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1036 "gl_TessLevelOuter")->data
.patch
= 1;
1037 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1038 "gl_TessLevelInner")->data
.patch
= 1;
1043 * Generate variables which only exist in tessellation evaluation shaders.
1046 builtin_variable_generator::generate_tes_special_vars()
1048 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1049 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1050 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
1051 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
1052 "gl_TessLevelOuter");
1053 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
1054 "gl_TessLevelInner");
1059 * Generate variables which only exist in geometry shaders.
1062 builtin_variable_generator::generate_gs_special_vars()
1066 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1067 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1068 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
) {
1069 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1070 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1072 if (state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
)
1073 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1075 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1076 * evaluation shaders, it has a different function there than it has in
1077 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1078 * as special geometry shader variables.
1080 * Note that although the general convention of suffixing geometry shader
1081 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1082 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1083 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1085 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
1086 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1087 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1088 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1093 * Generate variables which only exist in fragment shaders.
1096 builtin_variable_generator::generate_fs_special_vars()
1100 if (this->state
->ctx
->Const
.GLSLFragCoordIsSysVal
)
1101 add_system_value(SYSTEM_VALUE_FRAG_COORD
, vec4_t
, "gl_FragCoord");
1103 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
1105 if (this->state
->ctx
->Const
.GLSLFrontFacingIsSysVal
)
1106 add_system_value(SYSTEM_VALUE_FRONT_FACE
, bool_t
, "gl_FrontFacing");
1108 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
1110 if (state
->is_version(120, 100))
1111 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
1113 if (state
->has_geometry_shader()) {
1114 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1115 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1118 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1119 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1120 * They were removed from GLSL ES 3.00.
1122 if (compatibility
|| !state
->is_version(420, 300)) {
1123 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1124 add_output(FRAG_RESULT_DATA0
,
1125 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1128 if (state
->es_shader
&& state
->language_version
== 100 && state
->EXT_blend_func_extended_enable
) {
1129 /* We make an assumption here that there will only ever be one dual-source draw buffer
1130 * In case this assumption is ever proven to be false, make sure to assert here
1131 * since we don't handle this case.
1132 * In practice, this issue will never arise since no hardware will support it.
1134 assert(state
->Const
.MaxDualSourceDrawBuffers
<= 1);
1135 add_index_output(FRAG_RESULT_DATA0
, 1, vec4_t
, "gl_SecondaryFragColorEXT");
1136 add_index_output(FRAG_RESULT_DATA0
, 1,
1137 array(vec4_t
, state
->Const
.MaxDualSourceDrawBuffers
),
1138 "gl_SecondaryFragDataEXT");
1141 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1144 if (state
->is_version(110, 300))
1145 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1147 if (state
->ARB_shader_stencil_export_enable
) {
1148 ir_variable
*const var
=
1149 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1150 if (state
->ARB_shader_stencil_export_warn
)
1151 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1154 if (state
->AMD_shader_stencil_export_enable
) {
1155 ir_variable
*const var
=
1156 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1157 if (state
->AMD_shader_stencil_export_warn
)
1158 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1161 if (state
->is_version(400, 320) ||
1162 state
->ARB_sample_shading_enable
||
1163 state
->OES_sample_variables_enable
) {
1164 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1165 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1166 /* From the ARB_sample_shading specification:
1167 * "The number of elements in the array is ceil(<s>/32), where
1168 * <s> is the maximum number of color samples supported by the
1170 * Since no drivers expose more than 32x MSAA, we can simply set
1171 * the array size to 1 rather than computing it.
1173 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1176 if (state
->is_version(400, 320) ||
1177 state
->ARB_gpu_shader5_enable
||
1178 state
->OES_sample_variables_enable
) {
1179 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1182 if (state
->is_version(430, 0) || state
->ARB_fragment_layer_viewport_enable
) {
1183 var
= add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1184 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1185 var
= add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1186 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1189 if (state
->is_version(450, 310) || state
->ARB_ES3_1_compatibility_enable
)
1190 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION
, bool_t
, "gl_HelperInvocation");
1195 * Generate variables which only exist in compute shaders.
1198 builtin_variable_generator::generate_cs_special_vars()
1200 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1201 "gl_LocalInvocationID");
1202 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1203 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS
, uvec3_t
, "gl_NumWorkGroups");
1204 if (state
->ctx
->Const
.LowerCsDerivedVariables
) {
1205 add_variable("gl_GlobalInvocationID", uvec3_t
, ir_var_auto
, 0);
1206 add_variable("gl_LocalInvocationIndex", uint_t
, ir_var_auto
, 0);
1208 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID
,
1209 uvec3_t
, "gl_GlobalInvocationID");
1210 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
,
1211 uint_t
, "gl_LocalInvocationIndex");
1217 * Add a single "varying" variable. The variable's type and direction (input
1218 * or output) are adjusted as appropriate for the type of shader being
1222 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1225 switch (state
->stage
) {
1226 case MESA_SHADER_TESS_CTRL
:
1227 case MESA_SHADER_TESS_EVAL
:
1228 case MESA_SHADER_GEOMETRY
:
1229 this->per_vertex_in
.add_field(slot
, type
, name
);
1231 case MESA_SHADER_VERTEX
:
1232 this->per_vertex_out
.add_field(slot
, type
, name
);
1234 case MESA_SHADER_FRAGMENT
:
1235 add_input(slot
, type
, name
);
1237 case MESA_SHADER_COMPUTE
:
1238 /* Compute shaders don't have varyings. */
1245 * Generate variables that are used to communicate data from one shader stage
1246 * to the next ("varyings").
1249 builtin_variable_generator::generate_varyings()
1251 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1252 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1253 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1254 if (!state
->es_shader
||
1255 state
->stage
== MESA_SHADER_VERTEX
||
1256 (state
->stage
== MESA_SHADER_GEOMETRY
&&
1257 state
->OES_geometry_point_size_enable
)) {
1258 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1262 if (state
->has_clip_distance()) {
1263 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1266 if (state
->has_cull_distance()) {
1267 add_varying(VARYING_SLOT_CULL_DIST0
, array(float_t
, 0),
1271 if (compatibility
) {
1272 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1273 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1274 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1275 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1276 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1278 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1279 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1280 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1281 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1282 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1286 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1289 * "In the tessellation control language, built-in variables are
1290 * intrinsically declared as:
1294 * float gl_PointSize;
1295 * float gl_ClipDistance[];
1296 * } gl_in[gl_MaxPatchVertices];"
1298 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1299 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1300 const glsl_type
*per_vertex_in_type
=
1301 this->per_vertex_in
.construct_interface_instance();
1302 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1303 ir_var_shader_in
, -1);
1305 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1306 const glsl_type
*per_vertex_in_type
=
1307 this->per_vertex_in
.construct_interface_instance();
1308 add_variable("gl_in", array(per_vertex_in_type
, 0),
1309 ir_var_shader_in
, -1);
1311 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1312 const glsl_type
*per_vertex_out_type
=
1313 this->per_vertex_out
.construct_interface_instance();
1314 add_variable("gl_out", array(per_vertex_out_type
, 0),
1315 ir_var_shader_out
, -1);
1317 if (state
->stage
== MESA_SHADER_VERTEX
||
1318 state
->stage
== MESA_SHADER_TESS_EVAL
||
1319 state
->stage
== MESA_SHADER_GEOMETRY
) {
1320 const glsl_type
*per_vertex_out_type
=
1321 this->per_vertex_out
.construct_interface_instance();
1322 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1323 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1325 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1326 fields
[i
].location
);
1327 var
->data
.interpolation
= fields
[i
].interpolation
;
1328 var
->data
.centroid
= fields
[i
].centroid
;
1329 var
->data
.sample
= fields
[i
].sample
;
1330 var
->data
.patch
= fields
[i
].patch
;
1331 var
->data
.precision
= fields
[i
].precision
;
1332 var
->init_interface_type(per_vertex_out_type
);
1338 }; /* Anonymous namespace */
1342 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1343 struct _mesa_glsl_parse_state
*state
)
1345 builtin_variable_generator
gen(instructions
, state
);
1347 gen
.generate_constants();
1348 gen
.generate_uniforms();
1350 gen
.generate_varyings();
1352 switch (state
->stage
) {
1353 case MESA_SHADER_VERTEX
:
1354 gen
.generate_vs_special_vars();
1356 case MESA_SHADER_TESS_CTRL
:
1357 gen
.generate_tcs_special_vars();
1359 case MESA_SHADER_TESS_EVAL
:
1360 gen
.generate_tes_special_vars();
1362 case MESA_SHADER_GEOMETRY
:
1363 gen
.generate_gs_special_vars();
1365 case MESA_SHADER_FRAGMENT
:
1366 gen
.generate_fs_special_vars();
1368 case MESA_SHADER_COMPUTE
:
1369 gen
.generate_cs_special_vars();
1376 * Initialize compute shader variables with values that are derived from other
1377 * compute shader variable.
1380 initialize_cs_derived_variables(gl_shader
*shader
,
1381 ir_function_signature
*const main_sig
)
1383 assert(shader
->Stage
== MESA_SHADER_COMPUTE
);
1385 ir_variable
*gl_GlobalInvocationID
=
1386 shader
->symbols
->get_variable("gl_GlobalInvocationID");
1387 assert(gl_GlobalInvocationID
);
1388 ir_variable
*gl_WorkGroupID
=
1389 shader
->symbols
->get_variable("gl_WorkGroupID");
1390 assert(gl_WorkGroupID
);
1391 ir_variable
*gl_WorkGroupSize
=
1392 shader
->symbols
->get_variable("gl_WorkGroupSize");
1393 if (gl_WorkGroupSize
== NULL
) {
1394 void *const mem_ctx
= ralloc_parent(shader
->ir
);
1395 gl_WorkGroupSize
= new(mem_ctx
) ir_variable(glsl_type::uvec3_type
,
1398 gl_WorkGroupSize
->data
.how_declared
= ir_var_declared_implicitly
;
1399 gl_WorkGroupSize
->data
.read_only
= true;
1400 shader
->ir
->push_head(gl_WorkGroupSize
);
1402 ir_variable
*gl_LocalInvocationID
=
1403 shader
->symbols
->get_variable("gl_LocalInvocationID");
1404 assert(gl_LocalInvocationID
);
1406 /* gl_GlobalInvocationID =
1407 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1409 ir_instruction
*inst
=
1410 assign(gl_GlobalInvocationID
,
1411 add(mul(gl_WorkGroupID
, gl_WorkGroupSize
),
1412 gl_LocalInvocationID
));
1413 main_sig
->body
.push_head(inst
);
1415 /* gl_LocalInvocationIndex =
1416 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1417 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1418 * gl_LocalInvocationID.x;
1420 ir_expression
*index_z
=
1421 mul(mul(swizzle_z(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
)),
1422 swizzle_y(gl_WorkGroupSize
));
1423 ir_expression
*index_y
=
1424 mul(swizzle_y(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
));
1425 ir_expression
*index_y_plus_z
= add(index_y
, index_z
);
1426 operand
index_x(swizzle_x(gl_LocalInvocationID
));
1427 ir_expression
*index_x_plus_y_plus_z
= add(index_y_plus_z
, index_x
);
1428 ir_variable
*gl_LocalInvocationIndex
=
1429 shader
->symbols
->get_variable("gl_LocalInvocationIndex");
1430 assert(gl_LocalInvocationIndex
);
1431 inst
= assign(gl_LocalInvocationIndex
, index_x_plus_y_plus_z
);
1432 main_sig
->body
.push_head(inst
);
1437 * Initialize builtin variables with values based on other builtin variables.
1438 * These are initialized in the main function.
1441 _mesa_glsl_initialize_derived_variables(struct gl_context
*ctx
,
1444 /* We only need to set CS variables currently. */
1445 if (shader
->Stage
== MESA_SHADER_COMPUTE
&&
1446 ctx
->Const
.LowerCsDerivedVariables
) {
1447 ir_function_signature
*const main_sig
=
1448 _mesa_get_main_function_signature(shader
);
1450 if (main_sig
!= NULL
)
1451 initialize_cs_derived_variables(shader
, main_sig
);