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 "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "main/core.h"
28 #include "main/uniforms.h"
29 #include "program/prog_statevars.h"
30 #include "program/prog_instruction.h"
32 static const struct gl_builtin_uniform_element gl_NumSamples_elements
[] = {
33 {NULL
, {STATE_NUM_SAMPLES
, 0, 0}, SWIZZLE_XXXX
}
36 static const struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
37 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
38 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
39 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
42 static const struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
43 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
46 static const struct gl_builtin_uniform_element gl_Point_elements
[] = {
47 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
48 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
49 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
50 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
51 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
52 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
53 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
56 static const struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
57 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
58 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
59 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
60 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
61 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
64 static const struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
65 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
66 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
67 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
68 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
69 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
72 static const struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
73 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
74 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
75 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
76 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
77 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
78 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
79 MAKE_SWIZZLE4(SWIZZLE_X
,
83 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
84 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
85 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
86 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
87 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
88 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
91 static const struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
92 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
95 static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
96 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
99 static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
100 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
103 static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
104 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
105 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
106 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
109 static const struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
110 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
111 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
112 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
115 static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
116 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
119 static const struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
120 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
123 static const struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
124 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
127 static const struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
128 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
131 static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
132 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
135 static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
136 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
139 static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
140 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
143 static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
144 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
147 static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
148 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
151 static const struct gl_builtin_uniform_element gl_Fog_elements
[] = {
152 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
153 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
154 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
155 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
156 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
159 static const struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
160 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
163 static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
164 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
167 static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
168 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
171 static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
172 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
175 #define MATRIX(name, statevar, modifier) \
176 static const struct gl_builtin_uniform_element name ## _elements[] = { \
177 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
178 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
179 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
180 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
183 MATRIX(gl_ModelViewMatrix
,
184 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
185 MATRIX(gl_ModelViewMatrixInverse
,
186 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
187 MATRIX(gl_ModelViewMatrixTranspose
,
188 STATE_MODELVIEW_MATRIX
, 0);
189 MATRIX(gl_ModelViewMatrixInverseTranspose
,
190 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
192 MATRIX(gl_ProjectionMatrix
,
193 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
194 MATRIX(gl_ProjectionMatrixInverse
,
195 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
196 MATRIX(gl_ProjectionMatrixTranspose
,
197 STATE_PROJECTION_MATRIX
, 0);
198 MATRIX(gl_ProjectionMatrixInverseTranspose
,
199 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
201 MATRIX(gl_ModelViewProjectionMatrix
,
202 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
203 MATRIX(gl_ModelViewProjectionMatrixInverse
,
204 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
205 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
206 STATE_MVP_MATRIX
, 0);
207 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
208 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
210 MATRIX(gl_TextureMatrix
,
211 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
212 MATRIX(gl_TextureMatrixInverse
,
213 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
214 MATRIX(gl_TextureMatrixTranspose
,
215 STATE_TEXTURE_MATRIX
, 0);
216 MATRIX(gl_TextureMatrixInverseTranspose
,
217 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
219 static const struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
220 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
221 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
222 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
223 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
224 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
225 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
230 #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
232 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
233 STATEVAR(gl_NumSamples
),
234 STATEVAR(gl_DepthRange
),
235 STATEVAR(gl_ClipPlane
),
237 STATEVAR(gl_FrontMaterial
),
238 STATEVAR(gl_BackMaterial
),
239 STATEVAR(gl_LightSource
),
240 STATEVAR(gl_LightModel
),
241 STATEVAR(gl_FrontLightModelProduct
),
242 STATEVAR(gl_BackLightModelProduct
),
243 STATEVAR(gl_FrontLightProduct
),
244 STATEVAR(gl_BackLightProduct
),
245 STATEVAR(gl_TextureEnvColor
),
246 STATEVAR(gl_EyePlaneS
),
247 STATEVAR(gl_EyePlaneT
),
248 STATEVAR(gl_EyePlaneR
),
249 STATEVAR(gl_EyePlaneQ
),
250 STATEVAR(gl_ObjectPlaneS
),
251 STATEVAR(gl_ObjectPlaneT
),
252 STATEVAR(gl_ObjectPlaneR
),
253 STATEVAR(gl_ObjectPlaneQ
),
256 STATEVAR(gl_ModelViewMatrix
),
257 STATEVAR(gl_ModelViewMatrixInverse
),
258 STATEVAR(gl_ModelViewMatrixTranspose
),
259 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
261 STATEVAR(gl_ProjectionMatrix
),
262 STATEVAR(gl_ProjectionMatrixInverse
),
263 STATEVAR(gl_ProjectionMatrixTranspose
),
264 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
266 STATEVAR(gl_ModelViewProjectionMatrix
),
267 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
268 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
269 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
271 STATEVAR(gl_TextureMatrix
),
272 STATEVAR(gl_TextureMatrixInverse
),
273 STATEVAR(gl_TextureMatrixTranspose
),
274 STATEVAR(gl_TextureMatrixInverseTranspose
),
276 STATEVAR(gl_NormalMatrix
),
277 STATEVAR(gl_NormalScale
),
279 STATEVAR(gl_FogParamsOptimizedMESA
),
280 STATEVAR(gl_CurrentAttribVertMESA
),
281 STATEVAR(gl_CurrentAttribFragMESA
),
290 * Data structure that accumulates fields for the gl_PerVertex interface
293 class per_vertex_accumulator
296 per_vertex_accumulator();
297 void add_field(int slot
, const glsl_type
*type
, const char *name
);
298 const glsl_type
*construct_interface_instance() const;
301 glsl_struct_field fields
[10];
306 per_vertex_accumulator::per_vertex_accumulator()
314 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
317 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
318 this->fields
[this->num_fields
].type
= type
;
319 this->fields
[this->num_fields
].name
= name
;
320 this->fields
[this->num_fields
].matrix_layout
= GLSL_MATRIX_LAYOUT_INHERITED
;
321 this->fields
[this->num_fields
].location
= slot
;
322 this->fields
[this->num_fields
].interpolation
= INTERP_QUALIFIER_NONE
;
323 this->fields
[this->num_fields
].centroid
= 0;
324 this->fields
[this->num_fields
].sample
= 0;
325 this->fields
[this->num_fields
].patch
= 0;
331 per_vertex_accumulator::construct_interface_instance() const
333 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
334 GLSL_INTERFACE_PACKING_STD140
,
339 class builtin_variable_generator
342 builtin_variable_generator(exec_list
*instructions
,
343 struct _mesa_glsl_parse_state
*state
);
344 void generate_constants();
345 void generate_uniforms();
346 void generate_vs_special_vars();
347 void generate_tcs_special_vars();
348 void generate_tes_special_vars();
349 void generate_gs_special_vars();
350 void generate_fs_special_vars();
351 void generate_cs_special_vars();
352 void generate_varyings();
355 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
357 return glsl_type::get_array_instance(base
, elements
);
360 const glsl_type
*type(const char *name
)
362 return symtab
->get_type(name
);
365 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
367 return add_variable(name
, type
, ir_var_shader_in
, slot
);
370 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
372 return add_variable(name
, type
, ir_var_shader_out
, slot
);
375 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
378 return add_variable(name
, type
, ir_var_system_value
, slot
);
381 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
382 enum ir_variable_mode mode
, int slot
);
383 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
384 ir_variable
*add_const(const char *name
, int value
);
385 ir_variable
*add_const_ivec3(const char *name
, int x
, int y
, int z
);
386 void add_varying(int slot
, const glsl_type
*type
, const char *name
,
387 const char *name_as_gs_input
);
389 exec_list
* const instructions
;
390 struct _mesa_glsl_parse_state
* const state
;
391 glsl_symbol_table
* const symtab
;
394 * True if compatibility-profile-only variables should be included. (In
395 * desktop GL, these are always included when the GLSL version is 1.30 and
398 const bool compatibility
;
400 const glsl_type
* const bool_t
;
401 const glsl_type
* const int_t
;
402 const glsl_type
* const float_t
;
403 const glsl_type
* const vec2_t
;
404 const glsl_type
* const vec3_t
;
405 const glsl_type
* const vec4_t
;
406 const glsl_type
* const mat3_t
;
407 const glsl_type
* const mat4_t
;
409 per_vertex_accumulator per_vertex_in
;
410 per_vertex_accumulator per_vertex_out
;
414 builtin_variable_generator::builtin_variable_generator(
415 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
416 : instructions(instructions
), state(state
), symtab(state
->symbols
),
417 compatibility(!state
->is_version(140, 100)),
418 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
419 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
420 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
421 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
427 builtin_variable_generator::add_variable(const char *name
,
428 const glsl_type
*type
,
429 enum ir_variable_mode mode
, int slot
)
431 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
432 var
->data
.how_declared
= ir_var_declared_implicitly
;
434 switch (var
->data
.mode
) {
436 case ir_var_shader_in
:
438 case ir_var_system_value
:
439 var
->data
.read_only
= true;
441 case ir_var_shader_out
:
442 case ir_var_shader_storage
:
445 /* The only variables that are added using this function should be
446 * uniforms, shader storage, shader inputs, and shader outputs, constants
447 * (which use ir_var_auto), and system values.
453 var
->data
.location
= slot
;
454 var
->data
.explicit_location
= (slot
>= 0);
455 var
->data
.explicit_index
= 0;
457 /* Once the variable is created an initialized, add it to the symbol table
458 * and add the declaration to the IR stream.
460 instructions
->push_tail(var
);
462 symtab
->add_variable(var
);
468 builtin_variable_generator::add_uniform(const glsl_type
*type
,
471 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
474 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
475 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
480 assert(_mesa_builtin_uniform_desc
[i
].name
!= NULL
);
481 const struct gl_builtin_uniform_desc
* const statevar
=
482 &_mesa_builtin_uniform_desc
[i
];
484 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
486 ir_state_slot
*slots
=
487 uni
->allocate_state_slots(array_count
* statevar
->num_elements
);
489 for (unsigned a
= 0; a
< array_count
; a
++) {
490 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
491 const struct gl_builtin_uniform_element
*element
=
492 &statevar
->elements
[j
];
494 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
495 if (type
->is_array()) {
496 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
497 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
498 slots
->tokens
[2] = a
;
500 slots
->tokens
[1] = a
;
504 slots
->swizzle
= element
->swizzle
;
514 builtin_variable_generator::add_const(const char *name
, int value
)
516 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
518 var
->constant_value
= new(var
) ir_constant(value
);
519 var
->constant_initializer
= new(var
) ir_constant(value
);
520 var
->data
.has_initializer
= true;
526 builtin_variable_generator::add_const_ivec3(const char *name
, int x
, int y
,
529 ir_variable
*const var
= add_variable(name
, glsl_type::ivec3_type
,
531 ir_constant_data data
;
532 memset(&data
, 0, sizeof(data
));
536 var
->constant_value
= new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
537 var
->constant_initializer
=
538 new(var
) ir_constant(glsl_type::ivec3_type
, &data
);
539 var
->data
.has_initializer
= true;
545 builtin_variable_generator::generate_constants()
547 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
548 add_const("gl_MaxVertexTextureImageUnits",
549 state
->Const
.MaxVertexTextureImageUnits
);
550 add_const("gl_MaxCombinedTextureImageUnits",
551 state
->Const
.MaxCombinedTextureImageUnits
);
552 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
553 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
555 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
556 * GL counts them in units of "components" or "floats".
558 if (state
->es_shader
) {
559 add_const("gl_MaxVertexUniformVectors",
560 state
->Const
.MaxVertexUniformComponents
/ 4);
561 add_const("gl_MaxFragmentUniformVectors",
562 state
->Const
.MaxFragmentUniformComponents
/ 4);
564 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
565 * vertex and fragment shader constants.
567 if (state
->is_version(0, 300)) {
568 add_const("gl_MaxVertexOutputVectors",
569 state
->ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxOutputComponents
/ 4);
570 add_const("gl_MaxFragmentInputVectors",
571 state
->ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxInputComponents
/ 4);
573 add_const("gl_MaxVaryingVectors",
574 state
->ctx
->Const
.MaxVarying
);
577 add_const("gl_MaxVertexUniformComponents",
578 state
->Const
.MaxVertexUniformComponents
);
580 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
583 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
585 add_const("gl_MaxFragmentUniformComponents",
586 state
->Const
.MaxFragmentUniformComponents
);
589 /* Texel offsets were introduced in ARB_shading_language_420pack (which
590 * requires desktop GLSL version 130), and adopted into desktop GLSL
591 * version 4.20 and GLSL ES version 3.00.
593 if ((state
->is_version(130, 0) &&
594 state
->ARB_shading_language_420pack_enable
) ||
595 state
->is_version(420, 300)) {
596 add_const("gl_MinProgramTexelOffset",
597 state
->Const
.MinProgramTexelOffset
);
598 add_const("gl_MaxProgramTexelOffset",
599 state
->Const
.MaxProgramTexelOffset
);
602 if (state
->is_version(130, 0)) {
603 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
604 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
607 if (state
->is_version(150, 0)) {
608 add_const("gl_MaxVertexOutputComponents",
609 state
->Const
.MaxVertexOutputComponents
);
610 add_const("gl_MaxGeometryInputComponents",
611 state
->Const
.MaxGeometryInputComponents
);
612 add_const("gl_MaxGeometryOutputComponents",
613 state
->Const
.MaxGeometryOutputComponents
);
614 add_const("gl_MaxFragmentInputComponents",
615 state
->Const
.MaxFragmentInputComponents
);
616 add_const("gl_MaxGeometryTextureImageUnits",
617 state
->Const
.MaxGeometryTextureImageUnits
);
618 add_const("gl_MaxGeometryOutputVertices",
619 state
->Const
.MaxGeometryOutputVertices
);
620 add_const("gl_MaxGeometryTotalOutputComponents",
621 state
->Const
.MaxGeometryTotalOutputComponents
);
622 add_const("gl_MaxGeometryUniformComponents",
623 state
->Const
.MaxGeometryUniformComponents
);
625 /* Note: the GLSL 1.50-4.40 specs require
626 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
627 * But they do not define what it means (and there does not appear to be
628 * any corresponding constant in the GL specs). However,
629 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
630 * be the maximum number of components available for use as geometry
631 * outputs. So we assume this is a synonym for
632 * gl_MaxGeometryOutputComponents.
634 add_const("gl_MaxGeometryVaryingComponents",
635 state
->Const
.MaxGeometryOutputComponents
);
639 /* Note: gl_MaxLights stopped being listed as an explicit constant in
640 * GLSL 1.30, however it continues to be referred to (as a minimum size
641 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
642 * this seems like it was probably an oversight.
644 add_const("gl_MaxLights", state
->Const
.MaxLights
);
646 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
648 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
649 * 1.50, however this seems like it was probably an oversight.
651 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
653 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
654 * re-introduced in GLSL 1.50, so this seems like it was probably an
657 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
660 if (state
->has_atomic_counters()) {
661 add_const("gl_MaxVertexAtomicCounters",
662 state
->Const
.MaxVertexAtomicCounters
);
663 add_const("gl_MaxFragmentAtomicCounters",
664 state
->Const
.MaxFragmentAtomicCounters
);
665 add_const("gl_MaxCombinedAtomicCounters",
666 state
->Const
.MaxCombinedAtomicCounters
);
667 add_const("gl_MaxAtomicCounterBindings",
668 state
->Const
.MaxAtomicBufferBindings
);
670 /* When Mesa adds support for GL_OES_geometry_shader and
671 * GL_OES_tessellation_shader, this will need to change.
673 if (!state
->es_shader
) {
674 add_const("gl_MaxGeometryAtomicCounters",
675 state
->Const
.MaxGeometryAtomicCounters
);
677 if (state
->is_version(400, 0) ||
678 state
->ARB_tessellation_shader_enable
) {
679 add_const("gl_MaxTessControlAtomicCounters",
680 state
->Const
.MaxTessControlAtomicCounters
);
681 add_const("gl_MaxTessEvaluationAtomicCounters",
682 state
->Const
.MaxTessEvaluationAtomicCounters
);
687 if (state
->is_version(420, 310)) {
688 add_const("gl_MaxVertexAtomicCounterBuffers",
689 state
->Const
.MaxVertexAtomicCounterBuffers
);
690 add_const("gl_MaxFragmentAtomicCounterBuffers",
691 state
->Const
.MaxFragmentAtomicCounterBuffers
);
692 add_const("gl_MaxCombinedAtomicCounterBuffers",
693 state
->Const
.MaxCombinedAtomicCounterBuffers
);
694 add_const("gl_MaxAtomicCounterBufferSize",
695 state
->Const
.MaxAtomicCounterBufferSize
);
697 /* When Mesa adds support for GL_OES_geometry_shader and
698 * GL_OES_tessellation_shader, this will need to change.
700 if (!state
->es_shader
) {
701 add_const("gl_MaxGeometryAtomicCounterBuffers",
702 state
->Const
.MaxGeometryAtomicCounterBuffers
);
703 add_const("gl_MaxTessControlAtomicCounterBuffers",
704 state
->Const
.MaxTessControlAtomicCounterBuffers
);
705 add_const("gl_MaxTessEvaluationAtomicCounterBuffers",
706 state
->Const
.MaxTessEvaluationAtomicCounterBuffers
);
710 if (state
->is_version(430, 0) || state
->ARB_compute_shader_enable
) {
711 add_const("gl_MaxComputeAtomicCounterBuffers", MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS
);
712 add_const("gl_MaxComputeAtomicCounters", MAX_COMPUTE_ATOMIC_COUNTERS
);
713 add_const("gl_MaxComputeImageUniforms", MAX_COMPUTE_IMAGE_UNIFORMS
);
714 add_const("gl_MaxComputeTextureImageUnits", MAX_COMPUTE_TEXTURE_IMAGE_UNITS
);
715 add_const("gl_MaxComputeUniformComponents", MAX_COMPUTE_UNIFORM_COMPONENTS
);
717 add_const_ivec3("gl_MaxComputeWorkGroupCount",
718 state
->Const
.MaxComputeWorkGroupCount
[0],
719 state
->Const
.MaxComputeWorkGroupCount
[1],
720 state
->Const
.MaxComputeWorkGroupCount
[2]);
721 add_const_ivec3("gl_MaxComputeWorkGroupSize",
722 state
->Const
.MaxComputeWorkGroupSize
[0],
723 state
->Const
.MaxComputeWorkGroupSize
[1],
724 state
->Const
.MaxComputeWorkGroupSize
[2]);
726 /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
728 * The built-in constant gl_WorkGroupSize is a compute-shader
729 * constant containing the local work-group size of the shader. The
730 * size of the work group in the X, Y, and Z dimensions is stored in
731 * the x, y, and z components. The constants values in
732 * gl_WorkGroupSize will match those specified in the required
733 * local_size_x, local_size_y, and local_size_z layout qualifiers
734 * for the current shader. This is a constant so that it can be
735 * used to size arrays of memory that can be shared within the local
736 * work group. It is a compile-time error to use gl_WorkGroupSize
737 * in a shader that does not declare a fixed local group size, or
738 * before that shader has declared a fixed local group size, using
739 * local_size_x, local_size_y, and local_size_z.
741 * To prevent the shader from trying to refer to gl_WorkGroupSize before
742 * the layout declaration, we don't define it here. Intead we define it
743 * in ast_cs_input_layout::hir().
747 if (state
->is_version(420, 310) ||
748 state
->ARB_shader_image_load_store_enable
) {
749 add_const("gl_MaxImageUnits",
750 state
->Const
.MaxImageUnits
);
751 add_const("gl_MaxVertexImageUniforms",
752 state
->Const
.MaxVertexImageUniforms
);
753 add_const("gl_MaxFragmentImageUniforms",
754 state
->Const
.MaxFragmentImageUniforms
);
755 add_const("gl_MaxCombinedImageUniforms",
756 state
->Const
.MaxCombinedImageUniforms
);
758 if (!state
->es_shader
) {
759 add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
760 state
->Const
.MaxCombinedShaderOutputResources
);
761 add_const("gl_MaxImageSamples",
762 state
->Const
.MaxImageSamples
);
763 add_const("gl_MaxGeometryImageUniforms",
764 state
->Const
.MaxGeometryImageUniforms
);
767 if (state
->is_version(450, 310)) {
768 add_const("gl_MaxCombinedShaderOutputResources",
769 state
->Const
.MaxCombinedShaderOutputResources
);
772 if (state
->is_version(400, 0) ||
773 state
->ARB_tessellation_shader_enable
) {
774 add_const("gl_MaxTessControlImageUniforms",
775 state
->Const
.MaxTessControlImageUniforms
);
776 add_const("gl_MaxTessEvaluationImageUniforms",
777 state
->Const
.MaxTessEvaluationImageUniforms
);
781 if (state
->is_version(410, 0) ||
782 state
->ARB_viewport_array_enable
)
783 add_const("gl_MaxViewports", state
->Const
.MaxViewports
);
785 if (state
->is_version(400, 0) ||
786 state
->ARB_tessellation_shader_enable
) {
787 add_const("gl_MaxPatchVertices", state
->Const
.MaxPatchVertices
);
788 add_const("gl_MaxTessGenLevel", state
->Const
.MaxTessGenLevel
);
789 add_const("gl_MaxTessControlInputComponents", state
->Const
.MaxTessControlInputComponents
);
790 add_const("gl_MaxTessControlOutputComponents", state
->Const
.MaxTessControlOutputComponents
);
791 add_const("gl_MaxTessControlTextureImageUnits", state
->Const
.MaxTessControlTextureImageUnits
);
792 add_const("gl_MaxTessEvaluationInputComponents", state
->Const
.MaxTessEvaluationInputComponents
);
793 add_const("gl_MaxTessEvaluationOutputComponents", state
->Const
.MaxTessEvaluationOutputComponents
);
794 add_const("gl_MaxTessEvaluationTextureImageUnits", state
->Const
.MaxTessEvaluationTextureImageUnits
);
795 add_const("gl_MaxTessPatchComponents", state
->Const
.MaxTessPatchComponents
);
796 add_const("gl_MaxTessControlTotalOutputComponents", state
->Const
.MaxTessControlTotalOutputComponents
);
797 add_const("gl_MaxTessControlUniformComponents", state
->Const
.MaxTessControlUniformComponents
);
798 add_const("gl_MaxTessEvaluationUniformComponents", state
->Const
.MaxTessEvaluationUniformComponents
);
804 * Generate uniform variables (which exist in all types of shaders).
807 builtin_variable_generator::generate_uniforms()
809 if (state
->is_version(400, 0) || state
->ARB_sample_shading_enable
)
810 add_uniform(int_t
, "gl_NumSamples");
811 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
812 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
813 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
816 add_uniform(mat4_t
, "gl_ModelViewMatrix");
817 add_uniform(mat4_t
, "gl_ProjectionMatrix");
818 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
819 add_uniform(mat3_t
, "gl_NormalMatrix");
820 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
821 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
822 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
823 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
824 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
825 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
826 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
827 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
828 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
829 add_uniform(float_t
, "gl_NormalScale");
830 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
831 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
833 const glsl_type
*const mat4_array_type
=
834 array(mat4_t
, state
->Const
.MaxTextureCoords
);
835 add_uniform(mat4_array_type
, "gl_TextureMatrix");
836 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
837 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
838 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
840 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
841 add_uniform(type("gl_PointParameters"), "gl_Point");
843 const glsl_type
*const material_parameters_type
=
844 type("gl_MaterialParameters");
845 add_uniform(material_parameters_type
, "gl_FrontMaterial");
846 add_uniform(material_parameters_type
, "gl_BackMaterial");
848 add_uniform(array(type("gl_LightSourceParameters"),
849 state
->Const
.MaxLights
),
852 const glsl_type
*const light_model_products_type
=
853 type("gl_LightModelProducts");
854 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
855 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
857 const glsl_type
*const light_products_type
=
858 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
859 add_uniform(light_products_type
, "gl_FrontLightProduct");
860 add_uniform(light_products_type
, "gl_BackLightProduct");
862 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
863 "gl_TextureEnvColor");
865 const glsl_type
*const texcoords_vec4
=
866 array(vec4_t
, state
->Const
.MaxTextureCoords
);
867 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
868 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
869 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
870 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
871 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
872 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
873 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
874 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
876 add_uniform(type("gl_FogParameters"), "gl_Fog");
882 * Generate variables which only exist in vertex shaders.
885 builtin_variable_generator::generate_vs_special_vars()
887 if (state
->is_version(130, 300))
888 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
889 if (state
->ARB_draw_instanced_enable
)
890 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
891 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
892 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
893 if (state
->AMD_vertex_shader_layer_enable
)
894 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
895 if (state
->AMD_vertex_shader_viewport_index_enable
)
896 add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
898 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
899 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
900 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
901 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
902 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
903 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
904 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
905 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
906 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
907 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
908 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
909 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
910 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
916 * Generate variables which only exist in tessellation control shaders.
919 builtin_variable_generator::generate_tcs_special_vars()
921 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
922 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
923 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
925 add_output(VARYING_SLOT_TESS_LEVEL_OUTER
, array(float_t
, 4),
926 "gl_TessLevelOuter")->data
.patch
= 1;
927 add_output(VARYING_SLOT_TESS_LEVEL_INNER
, array(float_t
, 2),
928 "gl_TessLevelInner")->data
.patch
= 1;
933 * Generate variables which only exist in tessellation evaluation shaders.
936 builtin_variable_generator::generate_tes_special_vars()
938 add_system_value(SYSTEM_VALUE_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
939 add_system_value(SYSTEM_VALUE_VERTICES_IN
, int_t
, "gl_PatchVerticesIn");
940 add_system_value(SYSTEM_VALUE_TESS_COORD
, vec3_t
, "gl_TessCoord");
941 add_system_value(SYSTEM_VALUE_TESS_LEVEL_OUTER
, array(float_t
, 4),
942 "gl_TessLevelOuter");
943 add_system_value(SYSTEM_VALUE_TESS_LEVEL_INNER
, array(float_t
, 2),
944 "gl_TessLevelInner");
949 * Generate variables which only exist in geometry shaders.
952 builtin_variable_generator::generate_gs_special_vars()
954 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
955 if (state
->is_version(410, 0) || state
->ARB_viewport_array_enable
)
956 add_output(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
957 if (state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
)
958 add_system_value(SYSTEM_VALUE_INVOCATION_ID
, int_t
, "gl_InvocationID");
960 /* Although gl_PrimitiveID appears in tessellation control and tessellation
961 * evaluation shaders, it has a different function there than it has in
962 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
963 * as special geometry shader variables.
965 * Note that although the general convention of suffixing geometry shader
966 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
967 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
968 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
971 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
972 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
973 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
974 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
979 * Generate variables which only exist in fragment shaders.
982 builtin_variable_generator::generate_fs_special_vars()
984 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
985 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
986 if (state
->is_version(120, 100))
987 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
989 if (state
->is_version(150, 0)) {
991 add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
992 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
995 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
996 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
997 * They were removed from GLSL ES 3.00.
999 if (compatibility
|| !state
->is_version(420, 300)) {
1000 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
1001 add_output(FRAG_RESULT_DATA0
,
1002 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
1005 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
1008 if (state
->is_version(110, 300))
1009 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
1011 if (state
->ARB_shader_stencil_export_enable
) {
1012 ir_variable
*const var
=
1013 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
1014 if (state
->ARB_shader_stencil_export_warn
)
1015 var
->enable_extension_warning("GL_ARB_shader_stencil_export");
1018 if (state
->AMD_shader_stencil_export_enable
) {
1019 ir_variable
*const var
=
1020 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
1021 if (state
->AMD_shader_stencil_export_warn
)
1022 var
->enable_extension_warning("GL_AMD_shader_stencil_export");
1025 if (state
->is_version(400, 0) || state
->ARB_sample_shading_enable
) {
1026 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
1027 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
1028 /* From the ARB_sample_shading specification:
1029 * "The number of elements in the array is ceil(<s>/32), where
1030 * <s> is the maximum number of color samples supported by the
1032 * Since no drivers expose more than 32x MSAA, we can simply set
1033 * the array size to 1 rather than computing it.
1035 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
1038 if (state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) {
1039 add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN
, array(int_t
, 1), "gl_SampleMaskIn");
1042 if (state
->is_version(430, 0) || state
->ARB_fragment_layer_viewport_enable
) {
1043 add_input(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
1044 add_input(VARYING_SLOT_VIEWPORT
, int_t
, "gl_ViewportIndex");
1050 * Generate variables which only exist in compute shaders.
1053 builtin_variable_generator::generate_cs_special_vars()
1055 /* TODO: finish this. */
1060 * Add a single "varying" variable. The variable's type and direction (input
1061 * or output) are adjusted as appropriate for the type of shader being
1062 * compiled. For geometry shaders using {ARB,EXT}_geometry_shader4,
1063 * name_as_gs_input is used for the input (to avoid ambiguity).
1066 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
1068 const char *name_as_gs_input
)
1070 switch (state
->stage
) {
1071 case MESA_SHADER_TESS_CTRL
:
1072 case MESA_SHADER_TESS_EVAL
:
1073 case MESA_SHADER_GEOMETRY
:
1074 this->per_vertex_in
.add_field(slot
, type
, name
);
1076 case MESA_SHADER_VERTEX
:
1077 this->per_vertex_out
.add_field(slot
, type
, name
);
1079 case MESA_SHADER_FRAGMENT
:
1080 add_input(slot
, type
, name
);
1082 case MESA_SHADER_COMPUTE
:
1083 /* Compute shaders don't have varyings. */
1090 * Generate variables that are used to communicate data from one shader stage
1091 * to the next ("varyings").
1094 builtin_variable_generator::generate_varyings()
1096 #define ADD_VARYING(loc, type, name) \
1097 add_varying(loc, type, name, name "In")
1099 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
1100 if (state
->stage
!= MESA_SHADER_FRAGMENT
) {
1101 ADD_VARYING(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
1102 ADD_VARYING(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
1105 if (state
->is_version(130, 0)) {
1106 ADD_VARYING(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
1110 if (compatibility
) {
1111 ADD_VARYING(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
1112 ADD_VARYING(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
1113 if (state
->stage
== MESA_SHADER_FRAGMENT
) {
1114 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
1115 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
1117 ADD_VARYING(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
1118 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
1119 ADD_VARYING(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
1120 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
1121 ADD_VARYING(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
1125 /* Section 7.1 (Built-In Language Variables) of the GLSL 4.00 spec
1128 * "In the tessellation control language, built-in variables are
1129 * intrinsically declared as:
1133 * float gl_PointSize;
1134 * float gl_ClipDistance[];
1135 * } gl_in[gl_MaxPatchVertices];"
1137 if (state
->stage
== MESA_SHADER_TESS_CTRL
||
1138 state
->stage
== MESA_SHADER_TESS_EVAL
) {
1139 const glsl_type
*per_vertex_in_type
=
1140 this->per_vertex_in
.construct_interface_instance();
1141 add_variable("gl_in", array(per_vertex_in_type
, state
->Const
.MaxPatchVertices
),
1142 ir_var_shader_in
, -1);
1144 if (state
->stage
== MESA_SHADER_GEOMETRY
) {
1145 const glsl_type
*per_vertex_in_type
=
1146 this->per_vertex_in
.construct_interface_instance();
1147 add_variable("gl_in", array(per_vertex_in_type
, 0),
1148 ir_var_shader_in
, -1);
1150 if (state
->stage
== MESA_SHADER_TESS_CTRL
) {
1151 const glsl_type
*per_vertex_out_type
=
1152 this->per_vertex_out
.construct_interface_instance();
1153 add_variable("gl_out", array(per_vertex_out_type
, 0),
1154 ir_var_shader_out
, -1);
1156 if (state
->stage
== MESA_SHADER_VERTEX
||
1157 state
->stage
== MESA_SHADER_TESS_EVAL
||
1158 state
->stage
== MESA_SHADER_GEOMETRY
) {
1159 const glsl_type
*per_vertex_out_type
=
1160 this->per_vertex_out
.construct_interface_instance();
1161 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
1162 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
1164 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
1165 fields
[i
].location
);
1166 var
->data
.interpolation
= fields
[i
].interpolation
;
1167 var
->data
.centroid
= fields
[i
].centroid
;
1168 var
->data
.sample
= fields
[i
].sample
;
1169 var
->data
.patch
= fields
[i
].patch
;
1170 var
->init_interface_type(per_vertex_out_type
);
1176 }; /* Anonymous namespace */
1180 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1181 struct _mesa_glsl_parse_state
*state
)
1183 builtin_variable_generator
gen(instructions
, state
);
1185 gen
.generate_constants();
1186 gen
.generate_uniforms();
1188 gen
.generate_varyings();
1190 switch (state
->stage
) {
1191 case MESA_SHADER_VERTEX
:
1192 gen
.generate_vs_special_vars();
1194 case MESA_SHADER_TESS_CTRL
:
1195 gen
.generate_tcs_special_vars();
1197 case MESA_SHADER_TESS_EVAL
:
1198 gen
.generate_tes_special_vars();
1200 case MESA_SHADER_GEOMETRY
:
1201 gen
.generate_gs_special_vars();
1203 case MESA_SHADER_FRAGMENT
:
1204 gen
.generate_fs_special_vars();
1206 case MESA_SHADER_COMPUTE
:
1207 gen
.generate_cs_special_vars();