2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "ir_builder.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_symbol_table.h"
29 #include "main/core.h"
30 #include "main/uniforms.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_instruction.h"
33 #include "builtin_functions.h"
35 using namespace ir_builder
;
37 static const struct gl_builtin_uniform_element gl_NumSamples_elements
[] = {
38 {NULL
, {STATE_NUM_SAMPLES
, 0, 0}, SWIZZLE_XXXX
}
42 static const struct gl_builtin_uniform_element gl_PatchVerticesIn_elements
[] = {
43 {NULL
, {STATE_INTERNAL
, STATE_TCS_PATCH_VERTICES_IN
}, SWIZZLE_XXXX
}
46 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
47 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
48 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
49 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
52 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
53 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
56 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
57 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
58 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
59 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
60 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
61 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
62 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
63 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
66 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
67 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
68 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
69 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
70 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
71 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
74 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
75 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
76 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
77 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
78 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
79 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
82 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
83 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
84 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
85 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
86 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
87 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
88 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
89 MAKE_SWIZZLE4(SWIZZLE_X
,
93 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
94 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
95 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
96 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
97 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
98 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
101 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
102 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
105 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
106 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
109 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
110 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
113 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
114 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
115 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
116 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
119 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
120 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
121 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
122 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
125 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
126 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
129 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
130 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
133 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
134 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
137 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
138 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
141 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
142 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
145 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
146 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
149 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
150 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
153 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
154 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
157 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
158 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
161 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
162 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
163 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
164 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
165 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
166 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
169 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
170 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
173 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
174 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
177 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
178 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
181 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
182 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
185 #define MATRIX(name, statevar, modifier) \
186 static const struct gl_builtin_uniform_element name ## _elements[] = { \
187 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
188 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
189 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
190 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
193 MATRIX(gl_ModelViewMatrix
,
194 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
195 MATRIX(gl_ModelViewMatrixInverse
,
196 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
197 MATRIX(gl_ModelViewMatrixTranspose
,
198 STATE_MODELVIEW_MATRIX
, 0);
199 MATRIX(gl_ModelViewMatrixInverseTranspose
,
200 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
202 MATRIX(gl_ProjectionMatrix
,
203 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
204 MATRIX(gl_ProjectionMatrixInverse
,
205 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
206 MATRIX(gl_ProjectionMatrixTranspose
,
207 STATE_PROJECTION_MATRIX
, 0);
208 MATRIX(gl_ProjectionMatrixInverseTranspose
,
209 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
211 MATRIX(gl_ModelViewProjectionMatrix
,
212 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
213 MATRIX(gl_ModelViewProjectionMatrixInverse
,
214 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
215 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
216 STATE_MVP_MATRIX
, 0);
217 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
218 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
220 MATRIX(gl_TextureMatrix
,
221 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
222 MATRIX(gl_TextureMatrixInverse
,
223 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
224 MATRIX(gl_TextureMatrixTranspose
,
225 STATE_TEXTURE_MATRIX
, 0);
226 MATRIX(gl_TextureMatrixInverseTranspose
,
227 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
229 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
230 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
231 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
232 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
233 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
234 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
235 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
240 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
242 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
243 STATEVAR(gl_PatchVerticesIn
),
244 STATEVAR(gl_NumSamples
),
245 STATEVAR(gl_DepthRange
),
246 STATEVAR(gl_ClipPlane
),
248 STATEVAR(gl_FrontMaterial
),
249 STATEVAR(gl_BackMaterial
),
250 STATEVAR(gl_LightSource
),
251 STATEVAR(gl_LightModel
),
252 STATEVAR(gl_FrontLightModelProduct
),
253 STATEVAR(gl_BackLightModelProduct
),
254 STATEVAR(gl_FrontLightProduct
),
255 STATEVAR(gl_BackLightProduct
),
256 STATEVAR(gl_TextureEnvColor
),
257 STATEVAR(gl_EyePlaneS
),
258 STATEVAR(gl_EyePlaneT
),
259 STATEVAR(gl_EyePlaneR
),
260 STATEVAR(gl_EyePlaneQ
),
261 STATEVAR(gl_ObjectPlaneS
),
262 STATEVAR(gl_ObjectPlaneT
),
263 STATEVAR(gl_ObjectPlaneR
),
264 STATEVAR(gl_ObjectPlaneQ
),
267 STATEVAR(gl_ModelViewMatrix
),
268 STATEVAR(gl_ModelViewMatrixInverse
),
269 STATEVAR(gl_ModelViewMatrixTranspose
),
270 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
272 STATEVAR(gl_ProjectionMatrix
),
273 STATEVAR(gl_ProjectionMatrixInverse
),
274 STATEVAR(gl_ProjectionMatrixTranspose
),
275 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
277 STATEVAR(gl_ModelViewProjectionMatrix
),
278 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
279 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
280 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
282 STATEVAR(gl_TextureMatrix
),
283 STATEVAR(gl_TextureMatrixInverse
),
284 STATEVAR(gl_TextureMatrixTranspose
),
285 STATEVAR(gl_TextureMatrixInverseTranspose
),
287 STATEVAR(gl_NormalMatrix
),
288 STATEVAR(gl_NormalScale
),
290 STATEVAR(gl_FogParamsOptimizedMESA
),
291 STATEVAR(gl_CurrentAttribVertMESA
),
292 STATEVAR(gl_CurrentAttribFragMESA
),
301 * Data structure that accumulates fields for the gl_PerVertex interface
304 class per_vertex_accumulator
307 per_vertex_accumulator();
308 void add_field(int slot
, const glsl_type
*type
, const char *name
);
309 const glsl_type
*construct_interface_instance() const;
312 glsl_struct_field fields
[11];
317 per_vertex_accumulator::per_vertex_accumulator()
325 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
328 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
329 this->fields
[this->num_fields
].type
= type
;
330 this->fields
[this->num_fields
].name
= name
;
331 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
332 this->fields
[this->num_fields
].location
= slot
;
333 this->fields
[this->num_fields
].offset
= -1;
334 this->fields
[this->num_fields
].interpolation
= INTERP_MODE_NONE
;
335 this->fields
[this->num_fields
].centroid
= 0;
336 this->fields
[this->num_fields
].sample
= 0;
337 this->fields
[this->num_fields
].patch
= 0;
338 this->fields
[this->num_fields
].precision
= GLSL_PRECISION_NONE
;
339 this->fields
[this->num_fields
].memory_read_only
= 0;
340 this->fields
[this->num_fields
].memory_write_only
= 0;
341 this->fields
[this->num_fields
].memory_coherent
= 0;
342 this->fields
[this->num_fields
].memory_volatile
= 0;
343 this->fields
[this->num_fields
].memory_restrict
= 0;
344 this->fields
[this->num_fields
].image_format
= 0;
345 this->fields
[this->num_fields
].explicit_xfb_buffer
= 0;
346 this->fields
[this->num_fields
].xfb_buffer
= -1;
347 this->fields
[this->num_fields
].xfb_stride
= -1;
353 per_vertex_accumulator::construct_interface_instance() const
355 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
356 GLSL_INTERFACE_PACKING_STD140
,
362 class builtin_variable_generator
365 builtin_variable_generator(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
);
367 void generate_constants();
368 void generate_uniforms();
369 void generate_special_vars();
370 void generate_vs_special_vars();
371 void generate_tcs_special_vars();
372 void generate_tes_special_vars();
373 void generate_gs_special_vars();
374 void generate_fs_special_vars();
375 void generate_cs_special_vars();
376 void generate_varyings();
379 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
381 return glsl_type::get_array_instance(base
, elements
);
384 const glsl_type
*type(const char *name
)
386 return symtab
->get_type(name
);
389 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
391 return add_variable(name
, type
, ir_var_shader_in
, slot
);
394 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
396 return add_variable(name
, type
, ir_var_shader_out
, slot
);
399 ir_variable
*add_index_output(int slot
, int index
, const glsl_type
*type
, const char *name
)
401 return add_index_variable(name
, type
, ir_var_shader_out
, slot
, index
);
404 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
407 return add_variable(name
, type
, ir_var_system_value
, slot
);
410 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
411 enum ir_variable_mode mode
, int slot
);
412 ir_variable
*add_index_variable(const char *name
, const glsl_type
*type
,
413 enum ir_variable_mode mode
, int slot
, int index
);
414 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
415 ir_variable
*add_const(const char *name
, int value
);
416 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
417 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
419 exec_list
* const instructions
;
420 struct _mesa_glsl_parse_state
* const state
;
421 glsl_symbol_table
* const symtab
;
424 * True if compatibility-profile-only variables should be included. (In
425 * desktop GL, these are always included when the GLSL version is 1.30 and
428 const bool compatibility
;
430 const glsl_type
* const bool_t
;
431 const glsl_type
* const int_t
;
432 const glsl_type
* const uint_t
;
433 const glsl_type
* const uint64_t;
434 const glsl_type
* const float_t
;
435 const glsl_type
* const vec2_t
;
436 const glsl_type
* const vec3_t
;
437 const glsl_type
* const vec4_t
;
438 const glsl_type
* const uvec3_t
;
439 const glsl_type
* const mat3_t
;
440 const glsl_type
* const mat4_t
;
442 per_vertex_accumulator per_vertex_in
;
443 per_vertex_accumulator per_vertex_out
;
447 builtin_variable_generator::builtin_variable_generator(
448 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
449 : instructions(instructions
), state(state
), symtab(state
->symbols
),
450 compatibility(state
->compat_shader
|| !state
->is_version(140, 100)),
451 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
452 uint_t(glsl_type::uint_type
),
453 uint64_t(glsl_type::uint64_t_type
),
454 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
455 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
456 uvec3_t(glsl_type::uvec3_type
),
457 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
462 builtin_variable_generator::add_index_variable(const char *name
,
463 const glsl_type
*type
,
464 enum ir_variable_mode mode
, int slot
, int index
)
466 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
467 var
->data
.how_declared
= ir_var_declared_implicitly
;
469 switch (var
->data
.mode
) {
471 case ir_var_shader_in
:
473 case ir_var_system_value
:
474 var
->data
.read_only
= true;
476 case ir_var_shader_out
:
477 case ir_var_shader_storage
:
480 /* The only variables that are added using this function should be
481 * uniforms, shader storage, shader inputs, and shader outputs, constants
482 * (which use ir_var_auto), and system values.
488 var
->data
.location
= slot
;
489 var
->data
.explicit_location
= (slot
>= 0);
490 var
->data
.explicit_index
= 1;
491 var
->data
.index
= index
;
493 /* Once the variable is created an initialized, add it to the symbol table
494 * and add the declaration to the IR stream.
496 instructions
->push_tail(var
);
498 symtab
->add_variable(var
);
503 builtin_variable_generator::add_variable(const char *name
,
504 const glsl_type
*type
,
505 enum ir_variable_mode mode
, int slot
)
507 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
508 var
->data
.how_declared
= ir_var_declared_implicitly
;
510 switch (var
->data
.mode
) {
512 case ir_var_shader_in
:
514 case ir_var_system_value
:
515 var
->data
.read_only
= true;
517 case ir_var_shader_out
:
518 case ir_var_shader_storage
:
521 /* The only variables that are added using this function should be
522 * uniforms, shader storage, shader inputs, and shader outputs, constants
523 * (which use ir_var_auto), and system values.
529 var
->data
.location
= slot
;
530 var
->data
.explicit_location
= (slot
>= 0);
531 var
->data
.explicit_index
= 0;
533 /* Once the variable is created an initialized, add it to the symbol table
534 * and add the declaration to the IR stream.
536 instructions
->push_tail(var
);
538 symtab
->add_variable(var
);
542 extern "C" const struct gl_builtin_uniform_desc
*
543 _mesa_glsl_get_builtin_uniform_desc(const char *name
)
545 for (unsigned i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
546 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
547 return &_mesa_builtin_uniform_desc
[i
];
554 builtin_variable_generator::add_uniform(const glsl_type
*type
,
557 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
559 const struct gl_builtin_uniform_desc
* const statevar
=
560 _mesa_glsl_get_builtin_uniform_desc(name
);
561 assert(statevar
!= NULL
);
563 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
565 ir_state_slot
*slots
=
566 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
568 for (unsigned a
= 0; a
< array_count
; a
++) {
569 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
570 const struct gl_builtin_uniform_element
*element
=
571 &statevar
->elements
[j
];
573 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
574 if (type
->is_array()) {
575 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
576 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
577 slots
->tokens
[2] = a
;
579 slots
->tokens
[1] = a
;
583 slots
->swizzle
= element
->swizzle
;
593 builtin_variable_generator::add_const(const char *name
, int value
)
595 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
597 var
->constant_value
= new(var
) ir_constant(value
);
598 var
->constant_initializer
= new(var
) ir_constant(value
);
599 var
->data
.has_initializer
= true;
605 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
608 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
610 ir_constant_data data
;
611 memset(&data
, 0, sizeof(data
));
615 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
616 var
->constant_initializer
=
617 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
618 var
->data
.has_initializer
= true;
624 builtin_variable_generator::generate_constants()
626 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
627 add_const("gl_MaxVertexTextureImageUnits",
628 state
->Const
.MaxVertexTextureImageUnits
);
629 add_const("gl_MaxCombinedTextureImageUnits",
630 state
->Const
.MaxCombinedTextureImageUnits
);
631 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
632 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
634 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
635 * GL counts them in units of "components" or "floats" and also in units
636 * of vectors since GL 4.1
638 if (!state
->es_shader
) {
639 add_const("gl_MaxFragmentUniformComponents",
640 state
->Const
.MaxFragmentUniformComponents
);
641 add_const("gl_MaxVertexUniformComponents",
642 state
->Const
.MaxVertexUniformComponents
);
645 if (state
->is_version(410, 100)) {
646 add_const("gl_MaxVertexUniformVectors",
647 state
->Const
.MaxVertexUniformComponents
/ 4);
648 add_const("gl_MaxFragmentUniformVectors",
649 state
->Const
.MaxFragmentUniformComponents
/ 4);
651 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
652 * vertex and fragment shader constants.
654 if (state
->is_version(0, 300)) {
655 add_const("gl_MaxVertexOutputVectors",
656 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
657 add_const("gl_MaxFragmentInputVectors",
658 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
660 add_const("gl_MaxVaryingVectors",
661 state
->ctx
->Const
.MaxVarying
);
664 /* EXT_blend_func_extended brings a built in constant
665 * for determining number of dual source draw buffers
667 if (state
->EXT_blend_func_extended_enable
) {
668 add_const("gl_MaxDualSourceDrawBuffersEXT",
669 state
->Const
.MaxDualSourceDrawBuffers
);
672 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
675 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
678 /* Texel offsets were introduced in ARB_shading_language_420pack (which
679 * requires desktop GLSL version 130), and adopted into desktop GLSL
680 * version 4.20 and GLSL ES version 3.00.
682 if ((state
->is_version(130, 0) &&
683 state
->ARB_shading_language_420pack_enable
) ||
684 state
->is_version(420, 300)) {
685 add_const("gl_MinProgramTexelOffset",
686 state
->Const
.MinProgramTexelOffset
);
687 add_const("gl_MaxProgramTexelOffset",
688 state
->Const
.MaxProgramTexelOffset
);
691 if (state
->has_clip_distance()) {
692 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
694 if (state
->is_version(130, 0)) {
695 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
697 if (state
->has_cull_distance()) {
698 add_const("gl_MaxCullDistances", state
->Const
.MaxClipPlanes
);
699 add_const("gl_MaxCombinedClipAndCullDistances",
700 state
->Const
.MaxClipPlanes
);
703 if (state
->has_geometry_shader()) {
704 add_const("gl_MaxVertexOutputComponents",
705 state
->Const
.MaxVertexOutputComponents
);
706 add_const("gl_MaxGeometryInputComponents",
707 state
->Const
.MaxGeometryInputComponents
);
708 add_const("gl_MaxGeometryOutputComponents",
709 state
->Const
.MaxGeometryOutputComponents
);
710 add_const("gl_MaxFragmentInputComponents",
711 state
->Const
.MaxFragmentInputComponents
);
712 add_const("gl_MaxGeometryTextureImageUnits",
713 state
->Const
.MaxGeometryTextureImageUnits
);
714 add_const("gl_MaxGeometryOutputVertices",
715 state
->Const
.MaxGeometryOutputVertices
);
716 add_const("gl_MaxGeometryTotalOutputComponents",
717 state
->Const
.MaxGeometryTotalOutputComponents
);
718 add_const("gl_MaxGeometryUniformComponents",
719 state
->Const
.MaxGeometryUniformComponents
);
721 /* Note: the GLSL 1.50-4.40 specs require
722 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
723 * But they do not define what it means (and there does not appear to be
724 * any corresponding constant in the GL specs). However,
725 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
726 * be the maximum number of components available for use as geometry
727 * outputs. So we assume this is a synonym for
728 * gl_MaxGeometryOutputComponents.
730 add_const("gl_MaxGeometryVaryingComponents",
731 state
->Const
.MaxGeometryOutputComponents
);
735 /* Note: gl_MaxLights stopped being listed as an explicit constant in
736 * GLSL 1.30, however it continues to be referred to (as a minimum size
737 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
738 * this seems like it was probably an oversight.
740 add_const("gl_MaxLights", state
->Const
.MaxLights
);
742 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
744 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
745 * 1.50, however this seems like it was probably an oversight.
747 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
749 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
750 * re-introduced in GLSL 1.50, so this seems like it was probably an
753 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
756 if (state
->has_atomic_counters()) {
757 add_const("gl_MaxVertexAtomicCounters",
758 state
->Const
.MaxVertexAtomicCounters
);
759 add_const("gl_MaxFragmentAtomicCounters",
760 state
->Const
.MaxFragmentAtomicCounters
);
761 add_const("gl_MaxCombinedAtomicCounters",
762 state
->Const
.MaxCombinedAtomicCounters
);
763 add_const("gl_MaxAtomicCounterBindings",
764 state
->Const
.MaxAtomicBufferBindings
);
766 if (state
->has_geometry_shader()) {
767 add_const("gl_MaxGeometryAtomicCounters",
768 state
->Const
.MaxGeometryAtomicCounters
);
770 if (state
->is_version(110, 320)) {
771 add_const("gl_MaxTessControlAtomicCounters",
772 state
->Const
.MaxTessControlAtomicCounters
);
773 add_const("gl_MaxTessEvaluationAtomicCounters",
774 state
->Const
.MaxTessEvaluationAtomicCounters
);
778 if (state
->is_version(420, 310)) {
779 add_const("gl_MaxVertexAtomicCounterBuffers",
780 state
->Const
.MaxVertexAtomicCounterBuffers
);
781 add_const("gl_MaxFragmentAtomicCounterBuffers",
782 state
->Const
.MaxFragmentAtomicCounterBuffers
);
783 add_const("gl_MaxCombinedAtomicCounterBuffers",
784 state
->Const
.MaxCombinedAtomicCounterBuffers
);
785 add_const("gl_MaxAtomicCounterBufferSize",
786 state
->Const
.MaxAtomicCounterBufferSize
);
788 if (state
->has_geometry_shader()) {
789 add_const("gl_MaxGeometryAtomicCounterBuffers",
790 state
->Const
.MaxGeometryAtomicCounterBuffers
);
792 if (state
->is_version(110, 320)) {
793 add_const("gl_MaxTessControlAtomicCounterBuffers",
794 state
->Const
.MaxTessControlAtomicCounterBuffers
);
795 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
796 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
800 if (state
->is_version(430, 310) || state
->ARB_compute_shader_enable
) {
801 add_const("gl_MaxComputeAtomicCounterBuffers",
802 state
->Const
.MaxComputeAtomicCounterBuffers
);
803 add_const("gl_MaxComputeAtomicCounters",
804 state
->Const
.MaxComputeAtomicCounters
);
805 add_const("gl_MaxComputeImageUniforms",
806 state
->Const
.MaxComputeImageUniforms
);
807 add_const("gl_MaxComputeTextureImageUnits",
808 state
->Const
.MaxComputeTextureImageUnits
);
809 add_const("gl_MaxComputeUniformComponents",
810 state
->Const
.MaxComputeUniformComponents
);
812 add_const_ivec3("gl_MaxComputeWorkGroupCount",
813 state
->Const
.MaxComputeWorkGroupCount
[0],
814 state
->Const
.MaxComputeWorkGroupCount
[1],
815 state
->Const
.MaxComputeWorkGroupCount
[2]);
816 add_const_ivec3("gl_MaxComputeWorkGroupSize",
817 state
->Const
.MaxComputeWorkGroupSize
[0],
818 state
->Const
.MaxComputeWorkGroupSize
[1],
819 state
->Const
.MaxComputeWorkGroupSize
[2]);
821 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
823 * The built-in constant gl_WorkGroupSize is a compute-shader
824 * constant containing the local work-group size of the shader. The
825 * size of the work group in the X, Y, and Z dimensions is stored in
826 * the x, y, and z components. The constants values in
827 * gl_WorkGroupSize will match those specified in the required
828 * local_size_x, local_size_y, and local_size_z layout qualifiers
829 * for the current shader. This is a constant so that it can be
830 * used to size arrays of memory that can be shared within the local
831 * work group. It is a compile-time error to use gl_WorkGroupSize
832 * in a shader that does not declare a fixed local group size, or
833 * before that shader has declared a fixed local group size, using
834 * local_size_x, local_size_y, and local_size_z.
836 * To prevent the shader from trying to refer to gl_WorkGroupSize before
837 * the layout declaration, we don't define it here. Intead we define it
838 * in ast_cs_input_layout::hir().
842 if (state
->has_enhanced_layouts()) {
843 add_const("gl_MaxTransformFeedbackBuffers",
844 state
->Const
.MaxTransformFeedbackBuffers
);
845 add_const("gl_MaxTransformFeedbackInterleavedComponents",
846 state
->Const
.MaxTransformFeedbackInterleavedComponents
);
849 if (state
->has_shader_image_load_store()) {
850 add_const("gl_MaxImageUnits",
851 state
->Const
.MaxImageUnits
);
852 add_const("gl_MaxVertexImageUniforms",
853 state
->Const
.MaxVertexImageUniforms
);
854 add_const("gl_MaxFragmentImageUniforms",
855 state
->Const
.MaxFragmentImageUniforms
);
856 add_const("gl_MaxCombinedImageUniforms",
857 state
->Const
.MaxCombinedImageUniforms
);
859 if (state
->has_geometry_shader()) {
860 add_const("gl_MaxGeometryImageUniforms",
861 state
->Const
.MaxGeometryImageUniforms
);
864 if (!state
->es_shader
) {
865 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
866 state
->Const
.MaxCombinedShaderOutputResources
);
867 add_const("gl_MaxImageSamples",
868 state
->Const
.MaxImageSamples
);
871 if (state
->has_tessellation_shader()) {
872 add_const("gl_MaxTessControlImageUniforms",
873 state
->Const
.MaxTessControlImageUniforms
);
874 add_const("gl_MaxTessEvaluationImageUniforms",
875 state
->Const
.MaxTessEvaluationImageUniforms
);
879 if (state
->is_version(440, 310) ||
880 state
->ARB_ES3_1_compatibility_enable
) {
881 add_const("gl_MaxCombinedShaderOutputResources",
882 state
->Const
.MaxCombinedShaderOutputResources
);
885 if (state
->is_version(410, 0) ||
886 state
->ARB_viewport_array_enable
||
887 state
->OES_viewport_array_enable
)
888 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
890 if (state
->has_tessellation_shader()) {
891 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
892 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
893 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
894 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
895 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
896 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
897 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
898 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
899 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
900 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
901 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
902 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
905 if (state
->is_version(450, 320) ||
906 state
->OES_sample_variables_enable
||
907 state
->ARB_ES3_1_compatibility_enable
)
908 add_const("gl_MaxSamples", state
->Const
.MaxSamples
);
913 * Generate uniform variables (which exist in all types of shaders).
916 builtin_variable_generator::generate_uniforms()
918 if (state
->is_version(400, 320) ||
919 state
->ARB_sample_shading_enable
||
920 state
->OES_sample_variables_enable
)
921 add_uniform(int_t
, "gl_NumSamples");
922 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
923 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
924 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
927 add_uniform(mat4_t
, "gl_ModelViewMatrix");
928 add_uniform(mat4_t
, "gl_ProjectionMatrix");
929 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
930 add_uniform(mat3_t
, "gl_NormalMatrix");
931 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
932 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
933 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
934 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
935 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
936 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
937 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
938 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
939 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
940 add_uniform(float_t
, "gl_NormalScale");
941 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
942 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
944 const glsl_type
*const mat4_array_type
=
945 array(mat4_t
, state
->Const
.MaxTextureCoords
);
946 add_uniform(mat4_array_type
, "gl_TextureMatrix");
947 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
948 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
949 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
951 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
952 add_uniform(type("gl_PointParameters"), "gl_Point");
954 const glsl_type
*const material_parameters_type
=
955 type("gl_MaterialParameters");
956 add_uniform(material_parameters_type
, "gl_FrontMaterial");
957 add_uniform(material_parameters_type
, "gl_BackMaterial");
959 add_uniform(array(type("gl_LightSourceParameters"),
960 state
->Const
.MaxLights
),
963 const glsl_type
*const light_model_products_type
=
964 type("gl_LightModelProducts");
965 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
966 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
968 const glsl_type
*const light_products_type
=
969 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
970 add_uniform(light_products_type
, "gl_FrontLightProduct");
971 add_uniform(light_products_type
, "gl_BackLightProduct");
973 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
974 "gl_TextureEnvColor");
976 const glsl_type
*const texcoords_vec4
=
977 array(vec4_t
, state
->Const
.MaxTextureCoords
);
978 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
979 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
980 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
981 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
982 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
983 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
984 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
985 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
987 add_uniform(type("gl_FogParameters"), "gl_Fog");
993 * Generate special variables which exist in all shaders.
996 builtin_variable_generator::generate_special_vars()
998 if (state
->ARB_shader_ballot_enable
) {
999 add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE
, uint_t
, "gl_SubGroupSizeARB");
1000 add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION
, uint_t
, "gl_SubGroupInvocationARB");
1001 add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK
, uint64_t, "gl_SubGroupEqMaskARB");
1002 add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK
, uint64_t, "gl_SubGroupGeMaskARB");
1003 add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK
, uint64_t, "gl_SubGroupGtMaskARB");
1004 add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK
, uint64_t, "gl_SubGroupLeMaskARB");
1005 add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK
, uint64_t, "gl_SubGroupLtMaskARB");
1011 * Generate variables which only exist in vertex shaders.
1014 builtin_variable_generator::generate_vs_special_vars()
1018 if (state
->is_version(130, 300))
1019 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
1020 if (state
->ARB_draw_instanced_enable
)
1021 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
1022 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
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");
1066 if (state
->ctx
->Const
.LowerTCSPatchVerticesIn
) {
1067 add_uniform(int_t
, "gl_PatchVerticesIn");
1069 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1072 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1073 "gl_TessLevelOuter")->data
.patch
= 1;
1074 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1075 "gl_TessLevelInner")->data
.patch
= 1;
1076 /* XXX What to do if multiple are flipped on? */
1077 int bbox_slot
= state
->ctx
->Const
.NoPrimitiveBoundingBoxOutput
? -1 :
1078 VARYING_SLOT_BOUNDING_BOX0
;
1079 if (state
->EXT_primitive_bounding_box_enable
)
1080 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxEXT")
1082 if (state
->OES_primitive_bounding_box_enable
)
1083 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBoxOES")
1085 if (state
->is_version(0, 320) || state
->ARB_ES3_2_compatibility_enable
)
1086 add_output(bbox_slot
, array(vec4_t
, 2), "gl_BoundingBox")
1092 * Generate variables which only exist in tessellation evaluation shaders.
1095 builtin_variable_generator::generate_tes_special_vars()
1099 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1100 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
1101 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
1102 if (this->state
->ctx
->Const
.GLSLTessLevelsAsInputs
) {
1103 add_input(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
1104 "gl_TessLevelOuter")->data
.patch
= 1;
1105 add_input(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
1106 "gl_TessLevelInner")->data
.patch
= 1;
1108 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
1109 "gl_TessLevelOuter");
1110 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
1111 "gl_TessLevelInner");
1113 if (state
->ARB_shader_viewport_layer_array_enable
) {
1114 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1115 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1116 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1117 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1123 * Generate variables which only exist in geometry shaders.
1126 builtin_variable_generator::generate_gs_special_vars()
1130 var
= add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1131 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1132 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
||
1133 state
->OES_viewport_array_enable
) {
1134 var
= add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1135 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1137 if (state
->is_version(400, 320) || state
->ARB_gpu_shader5_enable
||
1138 state
->OES_geometry_shader_enable
|| state
->EXT_geometry_shader_enable
) {
1139 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
1142 /* Although gl_PrimitiveID appears in tessellation control and tessellation
1143 * evaluation shaders, it has a different function there than it has in
1144 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
1145 * as special geometry shader variables.
1147 * Note that although the general convention of suffixing geometry shader
1148 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
1149 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
1150 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
1152 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
1153 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1154 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1155 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1160 * Generate variables which only exist in fragment shaders.
1163 builtin_variable_generator::generate_fs_special_vars()
1167 if (this->state
->ctx
->Const
.GLSLFragCoordIsSysVal
)
1168 add_system_value(SYSTEM_VALUE_FRAG_COORD
, vec4_t
, "gl_FragCoord");
1170 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
1172 if (this->state
->ctx
->Const
.GLSLFrontFacingIsSysVal
)
1173 add_system_value(SYSTEM_VALUE_FRONT_FACE
, bool_t
, "gl_FrontFacing");
1175 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
1177 if (state
->is_version(120, 100))
1178 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
1180 if (state
->has_geometry_shader()) {
1181 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
1182 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1185 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1186 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1187 * They were removed from GLSL ES 3.00.
1189 if (compatibility
|| !state
->is_version(420, 300)) {
1190 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1191 add_output(FRAG_RESULT_DATA0
,
1192 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1195 if (state
->has_framebuffer_fetch() && !state
->is_version(130, 300)) {
1196 ir_variable
*const var
=
1197 add_output(FRAG_RESULT_DATA0
,
1198 array(vec4_t
, state
->Const
.MaxDrawBuffers
),
1200 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
1201 var
->data
.read_only
= 1;
1202 var
->data
.fb_fetch_output
= 1;
1205 if (state
->es_shader
&& state
->language_version
== 100 && state
->EXT_blend_func_extended_enable
) {
1206 add_index_output(FRAG_RESULT_COLOR
, 1, vec4_t
,
1207 "gl_SecondaryFragColorEXT");
1208 add_index_output(FRAG_RESULT_DATA0
, 1,
1209 array(vec4_t
, state
->Const
.MaxDualSourceDrawBuffers
),
1210 "gl_SecondaryFragDataEXT");
1213 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1216 if (state
->is_version(110, 300))
1217 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1219 if (state
->EXT_frag_depth_enable
)
1220 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepthEXT");
1222 if (state
->ARB_shader_stencil_export_enable
) {
1223 ir_variable
*const var
=
1224 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1225 if (state
->ARB_shader_stencil_export_warn
)
1226 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1229 if (state
->AMD_shader_stencil_export_enable
) {
1230 ir_variable
*const var
=
1231 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1232 if (state
->AMD_shader_stencil_export_warn
)
1233 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1236 if (state
->is_version(400, 320) ||
1237 state
->ARB_sample_shading_enable
||
1238 state
->OES_sample_variables_enable
) {
1239 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1240 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1241 /* From the ARB_sample_shading specification:
1242 * "The number of elements in the array is ceil(<s>/32), where
1243 * <s> is the maximum number of color samples supported by the
1245 * Since no drivers expose more than 32x MSAA, we can simply set
1246 * the array size to 1 rather than computing it.
1248 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1251 if (state
->is_version(400, 320) ||
1252 state
->ARB_gpu_shader5_enable
||
1253 state
->OES_sample_variables_enable
) {
1254 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1257 if (state
->is_version(430, 320) ||
1258 state
->ARB_fragment_layer_viewport_enable
||
1259 state
->OES_geometry_shader_enable
||
1260 state
->EXT_geometry_shader_enable
) {
1261 var
= add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1262 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1265 if (state
->is_version(430, 0) ||
1266 state
->ARB_fragment_layer_viewport_enable
||
1267 state
->OES_viewport_array_enable
) {
1268 var
= add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1269 var
->data
.interpolation
= INTERP_MODE_FLAT
;
1272 if (state
->is_version(450, 310) || state
->ARB_ES3_1_compatibility_enable
)
1273 add_system_value(SYSTEM_VALUE_HELPER_INVOCATION
, bool_t
, "gl_HelperInvocation");
1278 * Generate variables which only exist in compute shaders.
1281 builtin_variable_generator::generate_cs_special_vars()
1283 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1284 "gl_LocalInvocationID");
1285 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1286 add_system_value(SYSTEM_VALUE_NUM_WORK_GROUPS
, uvec3_t
, "gl_NumWorkGroups");
1288 if (state
->ARB_compute_variable_group_size_enable
) {
1289 add_system_value(SYSTEM_VALUE_LOCAL_GROUP_SIZE
,
1290 uvec3_t
, "gl_LocalGroupSizeARB");
1293 if (state
->ctx
->Const
.LowerCsDerivedVariables
) {
1294 add_variable("gl_GlobalInvocationID", uvec3_t
, ir_var_auto
, 0);
1295 add_variable("gl_LocalInvocationIndex", uint_t
, ir_var_auto
, 0);
1297 add_system_value(SYSTEM_VALUE_GLOBAL_INVOCATION_ID
,
1298 uvec3_t
, "gl_GlobalInvocationID");
1299 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
,
1300 uint_t
, "gl_LocalInvocationIndex");
1306 * Add a single "varying" variable. The variable's type and direction (input
1307 * or output) are adjusted as appropriate for the type of shader being
1311 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1314 switch (state
->stage
) {
1315 case MESA_SHADER_TESS_CTRL
:
1316 case MESA_SHADER_TESS_EVAL
:
1317 case MESA_SHADER_GEOMETRY
:
1318 this->per_vertex_in
.add_field(slot
, type
, name
);
1320 case MESA_SHADER_VERTEX
:
1321 this->per_vertex_out
.add_field(slot
, type
, name
);
1323 case MESA_SHADER_FRAGMENT
:
1324 add_input(slot
, type
, name
);
1326 case MESA_SHADER_COMPUTE
:
1327 /* Compute shaders don't have varyings. */
1336 * Generate variables that are used to communicate data from one shader stage
1337 * to the next ("varyings").
1340 builtin_variable_generator::generate_varyings()
1342 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1343 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1344 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1345 if (!state
->es_shader
||
1346 state
->stage
== MESA_SHADER_VERTEX
||
1347 (state
->stage
== MESA_SHADER_GEOMETRY
&&
1348 (state
->OES_geometry_point_size_enable
||
1349 state
->EXT_geometry_point_size_enable
)) ||
1350 ((state
->stage
== MESA_SHADER_TESS_CTRL
||
1351 state
->stage
== MESA_SHADER_TESS_EVAL
) &&
1352 (state
->OES_tessellation_point_size_enable
||
1353 state
->EXT_tessellation_point_size_enable
))) {
1354 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1358 if (state
->has_clip_distance()) {
1359 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1362 if (state
->has_cull_distance()) {
1363 add_varying(VARYING_SLOT_CULL_DIST0
, array(float_t
, 0),
1367 if (compatibility
) {
1368 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1369 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1370 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1371 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1372 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1374 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1375 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1376 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1377 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1378 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1382 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1385 * "In the tessellation control language, built-in variables are
1386 * intrinsically declared as:
1390 * float gl_PointSize;
1391 * float gl_ClipDistance[];
1392 * } gl_in[gl_MaxPatchVertices];"
1394 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1395 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1396 const glsl_type
*per_vertex_in_type
=
1397 this->per_vertex_in
.construct_interface_instance();
1398 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1399 ir_var_shader_in
, -1);
1401 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1402 const glsl_type
*per_vertex_in_type
=
1403 this->per_vertex_in
.construct_interface_instance();
1404 add_variable("gl_in", array(per_vertex_in_type
, 0),
1405 ir_var_shader_in
, -1);
1407 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1408 const glsl_type
*per_vertex_out_type
=
1409 this->per_vertex_out
.construct_interface_instance();
1410 add_variable("gl_out", array(per_vertex_out_type
, 0),
1411 ir_var_shader_out
, -1);
1413 if (state
->stage
== MESA_SHADER_VERTEX
||
1414 state
->stage
== MESA_SHADER_TESS_EVAL
||
1415 state
->stage
== MESA_SHADER_GEOMETRY
) {
1416 const glsl_type
*per_vertex_out_type
=
1417 this->per_vertex_out
.construct_interface_instance();
1418 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1419 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1421 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1422 fields
[i
].location
);
1423 var
->data
.interpolation
= fields
[i
].interpolation
;
1424 var
->data
.centroid
= fields
[i
].centroid
;
1425 var
->data
.sample
= fields
[i
].sample
;
1426 var
->data
.patch
= fields
[i
].patch
;
1427 var
->data
.precision
= fields
[i
].precision
;
1428 var
->init_interface_type(per_vertex_out_type
);
1434 }; /* Anonymous namespace */
1438 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1439 struct _mesa_glsl_parse_state
*state
)
1441 builtin_variable_generator
gen(instructions
, state
);
1443 gen
.generate_constants();
1444 gen
.generate_uniforms();
1445 gen
.generate_special_vars();
1447 gen
.generate_varyings();
1449 switch (state
->stage
) {
1450 case MESA_SHADER_VERTEX
:
1451 gen
.generate_vs_special_vars();
1453 case MESA_SHADER_TESS_CTRL
:
1454 gen
.generate_tcs_special_vars();
1456 case MESA_SHADER_TESS_EVAL
:
1457 gen
.generate_tes_special_vars();
1459 case MESA_SHADER_GEOMETRY
:
1460 gen
.generate_gs_special_vars();
1462 case MESA_SHADER_FRAGMENT
:
1463 gen
.generate_fs_special_vars();
1465 case MESA_SHADER_COMPUTE
:
1466 gen
.generate_cs_special_vars();
1475 * Initialize compute shader variables with values that are derived from other
1476 * compute shader variable.
1479 initialize_cs_derived_variables(gl_shader
*shader
,
1480 ir_function_signature
*const main_sig
)
1482 assert(shader
->Stage
== MESA_SHADER_COMPUTE
);
1484 ir_variable
*gl_GlobalInvocationID
=
1485 shader
->symbols
->get_variable("gl_GlobalInvocationID");
1486 assert(gl_GlobalInvocationID
);
1487 ir_variable
*gl_WorkGroupID
=
1488 shader
->symbols
->get_variable("gl_WorkGroupID");
1489 assert(gl_WorkGroupID
);
1490 ir_variable
*gl_WorkGroupSize
=
1491 shader
->symbols
->get_variable("gl_WorkGroupSize");
1492 if (gl_WorkGroupSize
== NULL
) {
1493 void *const mem_ctx
= ralloc_parent(shader
->ir
);
1494 gl_WorkGroupSize
= new(mem_ctx
) ir_variable(glsl_type::uvec3_type
,
1497 gl_WorkGroupSize
->data
.how_declared
= ir_var_declared_implicitly
;
1498 gl_WorkGroupSize
->data
.read_only
= true;
1499 shader
->ir
->push_head(gl_WorkGroupSize
);
1501 ir_variable
*gl_LocalInvocationID
=
1502 shader
->symbols
->get_variable("gl_LocalInvocationID");
1503 assert(gl_LocalInvocationID
);
1505 /* gl_GlobalInvocationID =
1506 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1508 ir_instruction
*inst
=
1509 assign(gl_GlobalInvocationID
,
1510 add(mul(gl_WorkGroupID
, gl_WorkGroupSize
),
1511 gl_LocalInvocationID
));
1512 main_sig
->body
.push_head(inst
);
1514 /* gl_LocalInvocationIndex =
1515 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1516 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1517 * gl_LocalInvocationID.x;
1519 ir_expression
*index_z
=
1520 mul(mul(swizzle_z(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
)),
1521 swizzle_y(gl_WorkGroupSize
));
1522 ir_expression
*index_y
=
1523 mul(swizzle_y(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
));
1524 ir_expression
*index_y_plus_z
= add(index_y
, index_z
);
1525 operand
index_x(swizzle_x(gl_LocalInvocationID
));
1526 ir_expression
*index_x_plus_y_plus_z
= add(index_y_plus_z
, index_x
);
1527 ir_variable
*gl_LocalInvocationIndex
=
1528 shader
->symbols
->get_variable("gl_LocalInvocationIndex");
1529 assert(gl_LocalInvocationIndex
);
1530 inst
= assign(gl_LocalInvocationIndex
, index_x_plus_y_plus_z
);
1531 main_sig
->body
.push_head(inst
);
1536 * Initialize builtin variables with values based on other builtin variables.
1537 * These are initialized in the main function.
1540 _mesa_glsl_initialize_derived_variables(struct gl_context
*ctx
,
1543 /* We only need to set CS variables currently. */
1544 if (shader
->Stage
== MESA_SHADER_COMPUTE
&&
1545 ctx
->Const
.LowerCsDerivedVariables
) {
1546 ir_function_signature
*const main_sig
=
1547 _mesa_get_main_function_signature(shader
->symbols
);
1549 if (main_sig
!= NULL
)
1550 initialize_cs_derived_variables(shader
, main_sig
);