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"
34 static struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
35 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
36 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
37 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
40 static struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
41 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
44 static struct gl_builtin_uniform_element gl_Point_elements
[] = {
45 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
46 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
47 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
48 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
49 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
50 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
51 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
54 static struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
55 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
56 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
57 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
58 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
59 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
62 static struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
63 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
64 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
65 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
66 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
67 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
70 static struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
71 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
72 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
73 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
74 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
75 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
76 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
77 MAKE_SWIZZLE4(SWIZZLE_X
,
81 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
82 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
83 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
84 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
85 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
86 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
89 static struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
90 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
93 static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
94 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
97 static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
98 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
101 static struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
102 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
103 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
104 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
107 static struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
108 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
109 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
110 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
113 static struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
114 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
117 static struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
118 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
121 static struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
122 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
125 static struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
126 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
129 static struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
130 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
133 static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
134 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
137 static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
138 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
141 static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
142 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
145 static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
146 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
149 static struct gl_builtin_uniform_element gl_Fog_elements
[] = {
150 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
151 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
152 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
153 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
154 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
157 static struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
158 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
161 static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements
[] = {
162 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_0
}, SWIZZLE_XYZW
},
165 static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements
[] = {
166 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_1
}, SWIZZLE_XYZW
},
169 static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
170 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
173 static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
174 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
177 static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
178 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
181 #define MATRIX(name, statevar, modifier) \
182 static struct gl_builtin_uniform_element name ## _elements[] = { \
183 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
184 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
185 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
186 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
189 MATRIX(gl_ModelViewMatrix
,
190 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
191 MATRIX(gl_ModelViewMatrixInverse
,
192 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
193 MATRIX(gl_ModelViewMatrixTranspose
,
194 STATE_MODELVIEW_MATRIX
, 0);
195 MATRIX(gl_ModelViewMatrixInverseTranspose
,
196 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
198 MATRIX(gl_ProjectionMatrix
,
199 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
200 MATRIX(gl_ProjectionMatrixInverse
,
201 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
202 MATRIX(gl_ProjectionMatrixTranspose
,
203 STATE_PROJECTION_MATRIX
, 0);
204 MATRIX(gl_ProjectionMatrixInverseTranspose
,
205 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
207 MATRIX(gl_ModelViewProjectionMatrix
,
208 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
209 MATRIX(gl_ModelViewProjectionMatrixInverse
,
210 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
211 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
212 STATE_MVP_MATRIX
, 0);
213 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
214 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
216 MATRIX(gl_TextureMatrix
,
217 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
218 MATRIX(gl_TextureMatrixInverse
,
219 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
220 MATRIX(gl_TextureMatrixTranspose
,
221 STATE_TEXTURE_MATRIX
, 0);
222 MATRIX(gl_TextureMatrixInverseTranspose
,
223 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
225 static struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
226 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
227 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
228 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
229 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
230 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
231 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
236 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
238 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
239 STATEVAR(gl_DepthRange
),
240 STATEVAR(gl_ClipPlane
),
242 STATEVAR(gl_FrontMaterial
),
243 STATEVAR(gl_BackMaterial
),
244 STATEVAR(gl_LightSource
),
245 STATEVAR(gl_LightModel
),
246 STATEVAR(gl_FrontLightModelProduct
),
247 STATEVAR(gl_BackLightModelProduct
),
248 STATEVAR(gl_FrontLightProduct
),
249 STATEVAR(gl_BackLightProduct
),
250 STATEVAR(gl_TextureEnvColor
),
251 STATEVAR(gl_EyePlaneS
),
252 STATEVAR(gl_EyePlaneT
),
253 STATEVAR(gl_EyePlaneR
),
254 STATEVAR(gl_EyePlaneQ
),
255 STATEVAR(gl_ObjectPlaneS
),
256 STATEVAR(gl_ObjectPlaneT
),
257 STATEVAR(gl_ObjectPlaneR
),
258 STATEVAR(gl_ObjectPlaneQ
),
261 STATEVAR(gl_ModelViewMatrix
),
262 STATEVAR(gl_ModelViewMatrixInverse
),
263 STATEVAR(gl_ModelViewMatrixTranspose
),
264 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
266 STATEVAR(gl_ProjectionMatrix
),
267 STATEVAR(gl_ProjectionMatrixInverse
),
268 STATEVAR(gl_ProjectionMatrixTranspose
),
269 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
271 STATEVAR(gl_ModelViewProjectionMatrix
),
272 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
273 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
274 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
276 STATEVAR(gl_TextureMatrix
),
277 STATEVAR(gl_TextureMatrixInverse
),
278 STATEVAR(gl_TextureMatrixTranspose
),
279 STATEVAR(gl_TextureMatrixInverseTranspose
),
281 STATEVAR(gl_NormalMatrix
),
282 STATEVAR(gl_NormalScale
),
284 STATEVAR(gl_BumpRotMatrix0MESA
),
285 STATEVAR(gl_BumpRotMatrix1MESA
),
286 STATEVAR(gl_FogParamsOptimizedMESA
),
287 STATEVAR(gl_CurrentAttribVertMESA
),
288 STATEVAR(gl_CurrentAttribFragMESA
),
297 * Data structure that accumulates fields for the gl_PerVertex interface
300 class per_vertex_accumulator
303 per_vertex_accumulator();
304 void add_field(int slot
, const glsl_type
*type
, const char *name
);
305 const glsl_type
*construct_interface_instance() const;
308 glsl_struct_field fields
[10];
313 per_vertex_accumulator::per_vertex_accumulator()
320 per_vertex_accumulator::add_field(int slot
, const glsl_type
*type
,
323 assert(this->num_fields
< ARRAY_SIZE(this->fields
));
324 this->fields
[this->num_fields
].type
= type
;
325 this->fields
[this->num_fields
].name
= name
;
326 this->fields
[this->num_fields
].row_major
= false;
327 this->fields
[this->num_fields
].location
= slot
;
333 per_vertex_accumulator::construct_interface_instance() const
335 return glsl_type::get_interface_instance(this->fields
, this->num_fields
,
336 GLSL_INTERFACE_PACKING_STD140
,
341 class builtin_variable_generator
344 builtin_variable_generator(exec_list
*instructions
,
345 struct _mesa_glsl_parse_state
*state
);
346 void generate_constants();
347 void generate_uniforms();
348 void generate_vs_special_vars();
349 void generate_gs_special_vars();
350 void generate_fs_special_vars();
351 void generate_varyings();
354 const glsl_type
*array(const glsl_type
*base
, unsigned elements
)
356 return glsl_type::get_array_instance(base
, elements
);
359 const glsl_type
*type(const char *name
)
361 return symtab
->get_type(name
);
364 ir_variable
*add_input(int slot
, const glsl_type
*type
, const char *name
)
366 return add_variable(name
, type
, ir_var_shader_in
, slot
);
369 ir_variable
*add_output(int slot
, const glsl_type
*type
, const char *name
)
371 return add_variable(name
, type
, ir_var_shader_out
, slot
);
374 ir_variable
*add_system_value(int slot
, const glsl_type
*type
,
377 return add_variable(name
, type
, ir_var_system_value
, slot
);
380 ir_variable
*add_variable(const char *name
, const glsl_type
*type
,
381 enum ir_variable_mode mode
, int slot
);
382 ir_variable
*add_uniform(const glsl_type
*type
, const char *name
);
383 ir_variable
*add_const(const char *name
, int value
);
384 void add_varying(int slot
, const glsl_type
*type
, const char *name
,
385 const char *name_as_gs_input
);
387 exec_list
* const instructions
;
388 struct _mesa_glsl_parse_state
* const state
;
389 glsl_symbol_table
* const symtab
;
392 * True if compatibility-profile-only variables should be included. (In
393 * desktop GL, these are always included when the GLSL version is 1.30 and
396 const bool compatibility
;
398 const glsl_type
* const bool_t
;
399 const glsl_type
* const int_t
;
400 const glsl_type
* const float_t
;
401 const glsl_type
* const vec2_t
;
402 const glsl_type
* const vec3_t
;
403 const glsl_type
* const vec4_t
;
404 const glsl_type
* const mat3_t
;
405 const glsl_type
* const mat4_t
;
407 per_vertex_accumulator per_vertex_in
;
408 per_vertex_accumulator per_vertex_out
;
412 builtin_variable_generator::builtin_variable_generator(
413 exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
414 : instructions(instructions
), state(state
), symtab(state
->symbols
),
415 compatibility(!state
->is_version(140, 100)),
416 bool_t(glsl_type::bool_type
), int_t(glsl_type::int_type
),
417 float_t(glsl_type::float_type
), vec2_t(glsl_type::vec2_type
),
418 vec3_t(glsl_type::vec3_type
), vec4_t(glsl_type::vec4_type
),
419 mat3_t(glsl_type::mat3_type
), mat4_t(glsl_type::mat4_type
)
425 builtin_variable_generator::add_variable(const char *name
,
426 const glsl_type
*type
,
427 enum ir_variable_mode mode
, int slot
)
429 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
433 case ir_var_shader_in
:
435 case ir_var_system_value
:
436 var
->read_only
= true;
438 case ir_var_shader_out
:
441 /* The only variables that are added using this function should be
442 * uniforms, shader inputs, and shader outputs, constants (which use
443 * ir_var_auto), and system values.
449 var
->location
= slot
;
450 var
->explicit_location
= (slot
>= 0);
451 var
->explicit_index
= 0;
453 /* Once the variable is created an initialized, add it to the symbol table
454 * and add the declaration to the IR stream.
456 instructions
->push_tail(var
);
458 symtab
->add_variable(var
);
464 builtin_variable_generator::add_uniform(const glsl_type
*type
,
467 ir_variable
*const uni
= add_variable(name
, type
, ir_var_uniform
, -1);
470 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
471 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
476 assert(_mesa_builtin_uniform_desc
[i
].name
!= NULL
);
477 const struct gl_builtin_uniform_desc
* const statevar
=
478 &_mesa_builtin_uniform_desc
[i
];
480 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
481 uni
->num_state_slots
= array_count
* statevar
->num_elements
;
483 ir_state_slot
*slots
=
484 ralloc_array(uni
, ir_state_slot
, uni
->num_state_slots
);
486 uni
->state_slots
= slots
;
488 for (unsigned a
= 0; a
< array_count
; a
++) {
489 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
490 struct gl_builtin_uniform_element
*element
= &statevar
->elements
[j
];
492 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
493 if (type
->is_array()) {
494 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
495 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
496 slots
->tokens
[2] = a
;
498 slots
->tokens
[1] = a
;
502 slots
->swizzle
= element
->swizzle
;
512 builtin_variable_generator::add_const(const char *name
, int value
)
514 ir_variable
*const var
= add_variable(name
, glsl_type::int_type
,
516 var
->constant_value
= new(var
) ir_constant(value
);
517 var
->constant_initializer
= new(var
) ir_constant(value
);
518 var
->has_initializer
= true;
524 builtin_variable_generator::generate_constants()
526 add_const("gl_MaxVertexAttribs", state
->Const
.MaxVertexAttribs
);
527 add_const("gl_MaxVertexTextureImageUnits",
528 state
->Const
.MaxVertexTextureImageUnits
);
529 add_const("gl_MaxCombinedTextureImageUnits",
530 state
->Const
.MaxCombinedTextureImageUnits
);
531 add_const("gl_MaxTextureImageUnits", state
->Const
.MaxTextureImageUnits
);
532 add_const("gl_MaxDrawBuffers", state
->Const
.MaxDrawBuffers
);
534 /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
535 * GL counts them in units of "components" or "floats".
537 if (state
->es_shader
) {
538 add_const("gl_MaxVertexUniformVectors",
539 state
->Const
.MaxVertexUniformComponents
/ 4);
540 add_const("gl_MaxFragmentUniformVectors",
541 state
->Const
.MaxFragmentUniformComponents
/ 4);
543 /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
544 * vertex and fragment shader constants.
546 if (state
->is_version(0, 300)) {
547 add_const("gl_MaxVertexOutputVectors",
548 state
->ctx
->Const
.VertexProgram
.MaxOutputComponents
/ 4);
549 add_const("gl_MaxFragmentInputVectors",
550 state
->ctx
->Const
.FragmentProgram
.MaxInputComponents
/ 4);
552 add_const("gl_MaxVaryingVectors",
553 state
->ctx
->Const
.MaxVarying
);
556 add_const("gl_MaxVertexUniformComponents",
557 state
->Const
.MaxVertexUniformComponents
);
559 /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
562 add_const("gl_MaxVaryingFloats", state
->ctx
->Const
.MaxVarying
* 4);
564 add_const("gl_MaxFragmentUniformComponents",
565 state
->Const
.MaxFragmentUniformComponents
);
568 /* Texel offsets were introduced in ARB_shading_language_420pack (which
569 * requires desktop GLSL version 130), and adopted into desktop GLSL
570 * version 4.20 and GLSL ES version 3.00.
572 if ((state
->is_version(130, 0) &&
573 state
->ARB_shading_language_420pack_enable
) ||
574 state
->is_version(420, 300)) {
575 add_const("gl_MinProgramTexelOffset",
576 state
->Const
.MinProgramTexelOffset
);
577 add_const("gl_MaxProgramTexelOffset",
578 state
->Const
.MaxProgramTexelOffset
);
581 if (state
->is_version(130, 0)) {
582 add_const("gl_MaxClipDistances", state
->Const
.MaxClipPlanes
);
583 add_const("gl_MaxVaryingComponents", state
->ctx
->Const
.MaxVarying
* 4);
587 /* Note: gl_MaxLights stopped being listed as an explicit constant in
588 * GLSL 1.30, however it continues to be referred to (as a minimum size
589 * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
590 * this seems like it was probably an oversight.
592 add_const("gl_MaxLights", state
->Const
.MaxLights
);
594 add_const("gl_MaxClipPlanes", state
->Const
.MaxClipPlanes
);
596 /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
597 * 1.50, however this seems like it was probably an oversight.
599 add_const("gl_MaxTextureUnits", state
->Const
.MaxTextureUnits
);
601 /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
602 * re-introduced in GLSL 1.50, so this seems like it was probably an
605 add_const("gl_MaxTextureCoords", state
->Const
.MaxTextureCoords
);
611 * Generate uniform variables (which exist in all types of shaders).
614 builtin_variable_generator::generate_uniforms()
616 add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
617 add_uniform(array(vec4_t
, VERT_ATTRIB_MAX
), "gl_CurrentAttribVertMESA");
618 add_uniform(array(vec4_t
, VARYING_SLOT_MAX
), "gl_CurrentAttribFragMESA");
621 add_uniform(mat4_t
, "gl_ModelViewMatrix");
622 add_uniform(mat4_t
, "gl_ProjectionMatrix");
623 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrix");
624 add_uniform(mat3_t
, "gl_NormalMatrix");
625 add_uniform(mat4_t
, "gl_ModelViewMatrixInverse");
626 add_uniform(mat4_t
, "gl_ProjectionMatrixInverse");
627 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverse");
628 add_uniform(mat4_t
, "gl_ModelViewMatrixTranspose");
629 add_uniform(mat4_t
, "gl_ProjectionMatrixTranspose");
630 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixTranspose");
631 add_uniform(mat4_t
, "gl_ModelViewMatrixInverseTranspose");
632 add_uniform(mat4_t
, "gl_ProjectionMatrixInverseTranspose");
633 add_uniform(mat4_t
, "gl_ModelViewProjectionMatrixInverseTranspose");
634 add_uniform(float_t
, "gl_NormalScale");
635 add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
636 add_uniform(vec2_t
, "gl_BumpRotMatrix0MESA");
637 add_uniform(vec2_t
, "gl_BumpRotMatrix1MESA");
638 add_uniform(vec4_t
, "gl_FogParamsOptimizedMESA");
640 const glsl_type
*const mat4_array_type
=
641 array(mat4_t
, state
->Const
.MaxTextureCoords
);
642 add_uniform(mat4_array_type
, "gl_TextureMatrix");
643 add_uniform(mat4_array_type
, "gl_TextureMatrixInverse");
644 add_uniform(mat4_array_type
, "gl_TextureMatrixTranspose");
645 add_uniform(mat4_array_type
, "gl_TextureMatrixInverseTranspose");
647 add_uniform(array(vec4_t
, state
->Const
.MaxClipPlanes
), "gl_ClipPlane");
648 add_uniform(type("gl_PointParameters"), "gl_Point");
650 const glsl_type
*const material_parameters_type
=
651 type("gl_MaterialParameters");
652 add_uniform(material_parameters_type
, "gl_FrontMaterial");
653 add_uniform(material_parameters_type
, "gl_BackMaterial");
655 add_uniform(array(type("gl_LightSourceParameters"),
656 state
->Const
.MaxLights
),
659 const glsl_type
*const light_model_products_type
=
660 type("gl_LightModelProducts");
661 add_uniform(light_model_products_type
, "gl_FrontLightModelProduct");
662 add_uniform(light_model_products_type
, "gl_BackLightModelProduct");
664 const glsl_type
*const light_products_type
=
665 array(type("gl_LightProducts"), state
->Const
.MaxLights
);
666 add_uniform(light_products_type
, "gl_FrontLightProduct");
667 add_uniform(light_products_type
, "gl_BackLightProduct");
669 add_uniform(array(vec4_t
, state
->Const
.MaxTextureUnits
),
670 "gl_TextureEnvColor");
672 const glsl_type
*const texcoords_vec4
=
673 array(vec4_t
, state
->Const
.MaxTextureCoords
);
674 add_uniform(texcoords_vec4
, "gl_EyePlaneS");
675 add_uniform(texcoords_vec4
, "gl_EyePlaneT");
676 add_uniform(texcoords_vec4
, "gl_EyePlaneR");
677 add_uniform(texcoords_vec4
, "gl_EyePlaneQ");
678 add_uniform(texcoords_vec4
, "gl_ObjectPlaneS");
679 add_uniform(texcoords_vec4
, "gl_ObjectPlaneT");
680 add_uniform(texcoords_vec4
, "gl_ObjectPlaneR");
681 add_uniform(texcoords_vec4
, "gl_ObjectPlaneQ");
683 add_uniform(type("gl_FogParameters"), "gl_Fog");
689 * Generate variables which only exist in vertex shaders.
692 builtin_variable_generator::generate_vs_special_vars()
694 if (state
->is_version(130, 300))
695 add_system_value(SYSTEM_VALUE_VERTEX_ID
, int_t
, "gl_VertexID");
696 if (state
->ARB_draw_instanced_enable
)
697 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceIDARB");
698 if (state
->ARB_draw_instanced_enable
|| state
->is_version(140, 300))
699 add_system_value(SYSTEM_VALUE_INSTANCE_ID
, int_t
, "gl_InstanceID");
700 if (state
->AMD_vertex_shader_layer_enable
)
701 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
703 add_input(VERT_ATTRIB_POS
, vec4_t
, "gl_Vertex");
704 add_input(VERT_ATTRIB_NORMAL
, vec3_t
, "gl_Normal");
705 add_input(VERT_ATTRIB_COLOR0
, vec4_t
, "gl_Color");
706 add_input(VERT_ATTRIB_COLOR1
, vec4_t
, "gl_SecondaryColor");
707 add_input(VERT_ATTRIB_TEX0
, vec4_t
, "gl_MultiTexCoord0");
708 add_input(VERT_ATTRIB_TEX1
, vec4_t
, "gl_MultiTexCoord1");
709 add_input(VERT_ATTRIB_TEX2
, vec4_t
, "gl_MultiTexCoord2");
710 add_input(VERT_ATTRIB_TEX3
, vec4_t
, "gl_MultiTexCoord3");
711 add_input(VERT_ATTRIB_TEX4
, vec4_t
, "gl_MultiTexCoord4");
712 add_input(VERT_ATTRIB_TEX5
, vec4_t
, "gl_MultiTexCoord5");
713 add_input(VERT_ATTRIB_TEX6
, vec4_t
, "gl_MultiTexCoord6");
714 add_input(VERT_ATTRIB_TEX7
, vec4_t
, "gl_MultiTexCoord7");
715 add_input(VERT_ATTRIB_FOG
, float_t
, "gl_FogCoord");
721 * Generate variables which only exist in geometry shaders.
724 builtin_variable_generator::generate_gs_special_vars()
726 add_output(VARYING_SLOT_LAYER
, int_t
, "gl_Layer");
728 /* Although gl_PrimitiveID appears in tessellation control and tessellation
729 * evaluation shaders, it has a different function there than it has in
730 * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
731 * as special geometry shader variables.
733 * Note that although the general convention of suffixing geometry shader
734 * input varyings with "In" was not adopted into GLSL 1.50, it is used in
735 * the specific case of gl_PrimitiveIDIn. So we don't need to treat
736 * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
739 var
= add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveIDIn");
740 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
741 var
= add_output(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
742 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
747 * Generate variables which only exist in fragment shaders.
750 builtin_variable_generator::generate_fs_special_vars()
752 add_input(VARYING_SLOT_POS
, vec4_t
, "gl_FragCoord");
753 add_input(VARYING_SLOT_FACE
, bool_t
, "gl_FrontFacing");
754 if (state
->is_version(120, 100))
755 add_input(VARYING_SLOT_PNTC
, vec2_t
, "gl_PointCoord");
757 if (state
->is_version(150, 0)) {
759 add_input(VARYING_SLOT_PRIMITIVE_ID
, int_t
, "gl_PrimitiveID");
760 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
763 /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
764 * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
765 * They were removed from GLSL ES 3.00.
767 if (compatibility
|| !state
->is_version(420, 300)) {
768 add_output(FRAG_RESULT_COLOR
, vec4_t
, "gl_FragColor");
769 add_output(FRAG_RESULT_DATA0
,
770 array(vec4_t
, state
->Const
.MaxDrawBuffers
), "gl_FragData");
773 /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
776 if (state
->is_version(110, 300))
777 add_output(FRAG_RESULT_DEPTH
, float_t
, "gl_FragDepth");
779 if (state
->ARB_shader_stencil_export_enable
) {
780 ir_variable
*const var
=
781 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefARB");
782 if (state
->ARB_shader_stencil_export_warn
)
783 var
->warn_extension
= "GL_ARB_shader_stencil_export";
786 if (state
->AMD_shader_stencil_export_enable
) {
787 ir_variable
*const var
=
788 add_output(FRAG_RESULT_STENCIL
, int_t
, "gl_FragStencilRefAMD");
789 if (state
->AMD_shader_stencil_export_warn
)
790 var
->warn_extension
= "GL_AMD_shader_stencil_export";
796 * Add a single "varying" variable. The variable's type and direction (input
797 * or output) are adjusted as appropriate for the type of shader being
798 * compiled. For geometry shaders using {ARB,EXT}_geometry_shader4,
799 * name_as_gs_input is used for the input (to avoid ambiguity).
802 builtin_variable_generator::add_varying(int slot
, const glsl_type
*type
,
804 const char *name_as_gs_input
)
806 switch (state
->target
) {
807 case geometry_shader
:
808 this->per_vertex_in
.add_field(slot
, type
, name
);
811 this->per_vertex_out
.add_field(slot
, type
, name
);
813 case fragment_shader
:
814 add_input(slot
, type
, name
);
821 * Generate variables that are used to communicate data from one shader stage
822 * to the next ("varyings").
825 builtin_variable_generator::generate_varyings()
827 #define ADD_VARYING(loc, type, name) \
828 add_varying(loc, type, name, name "In")
830 /* gl_Position and gl_PointSize are not visible from fragment shaders. */
831 if (state
->target
!= fragment_shader
) {
832 ADD_VARYING(VARYING_SLOT_POS
, vec4_t
, "gl_Position");
833 ADD_VARYING(VARYING_SLOT_PSIZ
, float_t
, "gl_PointSize");
836 if (state
->is_version(130, 0)) {
837 ADD_VARYING(VARYING_SLOT_CLIP_DIST0
, array(float_t
, 0),
842 ADD_VARYING(VARYING_SLOT_TEX0
, array(vec4_t
, 0), "gl_TexCoord");
843 ADD_VARYING(VARYING_SLOT_FOGC
, float_t
, "gl_FogFragCoord");
844 if (state
->target
== fragment_shader
) {
845 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_Color");
846 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_SecondaryColor");
848 ADD_VARYING(VARYING_SLOT_CLIP_VERTEX
, vec4_t
, "gl_ClipVertex");
849 ADD_VARYING(VARYING_SLOT_COL0
, vec4_t
, "gl_FrontColor");
850 ADD_VARYING(VARYING_SLOT_BFC0
, vec4_t
, "gl_BackColor");
851 ADD_VARYING(VARYING_SLOT_COL1
, vec4_t
, "gl_FrontSecondaryColor");
852 ADD_VARYING(VARYING_SLOT_BFC1
, vec4_t
, "gl_BackSecondaryColor");
856 if (state
->target
== geometry_shader
) {
857 const glsl_type
*per_vertex_in_type
=
858 this->per_vertex_in
.construct_interface_instance();
859 ir_variable
*var
= add_variable("gl_in", array(per_vertex_in_type
, 0),
860 ir_var_shader_in
, -1);
861 var
->init_interface_type(per_vertex_in_type
);
863 if (state
->target
== vertex_shader
|| state
->target
== geometry_shader
) {
864 const glsl_type
*per_vertex_out_type
=
865 this->per_vertex_out
.construct_interface_instance();
866 const glsl_struct_field
*fields
= per_vertex_out_type
->fields
.structure
;
867 for (unsigned i
= 0; i
< per_vertex_out_type
->length
; i
++) {
869 add_variable(fields
[i
].name
, fields
[i
].type
, ir_var_shader_out
,
871 var
->init_interface_type(per_vertex_out_type
);
877 }; /* Anonymous namespace */
881 _mesa_glsl_initialize_variables(exec_list
*instructions
,
882 struct _mesa_glsl_parse_state
*state
)
884 builtin_variable_generator
gen(instructions
, state
);
886 gen
.generate_constants();
887 gen
.generate_uniforms();
889 gen
.generate_varyings();
891 switch (state
->target
) {
893 gen
.generate_vs_special_vars();
895 case geometry_shader
:
896 gen
.generate_gs_special_vars();
898 case fragment_shader
:
899 gen
.generate_fs_special_vars();