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_parameter.h"
30 #include "program/prog_statevars.h"
31 #include "program/prog_instruction.h"
33 static struct gl_builtin_uniform_element gl_NumSamples_elements
[] = {
34 {NULL
, {STATE_NUM_SAMPLES
, 0, 0}, SWIZZLE_XXXX
}
37 static struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
38 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
39 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
40 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
43 static struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
44 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
47 static struct gl_builtin_uniform_element gl_Point_elements
[] = {
48 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
49 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
50 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
51 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
52 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
53 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
54 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
57 static struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
58 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
59 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
60 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
61 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
62 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
65 static struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
66 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
67 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
68 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
69 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
70 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
73 static struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
74 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
75 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
76 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
77 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
78 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
79 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
80 MAKE_SWIZZLE4(SWIZZLE_X
,
84 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
85 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
86 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
87 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
88 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
89 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
92 static struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
93 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
96 static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
97 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
100 static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
101 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
104 static struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
105 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
106 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
107 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
110 static struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
111 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
112 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
113 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
116 static struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
117 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
120 static struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
121 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
124 static struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
125 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
128 static struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
129 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
132 static struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
133 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
136 static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
137 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
140 static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
141 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
144 static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
145 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
148 static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
149 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
152 static struct gl_builtin_uniform_element gl_Fog_elements
[] = {
153 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
154 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
155 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
156 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
157 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
160 static struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
161 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
164 static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements
[] = {
165 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_0
}, SWIZZLE_XYZW
},
168 static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements
[] = {
169 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_1
}, SWIZZLE_XYZW
},
172 static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
173 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
176 static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
177 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
180 static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
181 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
184 #define MATRIX(name, statevar, modifier) \
185 static struct gl_builtin_uniform_element name ## _elements[] = { \
186 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
187 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
188 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
189 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
192 MATRIX(gl_ModelViewMatrix
,
193 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
194 MATRIX(gl_ModelViewMatrixInverse
,
195 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
196 MATRIX(gl_ModelViewMatrixTranspose
,
197 STATE_MODELVIEW_MATRIX
, 0);
198 MATRIX(gl_ModelViewMatrixInverseTranspose
,
199 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
201 MATRIX(gl_ProjectionMatrix
,
202 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
203 MATRIX(gl_ProjectionMatrixInverse
,
204 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
205 MATRIX(gl_ProjectionMatrixTranspose
,
206 STATE_PROJECTION_MATRIX
, 0);
207 MATRIX(gl_ProjectionMatrixInverseTranspose
,
208 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
210 MATRIX(gl_ModelViewProjectionMatrix
,
211 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
212 MATRIX(gl_ModelViewProjectionMatrixInverse
,
213 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
214 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
215 STATE_MVP_MATRIX
, 0);
216 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
217 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
219 MATRIX(gl_TextureMatrix
,
220 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
221 MATRIX(gl_TextureMatrixInverse
,
222 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
223 MATRIX(gl_TextureMatrixTranspose
,
224 STATE_TEXTURE_MATRIX
, 0);
225 MATRIX(gl_TextureMatrixInverseTranspose
,
226 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
228 static struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
229 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
230 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
231 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
232 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
233 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
234 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
239 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
241 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
242 STATEVAR(gl_NumSamples
),
243 STATEVAR(gl_DepthRange
),
244 STATEVAR(gl_ClipPlane
),
246 STATEVAR(gl_FrontMaterial
),
247 STATEVAR(gl_BackMaterial
),
248 STATEVAR(gl_LightSource
),
249 STATEVAR(gl_LightModel
),
250 STATEVAR(gl_FrontLightModelProduct
),
251 STATEVAR(gl_BackLightModelProduct
),
252 STATEVAR(gl_FrontLightProduct
),
253 STATEVAR(gl_BackLightProduct
),
254 STATEVAR(gl_TextureEnvColor
),
255 STATEVAR(gl_EyePlaneS
),
256 STATEVAR(gl_EyePlaneT
),
257 STATEVAR(gl_EyePlaneR
),
258 STATEVAR(gl_EyePlaneQ
),
259 STATEVAR(gl_ObjectPlaneS
),
260 STATEVAR(gl_ObjectPlaneT
),
261 STATEVAR(gl_ObjectPlaneR
),
262 STATEVAR(gl_ObjectPlaneQ
),
265 STATEVAR(gl_ModelViewMatrix
),
266 STATEVAR(gl_ModelViewMatrixInverse
),
267 STATEVAR(gl_ModelViewMatrixTranspose
),
268 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
270 STATEVAR(gl_ProjectionMatrix
),
271 STATEVAR(gl_ProjectionMatrixInverse
),
272 STATEVAR(gl_ProjectionMatrixTranspose
),
273 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
275 STATEVAR(gl_ModelViewProjectionMatrix
),
276 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
277 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
278 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
280 STATEVAR(gl_TextureMatrix
),
281 STATEVAR(gl_TextureMatrixInverse
),
282 STATEVAR(gl_TextureMatrixTranspose
),
283 STATEVAR(gl_TextureMatrixInverseTranspose
),
285 STATEVAR(gl_NormalMatrix
),
286 STATEVAR(gl_NormalScale
),
288 STATEVAR(gl_BumpRotMatrix0MESA
),
289 STATEVAR(gl_BumpRotMatrix1MESA
),
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
[10];
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
].row_major
= false;
332 this->fields
[this->num_fields
].location
= slot
;
333 this->fields
[this->num_fields
].interpolation
= INTERP_QUALIFIER_NONE
;
334 this->fields
[this->num_fields
].centroid
= 0;
335 this->fields
[this->num_fields
].sample
= 0;
341 per_vertex_accumulator::construct_interface_instance() const
343 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
344 GLSL_INTERFACE_PACKING_STD140
,
349 class builtin_variable_generator
352 builtin_variable_generator(exec_list
*instructions
,
353 struct _mesa_glsl_parse_state
*state
);
354 void generate_constants();
355 void generate_uniforms();
356 void generate_vs_special_vars();
357 void generate_gs_special_vars();
358 void generate_fs_special_vars();
359 void generate_varyings();
362 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
364 return glsl_type::get_array_instance(base
, elements
);
367 const glsl_type
*type(const char *name
)
369 return symtab
->get_type(name
);
372 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
374 return add_variable(name
, type
, ir_var_shader_in
, slot
);
377 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
379 return add_variable(name
, type
, ir_var_shader_out
, slot
);
382 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
385 return add_variable(name
, type
, ir_var_system_value
, slot
);
388 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
389 enum ir_variable_mode mode
, int slot
);
390 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
391 ir_variable
*add_const(const char *name
, int value
);
392 void add_varying(int slot
, const glsl_type
*type
, const char *name
,
393 const char *name_as_gs_input
);
395 exec_list
* const instructions
;
396 struct _mesa_glsl_parse_state
* const state
;
397 glsl_symbol_table
* const symtab
;
400 * True if compatibility-profile-only variables should be included. (In
401 * desktop GL, these are always included when the GLSL version is 1.30 and
404 const bool compatibility
;
406 const glsl_type
* const bool_t
;
407 const glsl_type
* const int_t
;
408 const glsl_type
* const float_t
;
409 const glsl_type
* const vec2_t
;
410 const glsl_type
* const vec3_t
;
411 const glsl_type
* const vec4_t
;
412 const glsl_type
* const mat3_t
;
413 const glsl_type
* const mat4_t
;
415 per_vertex_accumulator per_vertex_in
;
416 per_vertex_accumulator per_vertex_out
;
420 builtin_variable_generator::builtin_variable_generator(
421 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
422 : instructions(instructions
), state(state
), symtab(state
->symbols
),
423 compatibility(!state
->is_version(140, 100)),
424 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_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 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
433 builtin_variable_generator::add_variable(const char *name
,
434 const glsl_type
*type
,
435 enum ir_variable_mode mode
, int slot
)
437 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
438 var
->how_declared
= ir_var_declared_implicitly
;
442 case ir_var_shader_in
:
444 case ir_var_system_value
:
445 var
->read_only
= true;
447 case ir_var_shader_out
:
450 /* The only variables that are added using this function should be
451 * uniforms, shader inputs, and shader outputs, constants (which use
452 * ir_var_auto), and system values.
458 var
->location
= slot
;
459 var
->explicit_location
= (slot
>= 0);
460 var
->explicit_index
= 0;
462 /* Once the variable is created an initialized, add it to the symbol table
463 * and add the declaration to the IR stream.
465 instructions
->push_tail(var
);
467 symtab
->add_variable(var
);
473 builtin_variable_generator::add_uniform(const glsl_type
*type
,
476 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
479 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
480 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
485 assert(_mesa_builtin_uniform_desc
[i
].name
!= NULL
);
486 const struct gl_builtin_uniform_desc
* const statevar
=
487 &_mesa_builtin_uniform_desc
[i
];
489 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
490 uni
->num_state_slots
= array_count
* statevar
->num_elements
;
492 ir_state_slot
*slots
=
493 ralloc_array(uni
, ir_state_slot
, uni
->num_state_slots
);
495 uni
->state_slots
= slots
;
497 for (unsigned a
= 0; a
< array_count
; a
++) {
498 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
499 struct gl_builtin_uniform_element
*element
= &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
->has_initializer
= true;
533 builtin_variable_generator::generate_constants()
535 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
536 add_const("gl_MaxVertexTextureImageUnits",
537 state
->Const
.MaxVertexTextureImageUnits
);
538 add_const("gl_MaxCombinedTextureImageUnits",
539 state
->Const
.MaxCombinedTextureImageUnits
);
540 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
541 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
543 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
544 * GL counts them in units of "components" or "floats".
546 if (state
->es_shader
) {
547 add_const("gl_MaxVertexUniformVectors",
548 state
->Const
.MaxVertexUniformComponents
/ 4);
549 add_const("gl_MaxFragmentUniformVectors",
550 state
->Const
.MaxFragmentUniformComponents
/ 4);
552 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
553 * vertex and fragment shader constants.
555 if (state
->is_version(0, 300)) {
556 add_const("gl_MaxVertexOutputVectors",
557 state
->ctx
->Const
.VertexProgram
.MaxOutputComponents
/ 4);
558 add_const("gl_MaxFragmentInputVectors",
559 state
->ctx
->Const
.FragmentProgram
.MaxInputComponents
/ 4);
561 add_const("gl_MaxVaryingVectors",
562 state
->ctx
->Const
.MaxVarying
);
565 add_const("gl_MaxVertexUniformComponents",
566 state
->Const
.MaxVertexUniformComponents
);
568 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
571 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
573 add_const("gl_MaxFragmentUniformComponents",
574 state
->Const
.MaxFragmentUniformComponents
);
577 /* Texel offsets were introduced in ARB_shading_language_420pack (which
578 * requires desktop GLSL version 130), and adopted into desktop GLSL
579 * version 4.20 and GLSL ES version 3.00.
581 if ((state
->is_version(130, 0) &&
582 state
->ARB_shading_language_420pack_enable
) ||
583 state
->is_version(420, 300)) {
584 add_const("gl_MinProgramTexelOffset",
585 state
->Const
.MinProgramTexelOffset
);
586 add_const("gl_MaxProgramTexelOffset",
587 state
->Const
.MaxProgramTexelOffset
);
590 if (state
->is_version(130, 0)) {
591 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
592 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
595 if (state
->is_version(150, 0)) {
596 add_const("gl_MaxVertexOutputComponents",
597 state
->Const
.MaxVertexOutputComponents
);
598 add_const("gl_MaxGeometryInputComponents",
599 state
->Const
.MaxGeometryInputComponents
);
600 add_const("gl_MaxGeometryOutputComponents",
601 state
->Const
.MaxGeometryOutputComponents
);
602 add_const("gl_MaxFragmentInputComponents",
603 state
->Const
.MaxFragmentInputComponents
);
604 add_const("gl_MaxGeometryTextureImageUnits",
605 state
->Const
.MaxGeometryTextureImageUnits
);
606 add_const("gl_MaxGeometryOutputVertices",
607 state
->Const
.MaxGeometryOutputVertices
);
608 add_const("gl_MaxGeometryTotalOutputComponents",
609 state
->Const
.MaxGeometryTotalOutputComponents
);
610 add_const("gl_MaxGeometryUniformComponents",
611 state
->Const
.MaxGeometryUniformComponents
);
613 /* Note: the GLSL 1.50-4.40 specs require
614 * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
615 * But they do not define what it means (and there does not appear to be
616 * any corresponding constant in the GL specs). However,
617 * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
618 * be the maximum number of components available for use as geometry
619 * outputs. So we assume this is a synonym for
620 * gl_MaxGeometryOutputComponents.
622 add_const("gl_MaxGeometryVaryingComponents",
623 state
->Const
.MaxGeometryOutputComponents
);
627 /* Note: gl_MaxLights stopped being listed as an explicit constant in
628 * GLSL 1.30, however it continues to be referred to (as a minimum size
629 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
630 * this seems like it was probably an oversight.
632 add_const("gl_MaxLights", state
->Const
.MaxLights
);
634 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
636 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
637 * 1.50, however this seems like it was probably an oversight.
639 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
641 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
642 * re-introduced in GLSL 1.50, so this seems like it was probably an
645 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
648 if (state
->ARB_shader_atomic_counters_enable
) {
649 add_const("gl_MaxVertexAtomicCounters",
650 state
->Const
.MaxVertexAtomicCounters
);
651 add_const("gl_MaxGeometryAtomicCounters",
652 state
->Const
.MaxGeometryAtomicCounters
);
653 add_const("gl_MaxFragmentAtomicCounters",
654 state
->Const
.MaxFragmentAtomicCounters
);
655 add_const("gl_MaxCombinedAtomicCounters",
656 state
->Const
.MaxCombinedAtomicCounters
);
657 add_const("gl_MaxAtomicCounterBindings",
658 state
->Const
.MaxAtomicBufferBindings
);
659 add_const("gl_MaxTessControlAtomicCounters", 0);
660 add_const("gl_MaxTessEvaluationAtomicCounters", 0);
666 * Generate uniform variables (which exist in all types of shaders).
669 builtin_variable_generator::generate_uniforms()
671 add_uniform(int_t
, "gl_NumSamples");
672 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
673 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
674 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
677 add_uniform(mat4_t
, "gl_ModelViewMatrix");
678 add_uniform(mat4_t
, "gl_ProjectionMatrix");
679 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
680 add_uniform(mat3_t
, "gl_NormalMatrix");
681 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
682 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
683 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
684 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
685 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
686 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
687 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
688 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
689 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
690 add_uniform(float_t
, "gl_NormalScale");
691 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
692 add_uniform(vec2_t
, "gl_BumpRotMatrix0MESA");
693 add_uniform(vec2_t
, "gl_BumpRotMatrix1MESA");
694 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
696 const glsl_type
*const mat4_array_type
=
697 array(mat4_t
, state
->Const
.MaxTextureCoords
);
698 add_uniform(mat4_array_type
, "gl_TextureMatrix");
699 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
700 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
701 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
703 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
704 add_uniform(type("gl_PointParameters"), "gl_Point");
706 const glsl_type
*const material_parameters_type
=
707 type("gl_MaterialParameters");
708 add_uniform(material_parameters_type
, "gl_FrontMaterial");
709 add_uniform(material_parameters_type
, "gl_BackMaterial");
711 add_uniform(array(type("gl_LightSourceParameters"),
712 state
->Const
.MaxLights
),
715 const glsl_type
*const light_model_products_type
=
716 type("gl_LightModelProducts");
717 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
718 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
720 const glsl_type
*const light_products_type
=
721 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
722 add_uniform(light_products_type
, "gl_FrontLightProduct");
723 add_uniform(light_products_type
, "gl_BackLightProduct");
725 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
726 "gl_TextureEnvColor");
728 const glsl_type
*const texcoords_vec4
=
729 array(vec4_t
, state
->Const
.MaxTextureCoords
);
730 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
731 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
732 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
733 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
734 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
735 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
736 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
737 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
739 add_uniform(type("gl_FogParameters"), "gl_Fog");
745 * Generate variables which only exist in vertex shaders.
748 builtin_variable_generator::generate_vs_special_vars()
750 if (state
->is_version(130, 300))
751 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
752 if (state
->ARB_draw_instanced_enable
)
753 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
754 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
755 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
756 if (state
->AMD_vertex_shader_layer_enable
)
757 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
759 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
760 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
761 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
762 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
763 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
764 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
765 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
766 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
767 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
768 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
769 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
770 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
771 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
777 * Generate variables which only exist in geometry shaders.
780 builtin_variable_generator::generate_gs_special_vars()
782 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
784 /* Although gl_PrimitiveID appears in tessellation control and tessellation
785 * evaluation shaders, it has a different function there than it has in
786 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
787 * as special geometry shader variables.
789 * Note that although the general convention of suffixing geometry shader
790 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
791 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
792 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
795 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
796 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
797 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
798 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
803 * Generate variables which only exist in fragment shaders.
806 builtin_variable_generator::generate_fs_special_vars()
808 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
809 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
810 if (state
->is_version(120, 100))
811 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
813 if (state
->is_version(150, 0)) {
815 add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
816 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
819 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
820 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
821 * They were removed from GLSL ES 3.00.
823 if (compatibility
|| !state
->is_version(420, 300)) {
824 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
825 add_output(FRAG_RESULT_DATA0
,
826 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
829 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
832 if (state
->is_version(110, 300))
833 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
835 if (state
->ARB_shader_stencil_export_enable
) {
836 ir_variable
*const var
=
837 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
838 if (state
->ARB_shader_stencil_export_warn
)
839 var
->warn_extension
= "GL_ARB_shader_stencil_export";
842 if (state
->AMD_shader_stencil_export_enable
) {
843 ir_variable
*const var
=
844 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
845 if (state
->AMD_shader_stencil_export_warn
)
846 var
->warn_extension
= "GL_AMD_shader_stencil_export";
849 if (state
->ARB_sample_shading_enable
) {
850 add_system_value(SYSTEM_VALUE_SAMPLE_ID
, int_t
, "gl_SampleID");
851 add_system_value(SYSTEM_VALUE_SAMPLE_POS
, vec2_t
, "gl_SamplePosition");
852 /* From the ARB_sample_shading specification:
853 * "The number of elements in the array is ceil(<s>/32), where
854 * <s> is the maximum number of color samples supported by the
856 * Since no drivers expose more than 32x MSAA, we can simply set
857 * the array size to 1 rather than computing it.
859 add_output(FRAG_RESULT_SAMPLE_MASK
, array(int_t
, 1), "gl_SampleMask");
865 * Add a single "varying" variable. The variable's type and direction (input
866 * or output) are adjusted as appropriate for the type of shader being
867 * compiled. For geometry shaders using {ARB,EXT}_geometry_shader4,
868 * name_as_gs_input is used for the input (to avoid ambiguity).
871 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
873 const char *name_as_gs_input
)
875 switch (state
->target
) {
876 case geometry_shader
:
877 this->per_vertex_in
.add_field(slot
, type
, name
);
880 this->per_vertex_out
.add_field(slot
, type
, name
);
882 case fragment_shader
:
883 add_input(slot
, type
, name
);
890 * Generate variables that are used to communicate data from one shader stage
891 * to the next ("varyings").
894 builtin_variable_generator::generate_varyings()
896 #define ADD_VARYING(loc, type, name) \
897 add_varying(loc, type, name, name "In")
899 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
900 if (state
->target
!= fragment_shader
) {
901 ADD_VARYING(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
902 ADD_VARYING(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
905 if (state
->is_version(130, 0)) {
906 ADD_VARYING(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
911 ADD_VARYING(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
912 ADD_VARYING(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
913 if (state
->target
== fragment_shader
) {
914 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
915 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
917 ADD_VARYING(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
918 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
919 ADD_VARYING(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
920 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
921 ADD_VARYING(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
925 if (state
->target
== geometry_shader
) {
926 const glsl_type
*per_vertex_in_type
=
927 this->per_vertex_in
.construct_interface_instance();
928 add_variable("gl_in", array(per_vertex_in_type
, 0),
929 ir_var_shader_in
, -1);
931 if (state
->target
== vertex_shader
|| state
->target
== geometry_shader
) {
932 const glsl_type
*per_vertex_out_type
=
933 this->per_vertex_out
.construct_interface_instance();
934 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
935 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
937 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
939 var
->interpolation
= fields
[i
].interpolation
;
940 var
->centroid
= fields
[i
].centroid
;
941 var
->sample
= fields
[i
].sample
;
942 var
->init_interface_type(per_vertex_out_type
);
948 }; /* Anonymous namespace */
952 _mesa_glsl_initialize_variables(exec_list
*instructions
,
953 struct _mesa_glsl_parse_state
*state
)
955 builtin_variable_generator
gen(instructions
, state
);
957 gen
.generate_constants();
958 gen
.generate_uniforms();
960 gen
.generate_varyings();
962 switch (state
->target
) {
964 gen
.generate_vs_special_vars();
966 case geometry_shader
:
967 gen
.generate_gs_special_vars();
969 case fragment_shader
:
970 gen
.generate_fs_special_vars();