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
[10];
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
].interpolation
= INTERP_QUALIFIER_NONE
;
327 this->fields
[this->num_fields
].centroid
= 0;
328 this->fields
[this->num_fields
].sample
= 0;
329 this->fields
[this->num_fields
].patch
= 0;
335 per_vertex_accumulator::construct_interface_instance() const
337 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
338 GLSL_INTERFACE_PACKING_STD140
,
343 class builtin_variable_generator
346 builtin_variable_generator(exec_list
*instructions
,
347 struct _mesa_glsl_parse_state
*state
);
348 void generate_constants();
349 void generate_uniforms();
350 void generate_vs_special_vars();
351 void generate_tcs_special_vars();
352 void generate_tes_special_vars();
353 void generate_gs_special_vars();
354 void generate_fs_special_vars();
355 void generate_cs_special_vars();
356 void generate_varyings();
359 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
361 return glsl_type::get_array_instance(base
, elements
);
364 const glsl_type
*type(const char *name
)
366 return symtab
->get_type(name
);
369 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
371 return add_variable(name
, type
, ir_var_shader_in
, slot
);
374 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
376 return add_variable(name
, type
, ir_var_shader_out
, slot
);
379 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
382 return add_variable(name
, type
, ir_var_system_value
, slot
);
385 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
386 enum ir_variable_mode mode
, int slot
);
387 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
388 ir_variable
*add_const(const char *name
, int value
);
389 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
390 void add_varying(int slot
, const glsl_type
*type
, const char *name
);
392 exec_list
* const instructions
;
393 struct _mesa_glsl_parse_state
* const state
;
394 glsl_symbol_table
* const symtab
;
397 * True if compatibility-profile-only variables should be included. (In
398 * desktop GL, these are always included when the GLSL version is 1.30 and
401 const bool compatibility
;
403 const glsl_type
* const bool_t
;
404 const glsl_type
* const int_t
;
405 const glsl_type
* const uint_t
;
406 const glsl_type
* const float_t
;
407 const glsl_type
* const vec2_t
;
408 const glsl_type
* const vec3_t
;
409 const glsl_type
* const vec4_t
;
410 const glsl_type
* const uvec3_t
;
411 const glsl_type
* const mat3_t
;
412 const glsl_type
* const mat4_t
;
414 per_vertex_accumulator per_vertex_in
;
415 per_vertex_accumulator per_vertex_out
;
419 builtin_variable_generator::builtin_variable_generator(
420 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
421 : instructions(instructions
), state(state
), symtab(state
->symbols
),
422 compatibility(!state
->is_version(140, 100)),
423 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
424 uint_t(glsl_type::uint_type
),
425 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
426 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
427 uvec3_t(glsl_type::uvec3_type
),
428 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
434 builtin_variable_generator::add_variable(const char *name
,
435 const glsl_type
*type
,
436 enum ir_variable_mode mode
, int slot
)
438 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
439 var
->data
.how_declared
= ir_var_declared_implicitly
;
441 switch (var
->data
.mode
) {
443 case ir_var_shader_in
:
445 case ir_var_system_value
:
446 var
->data
.read_only
= true;
448 case ir_var_shader_out
:
449 case ir_var_shader_storage
:
452 /* The only variables that are added using this function should be
453 * uniforms, shader storage, shader inputs, and shader outputs, constants
454 * (which use ir_var_auto), and system values.
460 var
->data
.location
= slot
;
461 var
->data
.explicit_location
= (slot
>= 0);
462 var
->data
.explicit_index
= 0;
464 /* Once the variable is created an initialized, add it to the symbol table
465 * and add the declaration to the IR stream.
467 instructions
->push_tail(var
);
469 symtab
->add_variable(var
);
475 builtin_variable_generator::add_uniform(const glsl_type
*type
,
478 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
481 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
482 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
487 assert(_mesa_builtin_uniform_desc
[i
].name
!= NULL
);
488 const struct gl_builtin_uniform_desc
* const statevar
=
489 &_mesa_builtin_uniform_desc
[i
];
491 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
493 ir_state_slot
*slots
=
494 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
496 for (unsigned a
= 0; a
< array_count
; a
++) {
497 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
498 const struct gl_builtin_uniform_element
*element
=
499 &statevar
->elements
[j
];
501 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
502 if (type
->is_array()) {
503 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
504 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
505 slots
->tokens
[2] = a
;
507 slots
->tokens
[1] = a
;
511 slots
->swizzle
= element
->swizzle
;
521 builtin_variable_generator::add_const(const char *name
, int value
)
523 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
525 var
->constant_value
= new(var
) ir_constant(value
);
526 var
->constant_initializer
= new(var
) ir_constant(value
);
527 var
->data
.has_initializer
= true;
533 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
536 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
538 ir_constant_data data
;
539 memset(&data
, 0, sizeof(data
));
543 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
544 var
->constant_initializer
=
545 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
546 var
->data
.has_initializer
= true;
552 builtin_variable_generator::generate_constants()
554 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
555 add_const("gl_MaxVertexTextureImageUnits",
556 state
->Const
.MaxVertexTextureImageUnits
);
557 add_const("gl_MaxCombinedTextureImageUnits",
558 state
->Const
.MaxCombinedTextureImageUnits
);
559 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
560 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
562 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
563 * GL counts them in units of "components" or "floats".
565 if (state
->es_shader
) {
566 add_const("gl_MaxVertexUniformVectors",
567 state
->Const
.MaxVertexUniformComponents
/ 4);
568 add_const("gl_MaxFragmentUniformVectors",
569 state
->Const
.MaxFragmentUniformComponents
/ 4);
571 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
572 * vertex and fragment shader constants.
574 if (state
->is_version(0, 300)) {
575 add_const("gl_MaxVertexOutputVectors",
576 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
577 add_const("gl_MaxFragmentInputVectors",
578 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
580 add_const("gl_MaxVaryingVectors",
581 state
->ctx
->Const
.MaxVarying
);
584 add_const("gl_MaxVertexUniformComponents",
585 state
->Const
.MaxVertexUniformComponents
);
587 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
590 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
592 add_const("gl_MaxFragmentUniformComponents",
593 state
->Const
.MaxFragmentUniformComponents
);
596 /* Texel offsets were introduced in ARB_shading_language_420pack (which
597 * requires desktop GLSL version 130), and adopted into desktop GLSL
598 * version 4.20 and GLSL ES version 3.00.
600 if ((state
->is_version(130, 0) &&
601 state
->ARB_shading_language_420pack_enable
) ||
602 state
->is_version(420, 300)) {
603 add_const("gl_MinProgramTexelOffset",
604 state
->Const
.MinProgramTexelOffset
);
605 add_const("gl_MaxProgramTexelOffset",
606 state
->Const
.MaxProgramTexelOffset
);
609 if (state
->is_version(130, 0)) {
610 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
611 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
614 if (state
->is_version(150, 0)) {
615 add_const("gl_MaxVertexOutputComponents",
616 state
->Const
.MaxVertexOutputComponents
);
617 add_const("gl_MaxGeometryInputComponents",
618 state
->Const
.MaxGeometryInputComponents
);
619 add_const("gl_MaxGeometryOutputComponents",
620 state
->Const
.MaxGeometryOutputComponents
);
621 add_const("gl_MaxFragmentInputComponents",
622 state
->Const
.MaxFragmentInputComponents
);
623 add_const("gl_MaxGeometryTextureImageUnits",
624 state
->Const
.MaxGeometryTextureImageUnits
);
625 add_const("gl_MaxGeometryOutputVertices",
626 state
->Const
.MaxGeometryOutputVertices
);
627 add_const("gl_MaxGeometryTotalOutputComponents",
628 state
->Const
.MaxGeometryTotalOutputComponents
);
629 add_const("gl_MaxGeometryUniformComponents",
630 state
->Const
.MaxGeometryUniformComponents
);
632 /* Note: the GLSL 1.50-4.40 specs require
633 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
634 * But they do not define what it means (and there does not appear to be
635 * any corresponding constant in the GL specs). However,
636 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
637 * be the maximum number of components available for use as geometry
638 * outputs. So we assume this is a synonym for
639 * gl_MaxGeometryOutputComponents.
641 add_const("gl_MaxGeometryVaryingComponents",
642 state
->Const
.MaxGeometryOutputComponents
);
646 /* Note: gl_MaxLights stopped being listed as an explicit constant in
647 * GLSL 1.30, however it continues to be referred to (as a minimum size
648 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
649 * this seems like it was probably an oversight.
651 add_const("gl_MaxLights", state
->Const
.MaxLights
);
653 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
655 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
656 * 1.50, however this seems like it was probably an oversight.
658 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
660 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
661 * re-introduced in GLSL 1.50, so this seems like it was probably an
664 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
667 if (state
->has_atomic_counters()) {
668 add_const("gl_MaxVertexAtomicCounters",
669 state
->Const
.MaxVertexAtomicCounters
);
670 add_const("gl_MaxFragmentAtomicCounters",
671 state
->Const
.MaxFragmentAtomicCounters
);
672 add_const("gl_MaxCombinedAtomicCounters",
673 state
->Const
.MaxCombinedAtomicCounters
);
674 add_const("gl_MaxAtomicCounterBindings",
675 state
->Const
.MaxAtomicBufferBindings
);
677 /* When Mesa adds support for GL_OES_geometry_shader and
678 * GL_OES_tessellation_shader, this will need to change.
680 if (!state
->es_shader
) {
681 add_const("gl_MaxGeometryAtomicCounters",
682 state
->Const
.MaxGeometryAtomicCounters
);
684 if (state
->is_version(400, 0) ||
685 state
->ARB_tessellation_shader_enable
) {
686 add_const("gl_MaxTessControlAtomicCounters",
687 state
->Const
.MaxTessControlAtomicCounters
);
688 add_const("gl_MaxTessEvaluationAtomicCounters",
689 state
->Const
.MaxTessEvaluationAtomicCounters
);
694 if (state
->is_version(420, 310)) {
695 add_const("gl_MaxVertexAtomicCounterBuffers",
696 state
->Const
.MaxVertexAtomicCounterBuffers
);
697 add_const("gl_MaxFragmentAtomicCounterBuffers",
698 state
->Const
.MaxFragmentAtomicCounterBuffers
);
699 add_const("gl_MaxCombinedAtomicCounterBuffers",
700 state
->Const
.MaxCombinedAtomicCounterBuffers
);
701 add_const("gl_MaxAtomicCounterBufferSize",
702 state
->Const
.MaxAtomicCounterBufferSize
);
704 /* When Mesa adds support for GL_OES_geometry_shader and
705 * GL_OES_tessellation_shader, this will need to change.
707 if (!state
->es_shader
) {
708 add_const("gl_MaxGeometryAtomicCounterBuffers",
709 state
->Const
.MaxGeometryAtomicCounterBuffers
);
710 add_const("gl_MaxTessControlAtomicCounterBuffers",
711 state
->Const
.MaxTessControlAtomicCounterBuffers
);
712 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
713 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
717 if (state
->is_version(430, 0) || state
->ARB_compute_shader_enable
) {
718 add_const("gl_MaxComputeAtomicCounterBuffers", MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS
);
719 add_const("gl_MaxComputeAtomicCounters", MAX_COMPUTE_ATOMIC_COUNTERS
);
720 add_const("gl_MaxComputeImageUniforms", MAX_COMPUTE_IMAGE_UNIFORMS
);
721 add_const("gl_MaxComputeTextureImageUnits", MAX_COMPUTE_TEXTURE_IMAGE_UNITS
);
722 add_const("gl_MaxComputeUniformComponents", MAX_COMPUTE_UNIFORM_COMPONENTS
);
724 add_const_ivec3("gl_MaxComputeWorkGroupCount",
725 state
->Const
.MaxComputeWorkGroupCount
[0],
726 state
->Const
.MaxComputeWorkGroupCount
[1],
727 state
->Const
.MaxComputeWorkGroupCount
[2]);
728 add_const_ivec3("gl_MaxComputeWorkGroupSize",
729 state
->Const
.MaxComputeWorkGroupSize
[0],
730 state
->Const
.MaxComputeWorkGroupSize
[1],
731 state
->Const
.MaxComputeWorkGroupSize
[2]);
733 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
735 * The built-in constant gl_WorkGroupSize is a compute-shader
736 * constant containing the local work-group size of the shader. The
737 * size of the work group in the X, Y, and Z dimensions is stored in
738 * the x, y, and z components. The constants values in
739 * gl_WorkGroupSize will match those specified in the required
740 * local_size_x, local_size_y, and local_size_z layout qualifiers
741 * for the current shader. This is a constant so that it can be
742 * used to size arrays of memory that can be shared within the local
743 * work group. It is a compile-time error to use gl_WorkGroupSize
744 * in a shader that does not declare a fixed local group size, or
745 * before that shader has declared a fixed local group size, using
746 * local_size_x, local_size_y, and local_size_z.
748 * To prevent the shader from trying to refer to gl_WorkGroupSize before
749 * the layout declaration, we don't define it here. Intead we define it
750 * in ast_cs_input_layout::hir().
754 if (state
->is_version(420, 310) ||
755 state
->ARB_shader_image_load_store_enable
) {
756 add_const("gl_MaxImageUnits",
757 state
->Const
.MaxImageUnits
);
758 add_const("gl_MaxVertexImageUniforms",
759 state
->Const
.MaxVertexImageUniforms
);
760 add_const("gl_MaxFragmentImageUniforms",
761 state
->Const
.MaxFragmentImageUniforms
);
762 add_const("gl_MaxCombinedImageUniforms",
763 state
->Const
.MaxCombinedImageUniforms
);
765 if (!state
->es_shader
) {
766 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
767 state
->Const
.MaxCombinedShaderOutputResources
);
768 add_const("gl_MaxImageSamples",
769 state
->Const
.MaxImageSamples
);
770 add_const("gl_MaxGeometryImageUniforms",
771 state
->Const
.MaxGeometryImageUniforms
);
774 if (state
->is_version(450, 310)) {
775 add_const("gl_MaxCombinedShaderOutputResources",
776 state
->Const
.MaxCombinedShaderOutputResources
);
779 if (state
->is_version(400, 0) ||
780 state
->ARB_tessellation_shader_enable
) {
781 add_const("gl_MaxTessControlImageUniforms",
782 state
->Const
.MaxTessControlImageUniforms
);
783 add_const("gl_MaxTessEvaluationImageUniforms",
784 state
->Const
.MaxTessEvaluationImageUniforms
);
788 if (state
->is_version(410, 0) ||
789 state
->ARB_viewport_array_enable
)
790 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
792 if (state
->is_version(400, 0) ||
793 state
->ARB_tessellation_shader_enable
) {
794 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
795 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
796 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
797 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
798 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
799 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
800 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
801 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
802 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
803 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
804 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
805 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
811 * Generate uniform variables (which exist in all types of shaders).
814 builtin_variable_generator::generate_uniforms()
816 if (state
->is_version(400, 0) || state
->ARB_sample_shading_enable
)
817 add_uniform(int_t
, "gl_NumSamples");
818 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
819 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
820 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
823 add_uniform(mat4_t
, "gl_ModelViewMatrix");
824 add_uniform(mat4_t
, "gl_ProjectionMatrix");
825 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
826 add_uniform(mat3_t
, "gl_NormalMatrix");
827 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
828 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
829 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
830 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
831 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
832 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
833 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
834 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
835 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
836 add_uniform(float_t
, "gl_NormalScale");
837 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
838 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
840 const glsl_type
*const mat4_array_type
=
841 array(mat4_t
, state
->Const
.MaxTextureCoords
);
842 add_uniform(mat4_array_type
, "gl_TextureMatrix");
843 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
844 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
845 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
847 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
848 add_uniform(type("gl_PointParameters"), "gl_Point");
850 const glsl_type
*const material_parameters_type
=
851 type("gl_MaterialParameters");
852 add_uniform(material_parameters_type
, "gl_FrontMaterial");
853 add_uniform(material_parameters_type
, "gl_BackMaterial");
855 add_uniform(array(type("gl_LightSourceParameters"),
856 state
->Const
.MaxLights
),
859 const glsl_type
*const light_model_products_type
=
860 type("gl_LightModelProducts");
861 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
862 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
864 const glsl_type
*const light_products_type
=
865 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
866 add_uniform(light_products_type
, "gl_FrontLightProduct");
867 add_uniform(light_products_type
, "gl_BackLightProduct");
869 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
870 "gl_TextureEnvColor");
872 const glsl_type
*const texcoords_vec4
=
873 array(vec4_t
, state
->Const
.MaxTextureCoords
);
874 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
875 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
876 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
877 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
878 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
879 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
880 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
881 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
883 add_uniform(type("gl_FogParameters"), "gl_Fog");
889 * Generate variables which only exist in vertex shaders.
892 builtin_variable_generator::generate_vs_special_vars()
894 if (state
->is_version(130, 300))
895 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
896 if (state
->ARB_draw_instanced_enable
)
897 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
898 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
899 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
900 if (state
->AMD_vertex_shader_layer_enable
)
901 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
902 if (state
->AMD_vertex_shader_viewport_index_enable
)
903 add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
905 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
906 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
907 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
908 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
909 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
910 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
911 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
912 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
913 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
914 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
915 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
916 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
917 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
923 * Generate variables which only exist in tessellation control shaders.
926 builtin_variable_generator::generate_tcs_special_vars()
928 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
929 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
930 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
932 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
933 "gl_TessLevelOuter")->data
.patch
= 1;
934 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
935 "gl_TessLevelInner")->data
.patch
= 1;
940 * Generate variables which only exist in tessellation evaluation shaders.
943 builtin_variable_generator::generate_tes_special_vars()
945 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
946 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
947 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
948 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
949 "gl_TessLevelOuter");
950 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
951 "gl_TessLevelInner");
956 * Generate variables which only exist in geometry shaders.
959 builtin_variable_generator::generate_gs_special_vars()
961 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
962 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
)
963 add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
964 if (state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
)
965 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
967 /* Although gl_PrimitiveID appears in tessellation control and tessellation
968 * evaluation shaders, it has a different function there than it has in
969 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
970 * as special geometry shader variables.
972 * Note that although the general convention of suffixing geometry shader
973 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
974 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
975 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
978 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
979 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
980 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
981 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
986 * Generate variables which only exist in fragment shaders.
989 builtin_variable_generator::generate_fs_special_vars()
991 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
992 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
993 if (state
->is_version(120, 100))
994 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
996 if (state
->is_version(150, 0)) {
998 add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
999 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
1002 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
1003 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
1004 * They were removed from GLSL ES 3.00.
1006 if (compatibility
|| !state
->is_version(420, 300)) {
1007 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1008 add_output(FRAG_RESULT_DATA0
,
1009 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1012 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1015 if (state
->is_version(110, 300))
1016 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1018 if (state
->ARB_shader_stencil_export_enable
) {
1019 ir_variable
*const var
=
1020 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1021 if (state
->ARB_shader_stencil_export_warn
)
1022 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1025 if (state
->AMD_shader_stencil_export_enable
) {
1026 ir_variable
*const var
=
1027 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1028 if (state
->AMD_shader_stencil_export_warn
)
1029 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1032 if (state
->is_version(400, 0) || state
->ARB_sample_shading_enable
) {
1033 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1034 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1035 /* From the ARB_sample_shading specification:
1036 * "The number of elements in the array is ceil(<s>/32), where
1037 * <s> is the maximum number of color samples supported by the
1039 * Since no drivers expose more than 32x MSAA, we can simply set
1040 * the array size to 1 rather than computing it.
1042 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1045 if (state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) {
1046 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1049 if (state
->is_version(430, 0) || state
->ARB_fragment_layer_viewport_enable
) {
1050 add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1051 add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1057 * Generate variables which only exist in compute shaders.
1060 builtin_variable_generator::generate_cs_special_vars()
1062 add_system_value(SYSTEM_VALUE_LOCAL_INVOCATION_ID
, uvec3_t
,
1063 "gl_LocalInvocationID");
1064 add_system_value(SYSTEM_VALUE_WORK_GROUP_ID
, uvec3_t
, "gl_WorkGroupID");
1065 add_variable("gl_GlobalInvocationID", uvec3_t
, ir_var_auto
, 0);
1066 add_variable("gl_LocalInvocationIndex", uint_t
, ir_var_auto
, 0);
1067 /* TODO: finish this. */
1072 * Add a single "varying" variable. The variable's type and direction (input
1073 * or output) are adjusted as appropriate for the type of shader being
1077 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1080 switch (state
->stage
) {
1081 case MESA_SHADER_TESS_CTRL
:
1082 case MESA_SHADER_TESS_EVAL
:
1083 case MESA_SHADER_GEOMETRY
:
1084 this->per_vertex_in
.add_field(slot
, type
, name
);
1086 case MESA_SHADER_VERTEX
:
1087 this->per_vertex_out
.add_field(slot
, type
, name
);
1089 case MESA_SHADER_FRAGMENT
:
1090 add_input(slot
, type
, name
);
1092 case MESA_SHADER_COMPUTE
:
1093 /* Compute shaders don't have varyings. */
1100 * Generate variables that are used to communicate data from one shader stage
1101 * to the next ("varyings").
1104 builtin_variable_generator::generate_varyings()
1106 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1107 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1108 add_varying(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1109 add_varying(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1112 if (state
->is_version(130, 0)) {
1113 add_varying(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1117 if (compatibility
) {
1118 add_varying(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1119 add_varying(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1120 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1121 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1122 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1124 add_varying(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1125 add_varying(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1126 add_varying(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1127 add_varying(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1128 add_varying(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1132 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1135 * "In the tessellation control language, built-in variables are
1136 * intrinsically declared as:
1140 * float gl_PointSize;
1141 * float gl_ClipDistance[];
1142 * } gl_in[gl_MaxPatchVertices];"
1144 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1145 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1146 const glsl_type
*per_vertex_in_type
=
1147 this->per_vertex_in
.construct_interface_instance();
1148 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1149 ir_var_shader_in
, -1);
1151 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1152 const glsl_type
*per_vertex_in_type
=
1153 this->per_vertex_in
.construct_interface_instance();
1154 add_variable("gl_in", array(per_vertex_in_type
, 0),
1155 ir_var_shader_in
, -1);
1157 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1158 const glsl_type
*per_vertex_out_type
=
1159 this->per_vertex_out
.construct_interface_instance();
1160 add_variable("gl_out", array(per_vertex_out_type
, 0),
1161 ir_var_shader_out
, -1);
1163 if (state
->stage
== MESA_SHADER_VERTEX
||
1164 state
->stage
== MESA_SHADER_TESS_EVAL
||
1165 state
->stage
== MESA_SHADER_GEOMETRY
) {
1166 const glsl_type
*per_vertex_out_type
=
1167 this->per_vertex_out
.construct_interface_instance();
1168 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1169 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1171 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1172 fields
[i
].location
);
1173 var
->data
.interpolation
= fields
[i
].interpolation
;
1174 var
->data
.centroid
= fields
[i
].centroid
;
1175 var
->data
.sample
= fields
[i
].sample
;
1176 var
->data
.patch
= fields
[i
].patch
;
1177 var
->init_interface_type(per_vertex_out_type
);
1183 }; /* Anonymous namespace */
1187 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1188 struct _mesa_glsl_parse_state
*state
)
1190 builtin_variable_generator
gen(instructions
, state
);
1192 gen
.generate_constants();
1193 gen
.generate_uniforms();
1195 gen
.generate_varyings();
1197 switch (state
->stage
) {
1198 case MESA_SHADER_VERTEX
:
1199 gen
.generate_vs_special_vars();
1201 case MESA_SHADER_TESS_CTRL
:
1202 gen
.generate_tcs_special_vars();
1204 case MESA_SHADER_TESS_EVAL
:
1205 gen
.generate_tes_special_vars();
1207 case MESA_SHADER_GEOMETRY
:
1208 gen
.generate_gs_special_vars();
1210 case MESA_SHADER_FRAGMENT
:
1211 gen
.generate_fs_special_vars();
1213 case MESA_SHADER_COMPUTE
:
1214 gen
.generate_cs_special_vars();
1221 * Initialize compute shader variables with values that are derived from other
1222 * compute shader variable.
1225 initialize_cs_derived_variables(gl_shader
*shader
,
1226 ir_function_signature
*const main_sig
)
1228 assert(shader
->Stage
== MESA_SHADER_COMPUTE
);
1230 ir_variable
*gl_GlobalInvocationID
=
1231 shader
->symbols
->get_variable("gl_GlobalInvocationID");
1232 assert(gl_GlobalInvocationID
);
1233 ir_variable
*gl_WorkGroupID
=
1234 shader
->symbols
->get_variable("gl_WorkGroupID");
1235 assert(gl_WorkGroupID
);
1236 ir_variable
*gl_WorkGroupSize
=
1237 shader
->symbols
->get_variable("gl_WorkGroupSize");
1238 if (gl_WorkGroupSize
== NULL
) {
1239 void *const mem_ctx
= ralloc_parent(shader
->ir
);
1240 gl_WorkGroupSize
= new(mem_ctx
) ir_variable(glsl_type::uvec3_type
,
1243 gl_WorkGroupSize
->data
.how_declared
= ir_var_declared_implicitly
;
1244 gl_WorkGroupSize
->data
.read_only
= true;
1245 shader
->ir
->push_head(gl_WorkGroupSize
);
1247 ir_variable
*gl_LocalInvocationID
=
1248 shader
->symbols
->get_variable("gl_LocalInvocationID");
1249 assert(gl_LocalInvocationID
);
1251 /* gl_GlobalInvocationID =
1252 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
1254 ir_instruction
*inst
=
1255 assign(gl_GlobalInvocationID
,
1256 add(mul(gl_WorkGroupID
, gl_WorkGroupSize
),
1257 gl_LocalInvocationID
));
1258 main_sig
->body
.push_head(inst
);
1260 /* gl_LocalInvocationIndex =
1261 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
1262 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
1263 * gl_LocalInvocationID.x;
1265 ir_expression
*index_z
=
1266 mul(mul(swizzle_z(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
)),
1267 swizzle_y(gl_WorkGroupSize
));
1268 ir_expression
*index_y
=
1269 mul(swizzle_y(gl_LocalInvocationID
), swizzle_x(gl_WorkGroupSize
));
1270 ir_expression
*index_y_plus_z
= add(index_y
, index_z
);
1271 operand
index_x(swizzle_x(gl_LocalInvocationID
));
1272 ir_expression
*index_x_plus_y_plus_z
= add(index_y_plus_z
, index_x
);
1273 ir_variable
*gl_LocalInvocationIndex
=
1274 shader
->symbols
->get_variable("gl_LocalInvocationIndex");
1275 assert(gl_LocalInvocationIndex
);
1276 inst
= assign(gl_LocalInvocationIndex
, index_x_plus_y_plus_z
);
1277 main_sig
->body
.push_head(inst
);
1282 * Initialize builtin variables with values based on other builtin variables.
1283 * These are initialized in the main function.
1286 _mesa_glsl_initialize_derived_variables(gl_shader
*shader
)
1288 /* We only need to set CS variables currently. */
1289 if (shader
->Stage
!= MESA_SHADER_COMPUTE
)
1292 ir_function_signature
*const main_sig
=
1293 _mesa_get_main_function_signature(shader
);
1294 if (main_sig
== NULL
)
1297 initialize_cs_derived_variables(shader
, main_sig
);