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 void generate_ARB_draw_buffers_variables(exec_list
*,
34 struct _mesa_glsl_parse_state
*,
35 bool, _mesa_glsl_parser_targets
);
38 generate_ARB_draw_instanced_variables(exec_list
*,
39 struct _mesa_glsl_parse_state
*,
40 bool, _mesa_glsl_parser_targets
);
42 struct builtin_variable
{
43 enum ir_variable_mode mode
;
49 static const builtin_variable builtin_core_vs_variables
[] = {
50 { ir_var_out
, VERT_RESULT_HPOS
, "vec4", "gl_Position" },
51 { ir_var_out
, VERT_RESULT_PSIZ
, "float", "gl_PointSize" },
54 static const builtin_variable builtin_core_fs_variables
[] = {
55 { ir_var_in
, FRAG_ATTRIB_WPOS
, "vec4", "gl_FragCoord" },
56 { ir_var_in
, FRAG_ATTRIB_FACE
, "bool", "gl_FrontFacing" },
57 { ir_var_out
, FRAG_RESULT_COLOR
, "vec4", "gl_FragColor" },
60 static const builtin_variable builtin_100ES_fs_variables
[] = {
61 { ir_var_in
, FRAG_ATTRIB_PNTC
, "vec2", "gl_PointCoord" },
64 static const builtin_variable builtin_110_fs_variables
[] = {
65 { ir_var_out
, FRAG_RESULT_DEPTH
, "float", "gl_FragDepth" },
68 static const builtin_variable builtin_110_deprecated_fs_variables
[] = {
69 { ir_var_in
, FRAG_ATTRIB_COL0
, "vec4", "gl_Color" },
70 { ir_var_in
, FRAG_ATTRIB_COL1
, "vec4", "gl_SecondaryColor" },
71 { ir_var_in
, FRAG_ATTRIB_FOGC
, "float", "gl_FogFragCoord" },
74 static const builtin_variable builtin_110_deprecated_vs_variables
[] = {
75 { ir_var_in
, VERT_ATTRIB_POS
, "vec4", "gl_Vertex" },
76 { ir_var_in
, VERT_ATTRIB_NORMAL
, "vec3", "gl_Normal" },
77 { ir_var_in
, VERT_ATTRIB_COLOR0
, "vec4", "gl_Color" },
78 { ir_var_in
, VERT_ATTRIB_COLOR1
, "vec4", "gl_SecondaryColor" },
79 { ir_var_in
, VERT_ATTRIB_TEX0
, "vec4", "gl_MultiTexCoord0" },
80 { ir_var_in
, VERT_ATTRIB_TEX1
, "vec4", "gl_MultiTexCoord1" },
81 { ir_var_in
, VERT_ATTRIB_TEX2
, "vec4", "gl_MultiTexCoord2" },
82 { ir_var_in
, VERT_ATTRIB_TEX3
, "vec4", "gl_MultiTexCoord3" },
83 { ir_var_in
, VERT_ATTRIB_TEX4
, "vec4", "gl_MultiTexCoord4" },
84 { ir_var_in
, VERT_ATTRIB_TEX5
, "vec4", "gl_MultiTexCoord5" },
85 { ir_var_in
, VERT_ATTRIB_TEX6
, "vec4", "gl_MultiTexCoord6" },
86 { ir_var_in
, VERT_ATTRIB_TEX7
, "vec4", "gl_MultiTexCoord7" },
87 { ir_var_in
, VERT_ATTRIB_FOG
, "float", "gl_FogCoord" },
88 { ir_var_out
, VERT_RESULT_CLIP_VERTEX
, "vec4", "gl_ClipVertex" },
89 { ir_var_out
, VERT_RESULT_COL0
, "vec4", "gl_FrontColor" },
90 { ir_var_out
, VERT_RESULT_BFC0
, "vec4", "gl_BackColor" },
91 { ir_var_out
, VERT_RESULT_COL1
, "vec4", "gl_FrontSecondaryColor" },
92 { ir_var_out
, VERT_RESULT_BFC1
, "vec4", "gl_BackSecondaryColor" },
93 { ir_var_out
, VERT_RESULT_FOGC
, "float", "gl_FogFragCoord" },
96 static const builtin_variable builtin_120_fs_variables
[] = {
97 { ir_var_in
, FRAG_ATTRIB_PNTC
, "vec2", "gl_PointCoord" },
100 static const builtin_variable builtin_130_vs_variables
[] = {
101 { ir_var_system_value
, SYSTEM_VALUE_VERTEX_ID
, "int", "gl_VertexID" },
104 static const builtin_variable builtin_110_deprecated_uniforms
[] = {
105 { ir_var_uniform
, -1, "mat4", "gl_ModelViewMatrix" },
106 { ir_var_uniform
, -1, "mat4", "gl_ProjectionMatrix" },
107 { ir_var_uniform
, -1, "mat4", "gl_ModelViewProjectionMatrix" },
108 { ir_var_uniform
, -1, "mat3", "gl_NormalMatrix" },
109 { ir_var_uniform
, -1, "mat4", "gl_ModelViewMatrixInverse" },
110 { ir_var_uniform
, -1, "mat4", "gl_ProjectionMatrixInverse" },
111 { ir_var_uniform
, -1, "mat4", "gl_ModelViewProjectionMatrixInverse" },
112 { ir_var_uniform
, -1, "mat4", "gl_ModelViewMatrixTranspose" },
113 { ir_var_uniform
, -1, "mat4", "gl_ProjectionMatrixTranspose" },
114 { ir_var_uniform
, -1, "mat4", "gl_ModelViewProjectionMatrixTranspose" },
115 { ir_var_uniform
, -1, "mat4", "gl_ModelViewMatrixInverseTranspose" },
116 { ir_var_uniform
, -1, "mat4", "gl_ProjectionMatrixInverseTranspose" },
117 { ir_var_uniform
, -1, "mat4", "gl_ModelViewProjectionMatrixInverseTranspose" },
118 { ir_var_uniform
, -1, "float", "gl_NormalScale" },
119 { ir_var_uniform
, -1, "gl_LightModelParameters", "gl_LightModel"},
121 /* Mesa-internal ATI_envmap_bumpmap state. */
122 { ir_var_uniform
, -1, "vec2", "gl_BumpRotMatrix0MESA"},
123 { ir_var_uniform
, -1, "vec2", "gl_BumpRotMatrix1MESA"},
124 { ir_var_uniform
, -1, "vec4", "gl_FogParamsOptimizedMESA"},
127 static struct gl_builtin_uniform_element gl_DepthRange_elements
[] = {
128 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
129 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
130 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
133 static struct gl_builtin_uniform_element gl_ClipPlane_elements
[] = {
134 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
137 static struct gl_builtin_uniform_element gl_Point_elements
[] = {
138 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
139 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
140 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
141 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
142 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
143 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
144 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
147 static struct gl_builtin_uniform_element gl_FrontMaterial_elements
[] = {
148 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
149 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
150 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
151 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
152 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
155 static struct gl_builtin_uniform_element gl_BackMaterial_elements
[] = {
156 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
157 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
158 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
159 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
160 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
163 static struct gl_builtin_uniform_element gl_LightSource_elements
[] = {
164 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
165 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
166 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
167 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
168 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
169 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
},
170 MAKE_SWIZZLE4(SWIZZLE_X
,
174 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
175 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
176 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
177 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
178 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
179 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
182 static struct gl_builtin_uniform_element gl_LightModel_elements
[] = {
183 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
186 static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements
[] = {
187 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
190 static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements
[] = {
191 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
194 static struct gl_builtin_uniform_element gl_FrontLightProduct_elements
[] = {
195 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
196 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
197 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
200 static struct gl_builtin_uniform_element gl_BackLightProduct_elements
[] = {
201 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
202 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
203 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
206 static struct gl_builtin_uniform_element gl_TextureEnvColor_elements
[] = {
207 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
210 static struct gl_builtin_uniform_element gl_EyePlaneS_elements
[] = {
211 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
214 static struct gl_builtin_uniform_element gl_EyePlaneT_elements
[] = {
215 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
218 static struct gl_builtin_uniform_element gl_EyePlaneR_elements
[] = {
219 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
222 static struct gl_builtin_uniform_element gl_EyePlaneQ_elements
[] = {
223 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
226 static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements
[] = {
227 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
230 static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements
[] = {
231 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
234 static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements
[] = {
235 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
238 static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements
[] = {
239 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
242 static struct gl_builtin_uniform_element gl_Fog_elements
[] = {
243 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
244 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
245 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
246 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
247 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
250 static struct gl_builtin_uniform_element gl_NormalScale_elements
[] = {
251 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
254 static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements
[] = {
255 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_0
}, SWIZZLE_XYZW
},
258 static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements
[] = {
259 {NULL
, {STATE_INTERNAL
, STATE_ROT_MATRIX_1
}, SWIZZLE_XYZW
},
262 static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements
[] = {
263 {NULL
, {STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
}, SWIZZLE_XYZW
},
266 static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements
[] = {
267 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, 0}, SWIZZLE_XYZW
},
270 static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements
[] = {
271 {NULL
, {STATE_INTERNAL
, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
, 0}, SWIZZLE_XYZW
},
274 #define MATRIX(name, statevar, modifier) \
275 static struct gl_builtin_uniform_element name ## _elements[] = { \
276 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
277 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
278 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
279 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
282 MATRIX(gl_ModelViewMatrix
,
283 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
284 MATRIX(gl_ModelViewMatrixInverse
,
285 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
286 MATRIX(gl_ModelViewMatrixTranspose
,
287 STATE_MODELVIEW_MATRIX
, 0);
288 MATRIX(gl_ModelViewMatrixInverseTranspose
,
289 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
291 MATRIX(gl_ProjectionMatrix
,
292 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
293 MATRIX(gl_ProjectionMatrixInverse
,
294 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
295 MATRIX(gl_ProjectionMatrixTranspose
,
296 STATE_PROJECTION_MATRIX
, 0);
297 MATRIX(gl_ProjectionMatrixInverseTranspose
,
298 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
300 MATRIX(gl_ModelViewProjectionMatrix
,
301 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
302 MATRIX(gl_ModelViewProjectionMatrixInverse
,
303 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
304 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
305 STATE_MVP_MATRIX
, 0);
306 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
307 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
309 MATRIX(gl_TextureMatrix
,
310 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
311 MATRIX(gl_TextureMatrixInverse
,
312 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
313 MATRIX(gl_TextureMatrixTranspose
,
314 STATE_TEXTURE_MATRIX
, 0);
315 MATRIX(gl_TextureMatrixInverseTranspose
,
316 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
318 static struct gl_builtin_uniform_element gl_NormalMatrix_elements
[] = {
319 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
320 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
321 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
322 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
323 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
324 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
) },
329 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
331 const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc
[] = {
332 STATEVAR(gl_DepthRange
),
333 STATEVAR(gl_ClipPlane
),
335 STATEVAR(gl_FrontMaterial
),
336 STATEVAR(gl_BackMaterial
),
337 STATEVAR(gl_LightSource
),
338 STATEVAR(gl_LightModel
),
339 STATEVAR(gl_FrontLightModelProduct
),
340 STATEVAR(gl_BackLightModelProduct
),
341 STATEVAR(gl_FrontLightProduct
),
342 STATEVAR(gl_BackLightProduct
),
343 STATEVAR(gl_TextureEnvColor
),
344 STATEVAR(gl_EyePlaneS
),
345 STATEVAR(gl_EyePlaneT
),
346 STATEVAR(gl_EyePlaneR
),
347 STATEVAR(gl_EyePlaneQ
),
348 STATEVAR(gl_ObjectPlaneS
),
349 STATEVAR(gl_ObjectPlaneT
),
350 STATEVAR(gl_ObjectPlaneR
),
351 STATEVAR(gl_ObjectPlaneQ
),
354 STATEVAR(gl_ModelViewMatrix
),
355 STATEVAR(gl_ModelViewMatrixInverse
),
356 STATEVAR(gl_ModelViewMatrixTranspose
),
357 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
359 STATEVAR(gl_ProjectionMatrix
),
360 STATEVAR(gl_ProjectionMatrixInverse
),
361 STATEVAR(gl_ProjectionMatrixTranspose
),
362 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
364 STATEVAR(gl_ModelViewProjectionMatrix
),
365 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
366 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
367 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
369 STATEVAR(gl_TextureMatrix
),
370 STATEVAR(gl_TextureMatrixInverse
),
371 STATEVAR(gl_TextureMatrixTranspose
),
372 STATEVAR(gl_TextureMatrixInverseTranspose
),
374 STATEVAR(gl_NormalMatrix
),
375 STATEVAR(gl_NormalScale
),
377 STATEVAR(gl_BumpRotMatrix0MESA
),
378 STATEVAR(gl_BumpRotMatrix1MESA
),
379 STATEVAR(gl_FogParamsOptimizedMESA
),
380 STATEVAR(gl_CurrentAttribVertMESA
),
381 STATEVAR(gl_CurrentAttribFragMESA
),
387 add_variable(exec_list
*instructions
, glsl_symbol_table
*symtab
,
388 const char *name
, const glsl_type
*type
,
389 enum ir_variable_mode mode
, int slot
)
391 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
396 case ir_var_const_in
:
398 case ir_var_system_value
:
399 var
->read_only
= true;
409 var
->location
= slot
;
410 var
->explicit_location
= (slot
>= 0);
412 /* Once the variable is created an initialized, add it to the symbol table
413 * and add the declaration to the IR stream.
415 instructions
->push_tail(var
);
417 symtab
->add_variable(var
);
422 add_uniform(exec_list
*instructions
, glsl_symbol_table
*symtab
,
423 const char *name
, const glsl_type
*type
)
425 ir_variable
*const uni
=
426 add_variable(instructions
, symtab
, name
, type
, ir_var_uniform
, -1);
429 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
!= NULL
; i
++) {
430 if (strcmp(_mesa_builtin_uniform_desc
[i
].name
, name
) == 0) {
435 assert(_mesa_builtin_uniform_desc
[i
].name
!= NULL
);
436 const struct gl_builtin_uniform_desc
* const statevar
=
437 &_mesa_builtin_uniform_desc
[i
];
439 const unsigned array_count
= type
->is_array() ? type
->length
: 1;
440 uni
->num_state_slots
= array_count
* statevar
->num_elements
;
442 ir_state_slot
*slots
=
443 ralloc_array(uni
, ir_state_slot
, uni
->num_state_slots
);
445 uni
->state_slots
= slots
;
447 for (unsigned a
= 0; a
< array_count
; a
++) {
448 for (unsigned j
= 0; j
< statevar
->num_elements
; j
++) {
449 struct gl_builtin_uniform_element
*element
= &statevar
->elements
[j
];
451 memcpy(slots
->tokens
, element
->tokens
, sizeof(element
->tokens
));
452 if (type
->is_array()) {
453 if (strcmp(name
, "gl_CurrentAttribVertMESA") == 0 ||
454 strcmp(name
, "gl_CurrentAttribFragMESA") == 0) {
455 slots
->tokens
[2] = a
;
457 slots
->tokens
[1] = a
;
461 slots
->swizzle
= element
->swizzle
;
470 add_builtin_variable(exec_list
*instructions
, glsl_symbol_table
*symtab
,
471 const builtin_variable
*proto
)
473 /* Create a new variable declaration from the description supplied by
476 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
478 assert(type
!= NULL
);
480 if (proto
->mode
== ir_var_uniform
) {
481 add_uniform(instructions
, symtab
, proto
->name
, type
);
483 add_variable(instructions
, symtab
, proto
->name
, type
, proto
->mode
,
489 add_builtin_constant(exec_list
*instructions
, glsl_symbol_table
*symtab
,
490 const char *name
, int value
)
492 ir_variable
*const var
= add_variable(instructions
, symtab
,
493 name
, glsl_type::int_type
,
495 var
->constant_value
= new(var
) ir_constant(value
);
496 var
->constant_initializer
= new(var
) ir_constant(value
);
497 var
->has_initializer
= true;
501 /* Several constants in GLSL ES have different names than normal desktop GLSL.
502 * Therefore, this function should only be called on the ES path.
505 generate_100ES_uniforms(exec_list
*instructions
,
506 struct _mesa_glsl_parse_state
*state
)
508 glsl_symbol_table
*const symtab
= state
->symbols
;
510 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexAttribs",
511 state
->Const
.MaxVertexAttribs
);
512 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexUniformVectors",
513 state
->Const
.MaxVertexUniformComponents
);
514 add_builtin_constant(instructions
, symtab
, "gl_MaxVaryingVectors",
515 state
->Const
.MaxVaryingFloats
/ 4);
516 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexTextureImageUnits",
517 state
->Const
.MaxVertexTextureImageUnits
);
518 add_builtin_constant(instructions
, symtab
, "gl_MaxCombinedTextureImageUnits",
519 state
->Const
.MaxCombinedTextureImageUnits
);
520 add_builtin_constant(instructions
, symtab
, "gl_MaxTextureImageUnits",
521 state
->Const
.MaxTextureImageUnits
);
522 add_builtin_constant(instructions
, symtab
, "gl_MaxFragmentUniformVectors",
523 state
->Const
.MaxFragmentUniformComponents
);
525 add_uniform(instructions
, symtab
, "gl_DepthRange",
526 state
->symbols
->get_type("gl_DepthRangeParameters"));
530 generate_110_uniforms(exec_list
*instructions
,
531 struct _mesa_glsl_parse_state
*state
,
534 glsl_symbol_table
*const symtab
= state
->symbols
;
536 if (add_deprecated
) {
538 ; i
< Elements(builtin_110_deprecated_uniforms
)
540 add_builtin_variable(instructions
, symtab
,
541 & builtin_110_deprecated_uniforms
[i
]);
545 if (add_deprecated
) {
546 add_builtin_constant(instructions
, symtab
, "gl_MaxLights",
547 state
->Const
.MaxLights
);
548 add_builtin_constant(instructions
, symtab
, "gl_MaxClipPlanes",
549 state
->Const
.MaxClipPlanes
);
550 add_builtin_constant(instructions
, symtab
, "gl_MaxTextureUnits",
551 state
->Const
.MaxTextureUnits
);
552 add_builtin_constant(instructions
, symtab
, "gl_MaxTextureCoords",
553 state
->Const
.MaxTextureCoords
);
555 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexAttribs",
556 state
->Const
.MaxVertexAttribs
);
557 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexUniformComponents",
558 state
->Const
.MaxVertexUniformComponents
);
559 add_builtin_constant(instructions
, symtab
, "gl_MaxVaryingFloats",
560 state
->Const
.MaxVaryingFloats
);
561 add_builtin_constant(instructions
, symtab
, "gl_MaxVertexTextureImageUnits",
562 state
->Const
.MaxVertexTextureImageUnits
);
563 add_builtin_constant(instructions
, symtab
, "gl_MaxCombinedTextureImageUnits",
564 state
->Const
.MaxCombinedTextureImageUnits
);
565 add_builtin_constant(instructions
, symtab
, "gl_MaxTextureImageUnits",
566 state
->Const
.MaxTextureImageUnits
);
567 add_builtin_constant(instructions
, symtab
, "gl_MaxFragmentUniformComponents",
568 state
->Const
.MaxFragmentUniformComponents
);
570 if (add_deprecated
) {
571 const glsl_type
*const mat4_array_type
=
572 glsl_type::get_array_instance(glsl_type::mat4_type
,
573 state
->Const
.MaxTextureCoords
);
575 add_uniform(instructions
, symtab
, "gl_TextureMatrix", mat4_array_type
);
576 add_uniform(instructions
, symtab
, "gl_TextureMatrixInverse", mat4_array_type
);
577 add_uniform(instructions
, symtab
, "gl_TextureMatrixTranspose", mat4_array_type
);
578 add_uniform(instructions
, symtab
, "gl_TextureMatrixInverseTranspose", mat4_array_type
);
581 add_uniform(instructions
, symtab
, "gl_DepthRange",
582 symtab
->get_type("gl_DepthRangeParameters"));
584 if (add_deprecated
) {
585 add_uniform(instructions
, symtab
, "gl_ClipPlane",
586 glsl_type::get_array_instance(glsl_type::vec4_type
,
587 state
->Const
.MaxClipPlanes
));
588 add_uniform(instructions
, symtab
, "gl_Point",
589 symtab
->get_type("gl_PointParameters"));
591 const glsl_type
*const material_parameters_type
=
592 symtab
->get_type("gl_MaterialParameters");
593 add_uniform(instructions
, symtab
, "gl_FrontMaterial", material_parameters_type
);
594 add_uniform(instructions
, symtab
, "gl_BackMaterial", material_parameters_type
);
596 const glsl_type
*const light_source_array_type
=
597 glsl_type::get_array_instance(symtab
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
599 add_uniform(instructions
, symtab
, "gl_LightSource", light_source_array_type
);
601 const glsl_type
*const light_model_products_type
=
602 symtab
->get_type("gl_LightModelProducts");
603 add_uniform(instructions
, symtab
, "gl_FrontLightModelProduct",
604 light_model_products_type
);
605 add_uniform(instructions
, symtab
, "gl_BackLightModelProduct",
606 light_model_products_type
);
608 const glsl_type
*const light_products_type
=
609 glsl_type::get_array_instance(symtab
->get_type("gl_LightProducts"),
610 state
->Const
.MaxLights
);
611 add_uniform(instructions
, symtab
, "gl_FrontLightProduct", light_products_type
);
612 add_uniform(instructions
, symtab
, "gl_BackLightProduct", light_products_type
);
614 add_uniform(instructions
, symtab
, "gl_TextureEnvColor",
615 glsl_type::get_array_instance(glsl_type::vec4_type
,
616 state
->Const
.MaxTextureUnits
));
618 const glsl_type
*const texcoords_vec4
=
619 glsl_type::get_array_instance(glsl_type::vec4_type
,
620 state
->Const
.MaxTextureCoords
);
621 add_uniform(instructions
, symtab
, "gl_EyePlaneS", texcoords_vec4
);
622 add_uniform(instructions
, symtab
, "gl_EyePlaneT", texcoords_vec4
);
623 add_uniform(instructions
, symtab
, "gl_EyePlaneR", texcoords_vec4
);
624 add_uniform(instructions
, symtab
, "gl_EyePlaneQ", texcoords_vec4
);
625 add_uniform(instructions
, symtab
, "gl_ObjectPlaneS", texcoords_vec4
);
626 add_uniform(instructions
, symtab
, "gl_ObjectPlaneT", texcoords_vec4
);
627 add_uniform(instructions
, symtab
, "gl_ObjectPlaneR", texcoords_vec4
);
628 add_uniform(instructions
, symtab
, "gl_ObjectPlaneQ", texcoords_vec4
);
630 add_uniform(instructions
, symtab
, "gl_Fog",
631 symtab
->get_type("gl_FogParameters"));
634 /* Mesa-internal current attrib state */
635 const glsl_type
*const vert_attribs
=
636 glsl_type::get_array_instance(glsl_type::vec4_type
, VERT_ATTRIB_MAX
);
637 add_uniform(instructions
, symtab
, "gl_CurrentAttribVertMESA", vert_attribs
);
638 const glsl_type
*const frag_attribs
=
639 glsl_type::get_array_instance(glsl_type::vec4_type
, FRAG_ATTRIB_MAX
);
640 add_uniform(instructions
, symtab
, "gl_CurrentAttribFragMESA", frag_attribs
);
643 /* This function should only be called for ES, not desktop GL. */
645 generate_100ES_vs_variables(exec_list
*instructions
,
646 struct _mesa_glsl_parse_state
*state
)
648 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
649 add_builtin_variable(instructions
, state
->symbols
,
650 & builtin_core_vs_variables
[i
]);
653 generate_100ES_uniforms(instructions
, state
);
655 generate_ARB_draw_buffers_variables(instructions
, state
, false,
661 generate_110_vs_variables(exec_list
*instructions
,
662 struct _mesa_glsl_parse_state
*state
,
665 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
666 add_builtin_variable(instructions
, state
->symbols
,
667 & builtin_core_vs_variables
[i
]);
670 if (add_deprecated
) {
672 ; i
< Elements(builtin_110_deprecated_vs_variables
)
674 add_builtin_variable(instructions
, state
->symbols
,
675 & builtin_110_deprecated_vs_variables
[i
]);
678 generate_110_uniforms(instructions
, state
, add_deprecated
);
680 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
682 * "As with all arrays, indices used to subscript gl_TexCoord must
683 * either be an integral constant expressions, or this array must be
684 * re-declared by the shader with a size. The size can be at most
685 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
686 * implementation in preserving varying resources."
688 const glsl_type
*const vec4_array_type
=
689 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
691 add_variable(instructions
, state
->symbols
,
692 "gl_TexCoord", vec4_array_type
, ir_var_out
, VERT_RESULT_TEX0
);
694 generate_ARB_draw_buffers_variables(instructions
, state
, false,
700 generate_120_vs_variables(exec_list
*instructions
,
701 struct _mesa_glsl_parse_state
*state
,
704 /* GLSL version 1.20 did not add any built-in variables in the vertex
707 generate_110_vs_variables(instructions
, state
, add_deprecated
);
712 generate_130_uniforms(exec_list
*instructions
,
713 struct _mesa_glsl_parse_state
*state
)
715 glsl_symbol_table
*const symtab
= state
->symbols
;
717 add_builtin_constant(instructions
, symtab
, "gl_MaxClipDistances",
718 state
->Const
.MaxClipPlanes
);
719 add_builtin_constant(instructions
, symtab
, "gl_MaxVaryingComponents",
720 state
->Const
.MaxVaryingFloats
);
725 generate_130_vs_variables(exec_list
*instructions
,
726 struct _mesa_glsl_parse_state
*state
,
729 generate_120_vs_variables(instructions
, state
, add_deprecated
);
731 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
732 add_builtin_variable(instructions
, state
->symbols
,
733 & builtin_130_vs_variables
[i
]);
736 generate_130_uniforms(instructions
, state
);
738 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
741 * The gl_ClipDistance array is predeclared as unsized and must
742 * be sized by the shader either redeclaring it with a size or
743 * indexing it only with integral constant expressions.
745 * We represent this in Mesa by initially declaring the array as
748 const glsl_type
*const clip_distance_array_type
=
749 glsl_type::get_array_instance(glsl_type::float_type
, 0);
751 add_variable(instructions
, state
->symbols
,
752 "gl_ClipDistance", clip_distance_array_type
, ir_var_out
,
753 VERT_RESULT_CLIP_DIST0
);
759 initialize_vs_variables(exec_list
*instructions
,
760 struct _mesa_glsl_parse_state
*state
)
763 switch (state
->language_version
) {
765 generate_100ES_vs_variables(instructions
, state
);
768 generate_110_vs_variables(instructions
, state
, true);
771 generate_120_vs_variables(instructions
, state
, true);
774 generate_130_vs_variables(instructions
, state
, true);
777 generate_130_vs_variables(instructions
, state
, false);
781 generate_ARB_draw_instanced_variables(instructions
, state
, false,
786 /* This function should only be called for ES, not desktop GL. */
788 generate_100ES_fs_variables(exec_list
*instructions
,
789 struct _mesa_glsl_parse_state
*state
)
791 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
792 add_builtin_variable(instructions
, state
->symbols
,
793 & builtin_core_fs_variables
[i
]);
796 for (unsigned i
= 0; i
< Elements(builtin_100ES_fs_variables
); i
++) {
797 add_builtin_variable(instructions
, state
->symbols
,
798 & builtin_100ES_fs_variables
[i
]);
801 generate_100ES_uniforms(instructions
, state
);
803 generate_ARB_draw_buffers_variables(instructions
, state
, false,
808 generate_110_fs_variables(exec_list
*instructions
,
809 struct _mesa_glsl_parse_state
*state
,
812 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
813 add_builtin_variable(instructions
, state
->symbols
,
814 & builtin_core_fs_variables
[i
]);
817 for (unsigned i
= 0; i
< Elements(builtin_110_fs_variables
); i
++) {
818 add_builtin_variable(instructions
, state
->symbols
,
819 & builtin_110_fs_variables
[i
]);
822 if (add_deprecated
) {
824 ; i
< Elements(builtin_110_deprecated_fs_variables
)
826 add_builtin_variable(instructions
, state
->symbols
,
827 & builtin_110_deprecated_fs_variables
[i
]);
831 generate_110_uniforms(instructions
, state
, add_deprecated
);
833 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
835 * "As with all arrays, indices used to subscript gl_TexCoord must
836 * either be an integral constant expressions, or this array must be
837 * re-declared by the shader with a size. The size can be at most
838 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
839 * implementation in preserving varying resources."
841 const glsl_type
*const vec4_array_type
=
842 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
844 add_variable(instructions
, state
->symbols
,
845 "gl_TexCoord", vec4_array_type
, ir_var_in
, FRAG_ATTRIB_TEX0
);
847 generate_ARB_draw_buffers_variables(instructions
, state
, false,
853 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
854 struct _mesa_glsl_parse_state
*state
,
855 bool warn
, _mesa_glsl_parser_targets target
)
857 /* gl_MaxDrawBuffers is available in all shader stages.
859 ir_variable
*const mdb
=
860 add_builtin_constant(instructions
, state
->symbols
, "gl_MaxDrawBuffers",
861 state
->Const
.MaxDrawBuffers
);
864 mdb
->warn_extension
= "GL_ARB_draw_buffers";
866 /* gl_FragData is only available in the fragment shader.
868 if (target
== fragment_shader
) {
869 const glsl_type
*const vec4_array_type
=
870 glsl_type::get_array_instance(glsl_type::vec4_type
,
871 state
->Const
.MaxDrawBuffers
);
873 ir_variable
*const fd
=
874 add_variable(instructions
, state
->symbols
,
875 "gl_FragData", vec4_array_type
,
876 ir_var_out
, FRAG_RESULT_DATA0
);
879 fd
->warn_extension
= "GL_ARB_draw_buffers";
885 generate_ARB_draw_instanced_variables(exec_list
*instructions
,
886 struct _mesa_glsl_parse_state
*state
,
888 _mesa_glsl_parser_targets target
)
890 /* gl_InstanceIDARB is only available in the vertex shader.
892 if (target
!= vertex_shader
)
895 if (state
->ARB_draw_instanced_enable
) {
897 add_variable(instructions
, state
->symbols
,
898 "gl_InstanceIDARB", glsl_type::int_type
,
899 ir_var_system_value
, SYSTEM_VALUE_INSTANCE_ID
);
902 inst
->warn_extension
= "GL_ARB_draw_instanced";
905 if (state
->ARB_draw_instanced_enable
|| state
->language_version
>= 140) {
906 /* Originally ARB_draw_instanced only specified that ARB decorated name.
907 * Since no vendor actually implemented that behavior and some apps use
908 * the undecorated name, the extension now specifies that both names are
912 add_variable(instructions
, state
->symbols
,
913 "gl_InstanceID", glsl_type::int_type
,
914 ir_var_system_value
, SYSTEM_VALUE_INSTANCE_ID
);
916 if (state
->language_version
< 140 && warn
)
917 inst
->warn_extension
= "GL_ARB_draw_instanced";
923 generate_ARB_shader_stencil_export_variables(exec_list
*instructions
,
924 struct _mesa_glsl_parse_state
*state
,
927 /* gl_FragStencilRefARB is only available in the fragment shader.
929 ir_variable
*const fd
=
930 add_variable(instructions
, state
->symbols
,
931 "gl_FragStencilRefARB", glsl_type::int_type
,
932 ir_var_out
, FRAG_RESULT_STENCIL
);
935 fd
->warn_extension
= "GL_ARB_shader_stencil_export";
939 generate_AMD_shader_stencil_export_variables(exec_list
*instructions
,
940 struct _mesa_glsl_parse_state
*state
,
943 /* gl_FragStencilRefAMD is only available in the fragment shader.
945 ir_variable
*const fd
=
946 add_variable(instructions
, state
->symbols
,
947 "gl_FragStencilRefAMD", glsl_type::int_type
,
948 ir_var_out
, FRAG_RESULT_STENCIL
);
951 fd
->warn_extension
= "GL_AMD_shader_stencil_export";
955 generate_120_fs_variables(exec_list
*instructions
,
956 struct _mesa_glsl_parse_state
*state
,
959 generate_110_fs_variables(instructions
, state
, add_deprecated
);
962 ; i
< Elements(builtin_120_fs_variables
)
964 add_builtin_variable(instructions
, state
->symbols
,
965 & builtin_120_fs_variables
[i
]);
970 generate_fs_clipdistance(exec_list
*instructions
,
971 struct _mesa_glsl_parse_state
*state
)
973 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
976 * The built-in input variable gl_ClipDistance array contains linearly
977 * interpolated values for the vertex values written by the vertex shader
978 * to the gl_ClipDistance vertex output variable. This array must be
979 * sized in the fragment shader either implicitly or explicitly to be the
980 * same size as it was sized in the vertex shader.
982 * In other words, the array must be pre-declared as implicitly sized. We
983 * represent this in Mesa by initially declaring the array as size 0.
985 const glsl_type
*const clip_distance_array_type
=
986 glsl_type::get_array_instance(glsl_type::float_type
, 0);
988 add_variable(instructions
, state
->symbols
,
989 "gl_ClipDistance", clip_distance_array_type
, ir_var_in
,
990 FRAG_ATTRIB_CLIP_DIST0
);
994 generate_130_fs_variables(exec_list
*instructions
,
995 struct _mesa_glsl_parse_state
*state
)
997 generate_120_fs_variables(instructions
, state
, true);
999 generate_130_uniforms(instructions
, state
);
1000 generate_fs_clipdistance(instructions
, state
);
1005 generate_140_fs_variables(exec_list
*instructions
,
1006 struct _mesa_glsl_parse_state
*state
)
1008 generate_120_fs_variables(instructions
, state
, false);
1010 generate_130_uniforms(instructions
, state
);
1011 generate_fs_clipdistance(instructions
, state
);
1015 initialize_fs_variables(exec_list
*instructions
,
1016 struct _mesa_glsl_parse_state
*state
)
1019 switch (state
->language_version
) {
1021 generate_100ES_fs_variables(instructions
, state
);
1024 generate_110_fs_variables(instructions
, state
, true);
1027 generate_120_fs_variables(instructions
, state
, true);
1030 generate_130_fs_variables(instructions
, state
);
1033 generate_140_fs_variables(instructions
, state
);
1037 if (state
->ARB_shader_stencil_export_enable
)
1038 generate_ARB_shader_stencil_export_variables(instructions
, state
,
1039 state
->ARB_shader_stencil_export_warn
);
1041 if (state
->AMD_shader_stencil_export_enable
)
1042 generate_AMD_shader_stencil_export_variables(instructions
, state
,
1043 state
->AMD_shader_stencil_export_warn
);
1047 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1048 struct _mesa_glsl_parse_state
*state
)
1050 switch (state
->target
) {
1052 initialize_vs_variables(instructions
, state
);
1054 case geometry_shader
:
1056 case fragment_shader
:
1057 initialize_fs_variables(instructions
, state
);