blob: 66b3abdd84ce3b10de28d710834f1604185cfc04 [file] [log] [blame]
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001/*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
Eric Anholtac95f2f2010-06-22 10:38:52 -070024#include "ir.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080025#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070026#include "glsl_symbol_table.h"
Eric Anholt44fc3c62011-11-08 14:46:25 -080027#include "main/core.h"
Ian Romanick92e412e2011-01-24 16:55:50 -080028#include "main/uniforms.h"
29#include "program/prog_parameter.h"
30#include "program/prog_statevars.h"
31#include "program/prog_instruction.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080032
Ian Romanick22971e92010-06-29 15:29:56 -070033static void generate_ARB_draw_buffers_variables(exec_list *,
34 struct _mesa_glsl_parse_state *,
35 bool, _mesa_glsl_parser_targets);
Ian Romanick9c4b1f22010-06-29 15:10:09 -070036
Brian Paul7ce18632010-12-08 18:25:38 -070037static void
38generate_ARB_draw_instanced_variables(exec_list *,
39 struct _mesa_glsl_parse_state *,
40 bool, _mesa_glsl_parser_targets);
41
Eric Anholt44fc3c62011-11-08 14:46:25 -080042struct builtin_variable {
43 enum ir_variable_mode mode;
44 int slot;
45 const char *type;
46 const char *name;
47};
48
49static 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" },
52};
53
54static 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" },
58};
59
60static const builtin_variable builtin_100ES_fs_variables[] = {
61 { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord" },
62};
63
64static const builtin_variable builtin_110_fs_variables[] = {
65 { ir_var_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth" },
66};
67
68static 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" },
72};
73
74static 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" },
94};
95
96static const builtin_variable builtin_120_fs_variables[] = {
97 { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord" },
98};
99
100static const builtin_variable builtin_130_vs_variables[] = {
Eric Anholt919c53e2011-11-08 14:49:07 -0800101 { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID" },
Eric Anholt44fc3c62011-11-08 14:46:25 -0800102};
103
104static 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"},
120
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"},
125};
126
Ian Romanick92e412e2011-01-24 16:55:50 -0800127static 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},
131};
132
133static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
134 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
135};
136
137static 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},
145};
146
147static 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},
153};
154
155static 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},
161};
162
163static 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,
171 SWIZZLE_Y,
172 SWIZZLE_Z,
173 SWIZZLE_Z)},
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},
180};
181
182static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
183 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
184};
185
186static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
187 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
188};
189
190static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
191 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
192};
193
194static 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},
198};
199
200static 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},
204};
205
206static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
207 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
208};
209
210static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
211 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
212};
213
214static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
215 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
216};
217
218static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
219 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
220};
221
222static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
223 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
224};
225
226static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
227 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
228};
229
230static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
231 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
232};
233
234static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
235 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
236};
237
238static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
239 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
240};
241
242static 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},
248};
249
250static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
251 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
252};
253
Eric Anholt4fc9a982011-10-21 10:45:48 -0700254static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800255 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
256};
257
Eric Anholt4fc9a982011-10-21 10:45:48 -0700258static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800259 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
260};
261
Eric Anholt4fc9a982011-10-21 10:45:48 -0700262static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800263 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
264};
265
Eric Anholtf868cb02011-07-16 17:41:26 -0700266static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
267 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
268};
269
270static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
271 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
272};
273
Ian Romanick92e412e2011-01-24 16:55:50 -0800274#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 }, \
280 }
281
282MATRIX(gl_ModelViewMatrix,
283 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
284MATRIX(gl_ModelViewMatrixInverse,
285 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
286MATRIX(gl_ModelViewMatrixTranspose,
287 STATE_MODELVIEW_MATRIX, 0);
288MATRIX(gl_ModelViewMatrixInverseTranspose,
289 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
290
291MATRIX(gl_ProjectionMatrix,
292 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
293MATRIX(gl_ProjectionMatrixInverse,
294 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
295MATRIX(gl_ProjectionMatrixTranspose,
296 STATE_PROJECTION_MATRIX, 0);
297MATRIX(gl_ProjectionMatrixInverseTranspose,
298 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
299
300MATRIX(gl_ModelViewProjectionMatrix,
301 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
302MATRIX(gl_ModelViewProjectionMatrixInverse,
303 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
304MATRIX(gl_ModelViewProjectionMatrixTranspose,
305 STATE_MVP_MATRIX, 0);
306MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
307 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
308
309MATRIX(gl_TextureMatrix,
310 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
311MATRIX(gl_TextureMatrixInverse,
312 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
313MATRIX(gl_TextureMatrixTranspose,
314 STATE_TEXTURE_MATRIX, 0);
315MATRIX(gl_TextureMatrixInverseTranspose,
316 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
317
318static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
319 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700320 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800321 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700322 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800323 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700324 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800325};
326
327#undef MATRIX
328
329#define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
330
331const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
332 STATEVAR(gl_DepthRange),
333 STATEVAR(gl_ClipPlane),
334 STATEVAR(gl_Point),
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),
352 STATEVAR(gl_Fog),
353
354 STATEVAR(gl_ModelViewMatrix),
355 STATEVAR(gl_ModelViewMatrixInverse),
356 STATEVAR(gl_ModelViewMatrixTranspose),
357 STATEVAR(gl_ModelViewMatrixInverseTranspose),
358
359 STATEVAR(gl_ProjectionMatrix),
360 STATEVAR(gl_ProjectionMatrixInverse),
361 STATEVAR(gl_ProjectionMatrixTranspose),
362 STATEVAR(gl_ProjectionMatrixInverseTranspose),
363
364 STATEVAR(gl_ModelViewProjectionMatrix),
365 STATEVAR(gl_ModelViewProjectionMatrixInverse),
366 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
367 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
368
369 STATEVAR(gl_TextureMatrix),
370 STATEVAR(gl_TextureMatrixInverse),
371 STATEVAR(gl_TextureMatrixTranspose),
372 STATEVAR(gl_TextureMatrixInverseTranspose),
373
374 STATEVAR(gl_NormalMatrix),
375 STATEVAR(gl_NormalScale),
376
Eric Anholt4fc9a982011-10-21 10:45:48 -0700377 STATEVAR(gl_BumpRotMatrix0MESA),
378 STATEVAR(gl_BumpRotMatrix1MESA),
379 STATEVAR(gl_FogParamsOptimizedMESA),
Eric Anholtf868cb02011-07-16 17:41:26 -0700380 STATEVAR(gl_CurrentAttribVertMESA),
381 STATEVAR(gl_CurrentAttribFragMESA),
Ian Romanick92e412e2011-01-24 16:55:50 -0800382
383 {NULL, NULL, 0}
384};
385
Ian Romanickc77b2572010-04-07 16:59:46 -0700386static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800387add_variable(exec_list *instructions, glsl_symbol_table *symtab,
388 const char *name, const glsl_type *type,
389 enum ir_variable_mode mode, int slot)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800390{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700391 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800392
Eric Anholt71df19f2010-04-19 11:10:37 -0700393 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -0700394 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -0700395 case ir_var_in:
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800396 case ir_var_const_in:
Eric Anholt046bef22010-08-04 20:33:57 -0700397 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -0700398 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800399 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -0700400 break;
401 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -0700402 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -0700403 break;
404 default:
405 assert(0);
406 break;
407 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800408
Ian Romanicked0626e2010-06-21 11:42:57 -0700409 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700410 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700411
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800412 /* Once the variable is created an initialized, add it to the symbol table
413 * and add the declaration to the IR stream.
414 */
415 instructions->push_tail(var);
416
Eric Anholt001eee52010-11-05 06:11:24 -0700417 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -0700418 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800419}
420
Eric Anholt85b5dba2010-07-28 12:23:51 -0700421static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800422add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholt85b5dba2010-07-28 12:23:51 -0700423 const char *name, const glsl_type *type)
424{
Ian Romanick89d81ab2011-01-25 10:41:20 -0800425 ir_variable *const uni =
426 add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
427
428 unsigned i;
429 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
430 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
431 break;
432 }
433 }
434
435 assert(_mesa_builtin_uniform_desc[i].name != NULL);
436 const struct gl_builtin_uniform_desc* const statevar =
437 &_mesa_builtin_uniform_desc[i];
438
439 const unsigned array_count = type->is_array() ? type->length : 1;
440 uni->num_state_slots = array_count * statevar->num_elements;
441
442 ir_state_slot *slots =
443 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
444
445 uni->state_slots = slots;
446
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];
450
451 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
452 if (type->is_array()) {
Eric Anholtf868cb02011-07-16 17:41:26 -0700453 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
454 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
455 slots->tokens[2] = a;
456 } else {
457 slots->tokens[1] = a;
458 }
Ian Romanick89d81ab2011-01-25 10:41:20 -0800459 }
460
461 slots->swizzle = element->swizzle;
462 slots++;
463 }
464 }
465
466 return uni;
Eric Anholt85b5dba2010-07-28 12:23:51 -0700467}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700468
469static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800470add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
471 const builtin_variable *proto)
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700472{
473 /* Create a new variable declaration from the description supplied by
474 * the caller.
475 */
476 const glsl_type *const type = symtab->get_type(proto->type);
477
478 assert(type != NULL);
479
Ian Romanick89d81ab2011-01-25 10:41:20 -0800480 if (proto->mode == ir_var_uniform) {
481 add_uniform(instructions, symtab, proto->name, type);
482 } else {
483 add_variable(instructions, symtab, proto->name, type, proto->mode,
484 proto->slot);
485 }
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700486}
487
Ian Romanickd3b39192011-10-31 14:43:27 -0700488static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800489add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholtf8946692010-07-20 14:03:35 -0700490 const char *name, int value)
491{
Ian Romanickdfdff942011-01-24 16:45:11 -0800492 ir_variable *const var = add_variable(instructions, symtab,
493 name, glsl_type::int_type,
494 ir_var_auto, -1);
Eric Anholtf8946692010-07-20 14:03:35 -0700495 var->constant_value = new(var) ir_constant(value);
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700496 var->constant_initializer = new(var) ir_constant(value);
497 var->has_initializer = true;
Ian Romanickd3b39192011-10-31 14:43:27 -0700498 return var;
Eric Anholtf8946692010-07-20 14:03:35 -0700499}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700500
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700501/* Several constants in GLSL ES have different names than normal desktop GLSL.
502 * Therefore, this function should only be called on the ES path.
503 */
504static void
505generate_100ES_uniforms(exec_list *instructions,
506 struct _mesa_glsl_parse_state *state)
507{
Ian Romanickdfdff942011-01-24 16:45:11 -0800508 glsl_symbol_table *const symtab = state->symbols;
509
510 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700511 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800512 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700513 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800514 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700515 state->Const.MaxVaryingFloats / 4);
Ian Romanickdfdff942011-01-24 16:45:11 -0800516 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700517 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800518 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700519 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800520 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700521 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800522 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700523 state->Const.MaxFragmentUniformComponents);
524
Ian Romanickdfdff942011-01-24 16:45:11 -0800525 add_uniform(instructions, symtab, "gl_DepthRange",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700526 state->symbols->get_type("gl_DepthRangeParameters"));
527}
528
Eric Anholt78fe3c92010-03-28 01:46:48 -0700529static void
530generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700531 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700532{
Ian Romanickdfdff942011-01-24 16:45:11 -0800533 glsl_symbol_table *const symtab = state->symbols;
534
Eric Anholt78fe3c92010-03-28 01:46:48 -0700535 for (unsigned i = 0
536 ; i < Elements(builtin_110_deprecated_uniforms)
537 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800538 add_builtin_variable(instructions, symtab,
539 & builtin_110_deprecated_uniforms[i]);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700540 }
541
Ian Romanickdfdff942011-01-24 16:45:11 -0800542 add_builtin_constant(instructions, symtab, "gl_MaxLights",
Eric Anholtf8946692010-07-20 14:03:35 -0700543 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800544 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
Eric Anholtf8946692010-07-20 14:03:35 -0700545 state->Const.MaxClipPlanes);
Ian Romanickdfdff942011-01-24 16:45:11 -0800546 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700547 state->Const.MaxTextureUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800548 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
Eric Anholtf8946692010-07-20 14:03:35 -0700549 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800550 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Eric Anholtf8946692010-07-20 14:03:35 -0700551 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800552 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700553 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800554 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
Eric Anholtf8946692010-07-20 14:03:35 -0700555 state->Const.MaxVaryingFloats);
Ian Romanickdfdff942011-01-24 16:45:11 -0800556 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700557 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800558 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700559 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800560 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700561 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800562 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700563 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700564
Ian Romanick3eba5932010-04-26 14:59:32 -0700565 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700566 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700567 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700568
Ian Romanickdfdff942011-01-24 16:45:11 -0800569 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
570 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
571 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
572 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700573
Ian Romanickdfdff942011-01-24 16:45:11 -0800574 add_uniform(instructions, symtab, "gl_DepthRange",
575 symtab->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700576
Ian Romanickdfdff942011-01-24 16:45:11 -0800577 add_uniform(instructions, symtab, "gl_ClipPlane",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700578 glsl_type::get_array_instance(glsl_type::vec4_type,
579 state->Const.MaxClipPlanes));
Ian Romanickdfdff942011-01-24 16:45:11 -0800580 add_uniform(instructions, symtab, "gl_Point",
581 symtab->get_type("gl_PointParameters"));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700582
583 const glsl_type *const material_parameters_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800584 symtab->get_type("gl_MaterialParameters");
585 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
586 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700587
Eric Anholtaa579432010-05-19 14:09:04 -0700588 const glsl_type *const light_source_array_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800589 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700590
Ian Romanickdfdff942011-01-24 16:45:11 -0800591 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700592
Eric Anholt85b5dba2010-07-28 12:23:51 -0700593 const glsl_type *const light_model_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800594 symtab->get_type("gl_LightModelProducts");
595 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700596 light_model_products_type);
Ian Romanickdfdff942011-01-24 16:45:11 -0800597 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700598 light_model_products_type);
599
600 const glsl_type *const light_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800601 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
Eric Anholt85b5dba2010-07-28 12:23:51 -0700602 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800603 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
604 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700605
Ian Romanickdfdff942011-01-24 16:45:11 -0800606 add_uniform(instructions, symtab, "gl_TextureEnvColor",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700607 glsl_type::get_array_instance(glsl_type::vec4_type,
608 state->Const.MaxTextureUnits));
609
610 const glsl_type *const texcoords_vec4 =
611 glsl_type::get_array_instance(glsl_type::vec4_type,
612 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800613 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
614 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
615 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
616 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
617 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
618 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
619 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
620 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700621
Ian Romanickdfdff942011-01-24 16:45:11 -0800622 add_uniform(instructions, symtab, "gl_Fog",
623 symtab->get_type("gl_FogParameters"));
Eric Anholtf868cb02011-07-16 17:41:26 -0700624
625 /* Mesa-internal current attrib state */
626 const glsl_type *const vert_attribs =
627 glsl_type::get_array_instance(glsl_type::vec4_type, VERT_ATTRIB_MAX);
628 add_uniform(instructions, symtab, "gl_CurrentAttribVertMESA", vert_attribs);
629 const glsl_type *const frag_attribs =
630 glsl_type::get_array_instance(glsl_type::vec4_type, FRAG_ATTRIB_MAX);
631 add_uniform(instructions, symtab, "gl_CurrentAttribFragMESA", frag_attribs);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700632}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800633
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700634/* This function should only be called for ES, not desktop GL. */
635static void
636generate_100ES_vs_variables(exec_list *instructions,
637 struct _mesa_glsl_parse_state *state)
638{
639 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800640 add_builtin_variable(instructions, state->symbols,
641 & builtin_core_vs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700642 }
643
644 generate_100ES_uniforms(instructions, state);
645
646 generate_ARB_draw_buffers_variables(instructions, state, false,
647 vertex_shader);
648}
649
650
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800651static void
652generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700653 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800654{
655 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800656 add_builtin_variable(instructions, state->symbols,
657 & builtin_core_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800658 }
659
660 for (unsigned i = 0
661 ; i < Elements(builtin_110_deprecated_vs_variables)
662 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800663 add_builtin_variable(instructions, state->symbols,
664 & builtin_110_deprecated_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800665 }
Ian Romanick127308b2010-07-01 13:30:50 -0700666 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800667
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700668 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
669 *
670 * "As with all arrays, indices used to subscript gl_TexCoord must
671 * either be an integral constant expressions, or this array must be
672 * re-declared by the shader with a size. The size can be at most
673 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
674 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800675 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700676 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700677 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700678
Ian Romanickdfdff942011-01-24 16:45:11 -0800679 add_variable(instructions, state->symbols,
680 "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
Ian Romanick22971e92010-06-29 15:29:56 -0700681
682 generate_ARB_draw_buffers_variables(instructions, state, false,
683 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800684}
685
686
687static void
688generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700689 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800690{
691 /* GLSL version 1.20 did not add any built-in variables in the vertex
692 * shader.
693 */
Ian Romanick22971e92010-06-29 15:29:56 -0700694 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800695}
696
697
698static void
Paul Berry36c4b1a2011-08-11 17:33:06 -0700699generate_130_uniforms(exec_list *instructions,
700 struct _mesa_glsl_parse_state *state)
701{
702 glsl_symbol_table *const symtab = state->symbols;
703
704 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
705 state->Const.MaxClipPlanes);
Eric Anholt2ecfa882011-10-21 15:17:16 -0700706 add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
707 state->Const.MaxVaryingFloats);
Paul Berry36c4b1a2011-08-11 17:33:06 -0700708}
709
710
711static void
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800712generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700713 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800714{
Ian Romanick22971e92010-06-29 15:29:56 -0700715 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800716
717 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800718 add_builtin_variable(instructions, state->symbols,
719 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800720 }
721
Paul Berry36c4b1a2011-08-11 17:33:06 -0700722 generate_130_uniforms(instructions, state);
723
Paul Berryaf243b52011-08-11 15:03:19 -0700724 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
725 * Variables):
726 *
727 * The gl_ClipDistance array is predeclared as unsized and must
728 * be sized by the shader either redeclaring it with a size or
729 * indexing it only with integral constant expressions.
730 *
731 * We represent this in Mesa by initially declaring the array as
732 * size 0.
733 */
Eric Anholt271e1992010-04-02 23:47:06 -0700734 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700735 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700736
Ian Romanickdfdff942011-01-24 16:45:11 -0800737 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700738 "gl_ClipDistance", clip_distance_array_type, ir_var_out,
739 VERT_RESULT_CLIP_DIST0);
Eric Anholt271e1992010-04-02 23:47:06 -0700740
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800741}
742
743
744static void
745initialize_vs_variables(exec_list *instructions,
746 struct _mesa_glsl_parse_state *state)
747{
748
749 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700750 case 100:
751 generate_100ES_vs_variables(instructions, state);
752 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800753 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700754 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800755 break;
756 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700757 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800758 break;
759 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700760 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800761 break;
762 }
Brian Paul7ce18632010-12-08 18:25:38 -0700763
764 if (state->ARB_draw_instanced_enable)
765 generate_ARB_draw_instanced_variables(instructions, state, false,
766 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800767}
768
Brian Paul7ce18632010-12-08 18:25:38 -0700769
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700770/* This function should only be called for ES, not desktop GL. */
771static void
772generate_100ES_fs_variables(exec_list *instructions,
773 struct _mesa_glsl_parse_state *state)
774{
775 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800776 add_builtin_variable(instructions, state->symbols,
777 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700778 }
779
780 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800781 add_builtin_variable(instructions, state->symbols,
782 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700783 }
784
785 generate_100ES_uniforms(instructions, state);
786
787 generate_ARB_draw_buffers_variables(instructions, state, false,
788 fragment_shader);
789}
790
Eric Anholtb3f743a2010-03-25 14:48:25 -0700791static void
792generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700793 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700794{
795 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800796 add_builtin_variable(instructions, state->symbols,
797 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700798 }
799
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700800 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800801 add_builtin_variable(instructions, state->symbols,
802 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700803 }
804
Eric Anholt0f09aea2010-03-27 12:48:57 -0700805 for (unsigned i = 0
806 ; i < Elements(builtin_110_deprecated_fs_variables)
807 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800808 add_builtin_variable(instructions, state->symbols,
809 & builtin_110_deprecated_fs_variables[i]);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700810 }
Ian Romanick127308b2010-07-01 13:30:50 -0700811 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700812
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700813 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
814 *
815 * "As with all arrays, indices used to subscript gl_TexCoord must
816 * either be an integral constant expressions, or this array must be
817 * re-declared by the shader with a size. The size can be at most
818 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
819 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700820 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700821 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700822 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700823
Ian Romanickdfdff942011-01-24 16:45:11 -0800824 add_variable(instructions, state->symbols,
825 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700826
Ian Romanick22971e92010-06-29 15:29:56 -0700827 generate_ARB_draw_buffers_variables(instructions, state, false,
828 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700829}
830
Ian Romanickc77b2572010-04-07 16:59:46 -0700831
832static void
Ian Romanick22971e92010-06-29 15:29:56 -0700833generate_ARB_draw_buffers_variables(exec_list *instructions,
834 struct _mesa_glsl_parse_state *state,
835 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700836{
Ian Romanick22971e92010-06-29 15:29:56 -0700837 /* gl_MaxDrawBuffers is available in all shader stages.
838 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700839 ir_variable *const mdb =
Ian Romanickd3b39192011-10-31 14:43:27 -0700840 add_builtin_constant(instructions, state->symbols, "gl_MaxDrawBuffers",
841 state->Const.MaxDrawBuffers);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700842
843 if (warn)
844 mdb->warn_extension = "GL_ARB_draw_buffers";
845
Ian Romanick22971e92010-06-29 15:29:56 -0700846 /* gl_FragData is only available in the fragment shader.
847 */
848 if (target == fragment_shader) {
849 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700850 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700851 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700852
Ian Romanick22971e92010-06-29 15:29:56 -0700853 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800854 add_variable(instructions, state->symbols,
855 "gl_FragData", vec4_array_type,
856 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700857
858 if (warn)
859 fd->warn_extension = "GL_ARB_draw_buffers";
860 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700861}
862
Brian Paul7ce18632010-12-08 18:25:38 -0700863
864static void
865generate_ARB_draw_instanced_variables(exec_list *instructions,
866 struct _mesa_glsl_parse_state *state,
867 bool warn,
868 _mesa_glsl_parser_targets target)
869{
870 /* gl_InstanceIDARB is only available in the vertex shader.
871 */
872 if (target == vertex_shader) {
Ian Romanick1e6a2c12012-02-29 08:29:39 -0800873 ir_variable *inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800874 add_variable(instructions, state->symbols,
875 "gl_InstanceIDARB", glsl_type::int_type,
876 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700877
878 if (warn)
879 inst->warn_extension = "GL_ARB_draw_instanced";
Ian Romanick1e6a2c12012-02-29 08:29:39 -0800880
881 /* Originally ARB_draw_instanced only specified that ARB decorated name.
882 * Since no vendor actually implemented that behavior and some apps use
883 * the undecorated name, the extension now specifies that both names are
884 * available.
885 */
886 inst = add_variable(instructions, state->symbols,
887 "gl_InstanceID", glsl_type::int_type,
888 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
889
890 if (warn)
891 inst->warn_extension = "GL_ARB_draw_instanced";
Brian Paul7ce18632010-12-08 18:25:38 -0700892 }
893}
894
895
Dave Airlied9671862010-10-06 09:36:02 +1000896static void
897generate_ARB_shader_stencil_export_variables(exec_list *instructions,
898 struct _mesa_glsl_parse_state *state,
899 bool warn)
900{
901 /* gl_FragStencilRefARB is only available in the fragment shader.
902 */
903 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800904 add_variable(instructions, state->symbols,
905 "gl_FragStencilRefARB", glsl_type::int_type,
906 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +1000907
908 if (warn)
909 fd->warn_extension = "GL_ARB_shader_stencil_export";
910}
Ian Romanickc77b2572010-04-07 16:59:46 -0700911
Eric Anholtb3f743a2010-03-25 14:48:25 -0700912static void
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200913generate_AMD_shader_stencil_export_variables(exec_list *instructions,
914 struct _mesa_glsl_parse_state *state,
915 bool warn)
916{
917 /* gl_FragStencilRefAMD is only available in the fragment shader.
918 */
919 ir_variable *const fd =
920 add_variable(instructions, state->symbols,
921 "gl_FragStencilRefAMD", glsl_type::int_type,
922 ir_var_out, FRAG_RESULT_STENCIL);
923
924 if (warn)
925 fd->warn_extension = "GL_AMD_shader_stencil_export";
926}
927
928static void
Eric Anholtb3f743a2010-03-25 14:48:25 -0700929generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700930 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700931{
Ian Romanick5e18b052010-06-29 14:21:05 -0700932 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700933
934 for (unsigned i = 0
935 ; i < Elements(builtin_120_fs_variables)
936 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800937 add_builtin_variable(instructions, state->symbols,
938 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -0700939 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700940}
941
942static void
943generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700944 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700945{
Ian Romanick5e18b052010-06-29 14:21:05 -0700946 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700947
Paul Berry36c4b1a2011-08-11 17:33:06 -0700948 generate_130_uniforms(instructions, state);
949
Paul Berryaf243b52011-08-11 15:03:19 -0700950 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
951 * Variables):
952 *
953 * The built-in input variable gl_ClipDistance array contains linearly
954 * interpolated values for the vertex values written by the vertex shader
955 * to the gl_ClipDistance vertex output variable. This array must be
956 * sized in the fragment shader either implicitly or explicitly to be the
957 * same size as it was sized in the vertex shader.
958 *
959 * In other words, the array must be pre-declared as implicitly sized. We
960 * represent this in Mesa by initially declaring the array as size 0.
961 */
Ian Romanick8645a952010-04-07 16:47:44 -0700962 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700963 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700964
Ian Romanickdfdff942011-01-24 16:45:11 -0800965 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700966 "gl_ClipDistance", clip_distance_array_type, ir_var_in,
967 FRAG_ATTRIB_CLIP_DIST0);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700968}
969
970static void
971initialize_fs_variables(exec_list *instructions,
972 struct _mesa_glsl_parse_state *state)
973{
974
975 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700976 case 100:
977 generate_100ES_fs_variables(instructions, state);
978 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700979 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700980 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700981 break;
982 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700983 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700984 break;
985 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700986 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700987 break;
988 }
Dave Airlied9671862010-10-06 09:36:02 +1000989
990 if (state->ARB_shader_stencil_export_enable)
991 generate_ARB_shader_stencil_export_variables(instructions, state,
992 state->ARB_shader_stencil_export_warn);
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200993
994 if (state->AMD_shader_stencil_export_enable)
995 generate_AMD_shader_stencil_export_variables(instructions, state,
996 state->AMD_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700997}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800998
999void
1000_mesa_glsl_initialize_variables(exec_list *instructions,
1001 struct _mesa_glsl_parse_state *state)
1002{
1003 switch (state->target) {
1004 case vertex_shader:
1005 initialize_vs_variables(instructions, state);
1006 break;
1007 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -07001008 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001009 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -07001010 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001011 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001012 }
1013}