blob: f2a804986cd5ebb39785f22785109f3cfd3b0ab0 [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
Paul Berry8dec1bf2012-08-04 10:29:49 -070064static const builtin_variable builtin_300ES_vs_variables[] = {
65 { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID" },
66};
67
68static const builtin_variable builtin_300ES_fs_variables[] = {
69 { ir_var_in, FRAG_ATTRIB_WPOS, "vec4", "gl_FragCoord" },
70 { ir_var_in, FRAG_ATTRIB_FACE, "bool", "gl_FrontFacing" },
71 { ir_var_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth" },
72 { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord" },
73};
74
Eric Anholt44fc3c62011-11-08 14:46:25 -080075static const builtin_variable builtin_110_fs_variables[] = {
76 { ir_var_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth" },
77};
78
79static const builtin_variable builtin_110_deprecated_fs_variables[] = {
80 { ir_var_in, FRAG_ATTRIB_COL0, "vec4", "gl_Color" },
81 { ir_var_in, FRAG_ATTRIB_COL1, "vec4", "gl_SecondaryColor" },
82 { ir_var_in, FRAG_ATTRIB_FOGC, "float", "gl_FogFragCoord" },
83};
84
85static const builtin_variable builtin_110_deprecated_vs_variables[] = {
86 { ir_var_in, VERT_ATTRIB_POS, "vec4", "gl_Vertex" },
87 { ir_var_in, VERT_ATTRIB_NORMAL, "vec3", "gl_Normal" },
88 { ir_var_in, VERT_ATTRIB_COLOR0, "vec4", "gl_Color" },
89 { ir_var_in, VERT_ATTRIB_COLOR1, "vec4", "gl_SecondaryColor" },
90 { ir_var_in, VERT_ATTRIB_TEX0, "vec4", "gl_MultiTexCoord0" },
91 { ir_var_in, VERT_ATTRIB_TEX1, "vec4", "gl_MultiTexCoord1" },
92 { ir_var_in, VERT_ATTRIB_TEX2, "vec4", "gl_MultiTexCoord2" },
93 { ir_var_in, VERT_ATTRIB_TEX3, "vec4", "gl_MultiTexCoord3" },
94 { ir_var_in, VERT_ATTRIB_TEX4, "vec4", "gl_MultiTexCoord4" },
95 { ir_var_in, VERT_ATTRIB_TEX5, "vec4", "gl_MultiTexCoord5" },
96 { ir_var_in, VERT_ATTRIB_TEX6, "vec4", "gl_MultiTexCoord6" },
97 { ir_var_in, VERT_ATTRIB_TEX7, "vec4", "gl_MultiTexCoord7" },
98 { ir_var_in, VERT_ATTRIB_FOG, "float", "gl_FogCoord" },
99 { ir_var_out, VERT_RESULT_CLIP_VERTEX, "vec4", "gl_ClipVertex" },
100 { ir_var_out, VERT_RESULT_COL0, "vec4", "gl_FrontColor" },
101 { ir_var_out, VERT_RESULT_BFC0, "vec4", "gl_BackColor" },
102 { ir_var_out, VERT_RESULT_COL1, "vec4", "gl_FrontSecondaryColor" },
103 { ir_var_out, VERT_RESULT_BFC1, "vec4", "gl_BackSecondaryColor" },
104 { ir_var_out, VERT_RESULT_FOGC, "float", "gl_FogFragCoord" },
105};
106
107static const builtin_variable builtin_120_fs_variables[] = {
108 { ir_var_in, FRAG_ATTRIB_PNTC, "vec2", "gl_PointCoord" },
109};
110
111static const builtin_variable builtin_130_vs_variables[] = {
Eric Anholt919c53e2011-11-08 14:49:07 -0800112 { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID" },
Eric Anholt44fc3c62011-11-08 14:46:25 -0800113};
114
115static const builtin_variable builtin_110_deprecated_uniforms[] = {
116 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrix" },
117 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrix" },
118 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrix" },
119 { ir_var_uniform, -1, "mat3", "gl_NormalMatrix" },
120 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverse" },
121 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverse" },
122 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverse" },
123 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixTranspose" },
124 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixTranspose" },
125 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixTranspose" },
126 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverseTranspose" },
127 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverseTranspose" },
128 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverseTranspose" },
129 { ir_var_uniform, -1, "float", "gl_NormalScale" },
130 { ir_var_uniform, -1, "gl_LightModelParameters", "gl_LightModel"},
131
132 /* Mesa-internal ATI_envmap_bumpmap state. */
133 { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix0MESA"},
134 { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix1MESA"},
135 { ir_var_uniform, -1, "vec4", "gl_FogParamsOptimizedMESA"},
136};
137
Ian Romanick92e412e2011-01-24 16:55:50 -0800138static struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
139 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
140 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
141 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
142};
143
144static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
145 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
146};
147
148static struct gl_builtin_uniform_element gl_Point_elements[] = {
149 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
150 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
151 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
152 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
153 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
154 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
155 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
156};
157
158static struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
159 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
160 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
161 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
162 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
163 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
164};
165
166static struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
167 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
168 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
169 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
170 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
171 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
172};
173
174static struct gl_builtin_uniform_element gl_LightSource_elements[] = {
175 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
176 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
177 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
178 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
179 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
180 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
181 MAKE_SWIZZLE4(SWIZZLE_X,
182 SWIZZLE_Y,
183 SWIZZLE_Z,
184 SWIZZLE_Z)},
185 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
186 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
187 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
188 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
189 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
190 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
191};
192
193static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
194 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
195};
196
197static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
198 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
199};
200
201static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
202 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
203};
204
205static struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
206 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
207 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
208 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
209};
210
211static struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
212 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
213 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
214 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
215};
216
217static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
218 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
219};
220
221static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
222 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
223};
224
225static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
226 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
227};
228
229static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
230 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
231};
232
233static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
234 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
235};
236
237static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
238 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
239};
240
241static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
242 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
243};
244
245static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
246 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
247};
248
249static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
250 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
251};
252
253static struct gl_builtin_uniform_element gl_Fog_elements[] = {
254 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
255 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
256 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
257 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
258 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
259};
260
261static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
262 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
263};
264
Eric Anholt4fc9a982011-10-21 10:45:48 -0700265static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800266 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
267};
268
Eric Anholt4fc9a982011-10-21 10:45:48 -0700269static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800270 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
271};
272
Eric Anholt4fc9a982011-10-21 10:45:48 -0700273static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800274 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
275};
276
Eric Anholtf868cb02011-07-16 17:41:26 -0700277static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
278 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
279};
280
281static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
282 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
283};
284
Ian Romanick92e412e2011-01-24 16:55:50 -0800285#define MATRIX(name, statevar, modifier) \
286 static struct gl_builtin_uniform_element name ## _elements[] = { \
287 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
288 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
289 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
290 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
291 }
292
293MATRIX(gl_ModelViewMatrix,
294 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
295MATRIX(gl_ModelViewMatrixInverse,
296 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
297MATRIX(gl_ModelViewMatrixTranspose,
298 STATE_MODELVIEW_MATRIX, 0);
299MATRIX(gl_ModelViewMatrixInverseTranspose,
300 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
301
302MATRIX(gl_ProjectionMatrix,
303 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
304MATRIX(gl_ProjectionMatrixInverse,
305 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
306MATRIX(gl_ProjectionMatrixTranspose,
307 STATE_PROJECTION_MATRIX, 0);
308MATRIX(gl_ProjectionMatrixInverseTranspose,
309 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
310
311MATRIX(gl_ModelViewProjectionMatrix,
312 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
313MATRIX(gl_ModelViewProjectionMatrixInverse,
314 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
315MATRIX(gl_ModelViewProjectionMatrixTranspose,
316 STATE_MVP_MATRIX, 0);
317MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
318 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
319
320MATRIX(gl_TextureMatrix,
321 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
322MATRIX(gl_TextureMatrixInverse,
323 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
324MATRIX(gl_TextureMatrixTranspose,
325 STATE_TEXTURE_MATRIX, 0);
326MATRIX(gl_TextureMatrixInverseTranspose,
327 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
328
329static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
330 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700331 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800332 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700333 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800334 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700335 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800336};
337
338#undef MATRIX
339
340#define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
341
Dave Airlie1ce9f252012-09-15 13:26:39 +1000342static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
Ian Romanick92e412e2011-01-24 16:55:50 -0800343 STATEVAR(gl_DepthRange),
344 STATEVAR(gl_ClipPlane),
345 STATEVAR(gl_Point),
346 STATEVAR(gl_FrontMaterial),
347 STATEVAR(gl_BackMaterial),
348 STATEVAR(gl_LightSource),
349 STATEVAR(gl_LightModel),
350 STATEVAR(gl_FrontLightModelProduct),
351 STATEVAR(gl_BackLightModelProduct),
352 STATEVAR(gl_FrontLightProduct),
353 STATEVAR(gl_BackLightProduct),
354 STATEVAR(gl_TextureEnvColor),
355 STATEVAR(gl_EyePlaneS),
356 STATEVAR(gl_EyePlaneT),
357 STATEVAR(gl_EyePlaneR),
358 STATEVAR(gl_EyePlaneQ),
359 STATEVAR(gl_ObjectPlaneS),
360 STATEVAR(gl_ObjectPlaneT),
361 STATEVAR(gl_ObjectPlaneR),
362 STATEVAR(gl_ObjectPlaneQ),
363 STATEVAR(gl_Fog),
364
365 STATEVAR(gl_ModelViewMatrix),
366 STATEVAR(gl_ModelViewMatrixInverse),
367 STATEVAR(gl_ModelViewMatrixTranspose),
368 STATEVAR(gl_ModelViewMatrixInverseTranspose),
369
370 STATEVAR(gl_ProjectionMatrix),
371 STATEVAR(gl_ProjectionMatrixInverse),
372 STATEVAR(gl_ProjectionMatrixTranspose),
373 STATEVAR(gl_ProjectionMatrixInverseTranspose),
374
375 STATEVAR(gl_ModelViewProjectionMatrix),
376 STATEVAR(gl_ModelViewProjectionMatrixInverse),
377 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
378 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
379
380 STATEVAR(gl_TextureMatrix),
381 STATEVAR(gl_TextureMatrixInverse),
382 STATEVAR(gl_TextureMatrixTranspose),
383 STATEVAR(gl_TextureMatrixInverseTranspose),
384
385 STATEVAR(gl_NormalMatrix),
386 STATEVAR(gl_NormalScale),
387
Eric Anholt4fc9a982011-10-21 10:45:48 -0700388 STATEVAR(gl_BumpRotMatrix0MESA),
389 STATEVAR(gl_BumpRotMatrix1MESA),
390 STATEVAR(gl_FogParamsOptimizedMESA),
Eric Anholtf868cb02011-07-16 17:41:26 -0700391 STATEVAR(gl_CurrentAttribVertMESA),
392 STATEVAR(gl_CurrentAttribFragMESA),
Ian Romanick92e412e2011-01-24 16:55:50 -0800393
394 {NULL, NULL, 0}
395};
396
Ian Romanickc77b2572010-04-07 16:59:46 -0700397static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800398add_variable(exec_list *instructions, glsl_symbol_table *symtab,
399 const char *name, const glsl_type *type,
400 enum ir_variable_mode mode, int slot)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800401{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700402 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800403
Eric Anholt71df19f2010-04-19 11:10:37 -0700404 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -0700405 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -0700406 case ir_var_in:
Eric Anholt046bef22010-08-04 20:33:57 -0700407 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -0700408 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800409 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -0700410 break;
Eric Anholt71df19f2010-04-19 11:10:37 -0700411 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -0700412 break;
413 default:
Paul Berry7d51ead2013-01-24 16:11:08 -0800414 /* The only variables that are added using this function should be
415 * uniforms, shader inputs, and shader outputs, constants (which use
416 * ir_var_auto), and system values.
417 */
Eric Anholt71df19f2010-04-19 11:10:37 -0700418 assert(0);
419 break;
420 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800421
Ian Romanicked0626e2010-06-21 11:42:57 -0700422 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700423 var->explicit_location = (slot >= 0);
Dave Airlie1256a5d2012-03-24 13:33:41 +0000424 var->explicit_index = 0;
Ian Romanicked0626e2010-06-21 11:42:57 -0700425
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800426 /* Once the variable is created an initialized, add it to the symbol table
427 * and add the declaration to the IR stream.
428 */
429 instructions->push_tail(var);
430
Eric Anholt001eee52010-11-05 06:11:24 -0700431 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -0700432 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800433}
434
Eric Anholt85b5dba2010-07-28 12:23:51 -0700435static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800436add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholt85b5dba2010-07-28 12:23:51 -0700437 const char *name, const glsl_type *type)
438{
Ian Romanick89d81ab2011-01-25 10:41:20 -0800439 ir_variable *const uni =
440 add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
441
442 unsigned i;
443 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
444 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
445 break;
446 }
447 }
448
449 assert(_mesa_builtin_uniform_desc[i].name != NULL);
450 const struct gl_builtin_uniform_desc* const statevar =
451 &_mesa_builtin_uniform_desc[i];
452
453 const unsigned array_count = type->is_array() ? type->length : 1;
454 uni->num_state_slots = array_count * statevar->num_elements;
455
456 ir_state_slot *slots =
457 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
458
459 uni->state_slots = slots;
460
461 for (unsigned a = 0; a < array_count; a++) {
462 for (unsigned j = 0; j < statevar->num_elements; j++) {
463 struct gl_builtin_uniform_element *element = &statevar->elements[j];
464
465 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
466 if (type->is_array()) {
Eric Anholtf868cb02011-07-16 17:41:26 -0700467 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
468 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
469 slots->tokens[2] = a;
470 } else {
471 slots->tokens[1] = a;
472 }
Ian Romanick89d81ab2011-01-25 10:41:20 -0800473 }
474
475 slots->swizzle = element->swizzle;
476 slots++;
477 }
478 }
479
480 return uni;
Eric Anholt85b5dba2010-07-28 12:23:51 -0700481}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700482
483static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800484add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
485 const builtin_variable *proto)
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700486{
487 /* Create a new variable declaration from the description supplied by
488 * the caller.
489 */
490 const glsl_type *const type = symtab->get_type(proto->type);
491
492 assert(type != NULL);
493
Ian Romanick89d81ab2011-01-25 10:41:20 -0800494 if (proto->mode == ir_var_uniform) {
495 add_uniform(instructions, symtab, proto->name, type);
496 } else {
497 add_variable(instructions, symtab, proto->name, type, proto->mode,
498 proto->slot);
499 }
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700500}
501
Ian Romanickd3b39192011-10-31 14:43:27 -0700502static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800503add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholtf8946692010-07-20 14:03:35 -0700504 const char *name, int value)
505{
Ian Romanickdfdff942011-01-24 16:45:11 -0800506 ir_variable *const var = add_variable(instructions, symtab,
507 name, glsl_type::int_type,
508 ir_var_auto, -1);
Eric Anholtf8946692010-07-20 14:03:35 -0700509 var->constant_value = new(var) ir_constant(value);
Ian Romanickf37b1ad2011-10-31 14:31:07 -0700510 var->constant_initializer = new(var) ir_constant(value);
511 var->has_initializer = true;
Ian Romanickd3b39192011-10-31 14:43:27 -0700512 return var;
Eric Anholtf8946692010-07-20 14:03:35 -0700513}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700514
Paul Berry8dec1bf2012-08-04 10:29:49 -0700515/**
516 * Uniforms that are common to all GLSL ES implementations.
517 *
518 * Several constants in GLSL ES have different names than normal desktop GLSL.
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700519 * Therefore, this function should only be called on the ES path.
520 */
521static void
Paul Berry8dec1bf2012-08-04 10:29:49 -0700522generate_common_ES_uniforms(exec_list *instructions,
523 struct _mesa_glsl_parse_state *state)
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700524{
Ian Romanickdfdff942011-01-24 16:45:11 -0800525 glsl_symbol_table *const symtab = state->symbols;
526
527 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700528 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800529 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700530 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800531 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700532 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800533 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700534 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800535 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700536 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800537 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700538 state->Const.MaxFragmentUniformComponents);
539
Ian Romanickdfdff942011-01-24 16:45:11 -0800540 add_uniform(instructions, symtab, "gl_DepthRange",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700541 state->symbols->get_type("gl_DepthRangeParameters"));
542}
543
Eric Anholt78fe3c92010-03-28 01:46:48 -0700544static void
Paul Berry8dec1bf2012-08-04 10:29:49 -0700545generate_100ES_uniforms(exec_list *instructions,
546 struct _mesa_glsl_parse_state *state)
547{
548 generate_common_ES_uniforms(instructions, state);
549
550 glsl_symbol_table *const symtab = state->symbols;
551
552 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
553 state->Const.MaxVaryingFloats / 4);
554}
555
556static void
557generate_300ES_uniforms(exec_list *instructions,
558 struct _mesa_glsl_parse_state *state)
559{
560 generate_common_ES_uniforms(instructions, state);
561
562 glsl_symbol_table *const symtab = state->symbols;
563
564 add_builtin_constant(instructions, symtab, "gl_MaxVertexOutputVectors",
565 state->Const.MaxVaryingFloats / 4);
566 add_builtin_constant(instructions, symtab, "gl_MaxFragmentInputVectors",
567 state->Const.MaxVaryingFloats / 4);
568 add_builtin_constant(instructions, symtab, "gl_MinProgramTexelOffset",
569 state->Const.MinProgramTexelOffset);
570 add_builtin_constant(instructions, symtab, "gl_MaxProgramTexelOffset",
571 state->Const.MaxProgramTexelOffset);
572}
573
574static void
Eric Anholt78fe3c92010-03-28 01:46:48 -0700575generate_110_uniforms(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -0800576 struct _mesa_glsl_parse_state *state,
577 bool add_deprecated)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700578{
Ian Romanickdfdff942011-01-24 16:45:11 -0800579 glsl_symbol_table *const symtab = state->symbols;
580
Eric Anholt177ccd22012-03-09 11:38:34 -0800581 if (add_deprecated) {
582 for (unsigned i = 0
583 ; i < Elements(builtin_110_deprecated_uniforms)
584 ; i++) {
585 add_builtin_variable(instructions, symtab,
586 & builtin_110_deprecated_uniforms[i]);
587 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700588 }
589
Eric Anholt177ccd22012-03-09 11:38:34 -0800590 if (add_deprecated) {
591 add_builtin_constant(instructions, symtab, "gl_MaxLights",
592 state->Const.MaxLights);
593 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
594 state->Const.MaxClipPlanes);
595 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
596 state->Const.MaxTextureUnits);
597 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
598 state->Const.MaxTextureCoords);
599 }
Ian Romanickdfdff942011-01-24 16:45:11 -0800600 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Eric Anholtf8946692010-07-20 14:03:35 -0700601 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800602 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700603 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800604 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
Eric Anholtf8946692010-07-20 14:03:35 -0700605 state->Const.MaxVaryingFloats);
Ian Romanickdfdff942011-01-24 16:45:11 -0800606 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700607 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800608 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700609 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800610 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700611 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800612 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700613 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700614
Eric Anholt177ccd22012-03-09 11:38:34 -0800615 if (add_deprecated) {
616 const glsl_type *const mat4_array_type =
617 glsl_type::get_array_instance(glsl_type::mat4_type,
618 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700619
Eric Anholt177ccd22012-03-09 11:38:34 -0800620 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
621 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
622 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
623 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
624 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700625
Ian Romanickdfdff942011-01-24 16:45:11 -0800626 add_uniform(instructions, symtab, "gl_DepthRange",
627 symtab->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700628
Eric Anholt177ccd22012-03-09 11:38:34 -0800629 if (add_deprecated) {
630 add_uniform(instructions, symtab, "gl_ClipPlane",
631 glsl_type::get_array_instance(glsl_type::vec4_type,
632 state->Const.MaxClipPlanes));
633 add_uniform(instructions, symtab, "gl_Point",
634 symtab->get_type("gl_PointParameters"));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700635
Eric Anholt177ccd22012-03-09 11:38:34 -0800636 const glsl_type *const material_parameters_type =
637 symtab->get_type("gl_MaterialParameters");
638 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
639 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700640
Eric Anholt177ccd22012-03-09 11:38:34 -0800641 const glsl_type *const light_source_array_type =
642 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700643
Eric Anholt177ccd22012-03-09 11:38:34 -0800644 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700645
Eric Anholt177ccd22012-03-09 11:38:34 -0800646 const glsl_type *const light_model_products_type =
647 symtab->get_type("gl_LightModelProducts");
648 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
649 light_model_products_type);
650 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
651 light_model_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700652
Eric Anholt177ccd22012-03-09 11:38:34 -0800653 const glsl_type *const light_products_type =
654 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
655 state->Const.MaxLights);
656 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
657 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700658
Eric Anholt177ccd22012-03-09 11:38:34 -0800659 add_uniform(instructions, symtab, "gl_TextureEnvColor",
660 glsl_type::get_array_instance(glsl_type::vec4_type,
661 state->Const.MaxTextureUnits));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700662
Eric Anholt177ccd22012-03-09 11:38:34 -0800663 const glsl_type *const texcoords_vec4 =
664 glsl_type::get_array_instance(glsl_type::vec4_type,
665 state->Const.MaxTextureCoords);
666 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
667 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
668 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
669 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
670 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
671 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
672 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
673 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700674
Eric Anholt177ccd22012-03-09 11:38:34 -0800675 add_uniform(instructions, symtab, "gl_Fog",
676 symtab->get_type("gl_FogParameters"));
677 }
Eric Anholtf868cb02011-07-16 17:41:26 -0700678
679 /* Mesa-internal current attrib state */
680 const glsl_type *const vert_attribs =
681 glsl_type::get_array_instance(glsl_type::vec4_type, VERT_ATTRIB_MAX);
682 add_uniform(instructions, symtab, "gl_CurrentAttribVertMESA", vert_attribs);
683 const glsl_type *const frag_attribs =
684 glsl_type::get_array_instance(glsl_type::vec4_type, FRAG_ATTRIB_MAX);
685 add_uniform(instructions, symtab, "gl_CurrentAttribFragMESA", frag_attribs);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700686}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800687
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700688/* This function should only be called for ES, not desktop GL. */
689static void
690generate_100ES_vs_variables(exec_list *instructions,
691 struct _mesa_glsl_parse_state *state)
692{
693 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800694 add_builtin_variable(instructions, state->symbols,
695 & builtin_core_vs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700696 }
697
698 generate_100ES_uniforms(instructions, state);
699
700 generate_ARB_draw_buffers_variables(instructions, state, false,
701 vertex_shader);
702}
703
Paul Berry8dec1bf2012-08-04 10:29:49 -0700704static void
705generate_300ES_vs_variables(exec_list *instructions,
706 struct _mesa_glsl_parse_state *state)
707{
708 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
709 add_builtin_variable(instructions, state->symbols,
710 & builtin_core_vs_variables[i]);
711 }
712
713 for (unsigned i = 0; i < Elements(builtin_300ES_vs_variables); i++) {
714 add_builtin_variable(instructions, state->symbols,
715 & builtin_300ES_vs_variables[i]);
716 }
717
718 generate_300ES_uniforms(instructions, state);
719
720 generate_ARB_draw_buffers_variables(instructions, state, false,
721 vertex_shader);
722}
723
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700724
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800725static void
726generate_110_vs_variables(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -0800727 struct _mesa_glsl_parse_state *state,
728 bool add_deprecated)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800729{
730 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800731 add_builtin_variable(instructions, state->symbols,
732 & builtin_core_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800733 }
734
Eric Anholt177ccd22012-03-09 11:38:34 -0800735 if (add_deprecated) {
736 for (unsigned i = 0
737 ; i < Elements(builtin_110_deprecated_vs_variables)
738 ; i++) {
739 add_builtin_variable(instructions, state->symbols,
740 & builtin_110_deprecated_vs_variables[i]);
741 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800742 }
Eric Anholt177ccd22012-03-09 11:38:34 -0800743 generate_110_uniforms(instructions, state, add_deprecated);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800744
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700745 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
746 *
747 * "As with all arrays, indices used to subscript gl_TexCoord must
748 * either be an integral constant expressions, or this array must be
749 * re-declared by the shader with a size. The size can be at most
750 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
751 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800752 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700753 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700754 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700755
Ian Romanickdfdff942011-01-24 16:45:11 -0800756 add_variable(instructions, state->symbols,
757 "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
Ian Romanick22971e92010-06-29 15:29:56 -0700758
759 generate_ARB_draw_buffers_variables(instructions, state, false,
760 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800761}
762
763
764static void
765generate_120_vs_variables(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -0800766 struct _mesa_glsl_parse_state *state,
767 bool add_deprecated)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800768{
769 /* GLSL version 1.20 did not add any built-in variables in the vertex
770 * shader.
771 */
Eric Anholt177ccd22012-03-09 11:38:34 -0800772 generate_110_vs_variables(instructions, state, add_deprecated);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800773}
774
775
776static void
Paul Berry36c4b1a2011-08-11 17:33:06 -0700777generate_130_uniforms(exec_list *instructions,
778 struct _mesa_glsl_parse_state *state)
779{
780 glsl_symbol_table *const symtab = state->symbols;
781
782 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
783 state->Const.MaxClipPlanes);
Eric Anholt2ecfa882011-10-21 15:17:16 -0700784 add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
785 state->Const.MaxVaryingFloats);
Paul Berry36c4b1a2011-08-11 17:33:06 -0700786}
787
788
789static void
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800790generate_130_vs_variables(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -0800791 struct _mesa_glsl_parse_state *state,
792 bool add_deprecated)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800793{
Eric Anholt177ccd22012-03-09 11:38:34 -0800794 generate_120_vs_variables(instructions, state, add_deprecated);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800795
796 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800797 add_builtin_variable(instructions, state->symbols,
798 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800799 }
800
Paul Berry36c4b1a2011-08-11 17:33:06 -0700801 generate_130_uniforms(instructions, state);
802
Paul Berryaf243b52011-08-11 15:03:19 -0700803 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
804 * Variables):
805 *
806 * The gl_ClipDistance array is predeclared as unsized and must
807 * be sized by the shader either redeclaring it with a size or
808 * indexing it only with integral constant expressions.
809 *
810 * We represent this in Mesa by initially declaring the array as
811 * size 0.
812 */
Eric Anholt271e1992010-04-02 23:47:06 -0700813 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700814 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700815
Ian Romanickdfdff942011-01-24 16:45:11 -0800816 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700817 "gl_ClipDistance", clip_distance_array_type, ir_var_out,
818 VERT_RESULT_CLIP_DIST0);
Eric Anholt271e1992010-04-02 23:47:06 -0700819
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800820}
821
822
823static void
824initialize_vs_variables(exec_list *instructions,
825 struct _mesa_glsl_parse_state *state)
826{
Paul Berry8dec1bf2012-08-04 10:29:49 -0700827 if (state->es_shader) {
828 switch (state->language_version) {
829 case 100:
830 generate_100ES_vs_variables(instructions, state);
831 break;
832 case 300:
833 generate_300ES_vs_variables(instructions, state);
834 break;
835 default:
836 assert(!"Unexpected language version");
837 break;
838 }
839 } else {
840 switch (state->language_version) {
841 case 110:
842 generate_110_vs_variables(instructions, state, true);
843 break;
844 case 120:
845 generate_120_vs_variables(instructions, state, true);
846 break;
847 case 130:
848 generate_130_vs_variables(instructions, state, true);
849 break;
850 case 140:
851 generate_130_vs_variables(instructions, state, false);
852 break;
853 default:
854 assert(!"Unexpected language version");
855 break;
856 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800857 }
Brian Paul7ce18632010-12-08 18:25:38 -0700858
Eric Anholt177ccd22012-03-09 11:38:34 -0800859 generate_ARB_draw_instanced_variables(instructions, state, false,
860 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800861}
862
Brian Paul7ce18632010-12-08 18:25:38 -0700863
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700864/* This function should only be called for ES, not desktop GL. */
865static void
866generate_100ES_fs_variables(exec_list *instructions,
867 struct _mesa_glsl_parse_state *state)
868{
869 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800870 add_builtin_variable(instructions, state->symbols,
871 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700872 }
873
874 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800875 add_builtin_variable(instructions, state->symbols,
876 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700877 }
878
879 generate_100ES_uniforms(instructions, state);
880
881 generate_ARB_draw_buffers_variables(instructions, state, false,
882 fragment_shader);
883}
884
Eric Anholtb3f743a2010-03-25 14:48:25 -0700885static void
Paul Berry8dec1bf2012-08-04 10:29:49 -0700886generate_300ES_fs_variables(exec_list *instructions,
887 struct _mesa_glsl_parse_state *state)
888{
889 /* Note: we don't add builtin_core_fs_variables, because it contains
890 * gl_FragColor, which is not in GLSL 3.00 ES.
891 */
892
893 for (unsigned i = 0; i < Elements(builtin_300ES_fs_variables); i++) {
894 add_builtin_variable(instructions, state->symbols,
895 & builtin_300ES_fs_variables[i]);
896 }
897
898 generate_300ES_uniforms(instructions, state);
899
900 generate_ARB_draw_buffers_variables(instructions, state, false,
901 fragment_shader);
902}
903
904static void
Eric Anholtb3f743a2010-03-25 14:48:25 -0700905generate_110_fs_variables(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -0800906 struct _mesa_glsl_parse_state *state,
907 bool add_deprecated)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700908{
909 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800910 add_builtin_variable(instructions, state->symbols,
911 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700912 }
913
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700914 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800915 add_builtin_variable(instructions, state->symbols,
916 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700917 }
918
Eric Anholt177ccd22012-03-09 11:38:34 -0800919 if (add_deprecated) {
920 for (unsigned i = 0
921 ; i < Elements(builtin_110_deprecated_fs_variables)
922 ; i++) {
923 add_builtin_variable(instructions, state->symbols,
924 & builtin_110_deprecated_fs_variables[i]);
925 }
Eric Anholt0f09aea2010-03-27 12:48:57 -0700926 }
Eric Anholt177ccd22012-03-09 11:38:34 -0800927
928 generate_110_uniforms(instructions, state, add_deprecated);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700929
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700930 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
931 *
932 * "As with all arrays, indices used to subscript gl_TexCoord must
933 * either be an integral constant expressions, or this array must be
934 * re-declared by the shader with a size. The size can be at most
935 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
936 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700937 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700938 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700939 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700940
Ian Romanickdfdff942011-01-24 16:45:11 -0800941 add_variable(instructions, state->symbols,
942 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700943
Ian Romanick22971e92010-06-29 15:29:56 -0700944 generate_ARB_draw_buffers_variables(instructions, state, false,
945 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700946}
947
Ian Romanickc77b2572010-04-07 16:59:46 -0700948
949static void
Ian Romanick22971e92010-06-29 15:29:56 -0700950generate_ARB_draw_buffers_variables(exec_list *instructions,
951 struct _mesa_glsl_parse_state *state,
952 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700953{
Ian Romanick22971e92010-06-29 15:29:56 -0700954 /* gl_MaxDrawBuffers is available in all shader stages.
955 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700956 ir_variable *const mdb =
Ian Romanickd3b39192011-10-31 14:43:27 -0700957 add_builtin_constant(instructions, state->symbols, "gl_MaxDrawBuffers",
958 state->Const.MaxDrawBuffers);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700959
960 if (warn)
961 mdb->warn_extension = "GL_ARB_draw_buffers";
962
Ian Romanick22971e92010-06-29 15:29:56 -0700963 /* gl_FragData is only available in the fragment shader.
Paul Berry8dec1bf2012-08-04 10:29:49 -0700964 * It is not present in GLSL 3.00 ES.
Ian Romanick22971e92010-06-29 15:29:56 -0700965 */
Paul Berry8dec1bf2012-08-04 10:29:49 -0700966 if (target == fragment_shader && !state->is_version(0, 300)) {
Ian Romanick22971e92010-06-29 15:29:56 -0700967 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700968 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700969 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700970
Ian Romanick22971e92010-06-29 15:29:56 -0700971 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800972 add_variable(instructions, state->symbols,
973 "gl_FragData", vec4_array_type,
974 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700975
976 if (warn)
977 fd->warn_extension = "GL_ARB_draw_buffers";
978 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700979}
980
Brian Paul7ce18632010-12-08 18:25:38 -0700981
982static void
983generate_ARB_draw_instanced_variables(exec_list *instructions,
984 struct _mesa_glsl_parse_state *state,
985 bool warn,
986 _mesa_glsl_parser_targets target)
987{
988 /* gl_InstanceIDARB is only available in the vertex shader.
989 */
Eric Anholt177ccd22012-03-09 11:38:34 -0800990 if (target != vertex_shader)
991 return;
992
993 if (state->ARB_draw_instanced_enable) {
Ian Romanick1e6a2c12012-02-29 08:29:39 -0800994 ir_variable *inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800995 add_variable(instructions, state->symbols,
996 "gl_InstanceIDARB", glsl_type::int_type,
997 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700998
999 if (warn)
1000 inst->warn_extension = "GL_ARB_draw_instanced";
Eric Anholt177ccd22012-03-09 11:38:34 -08001001 }
Ian Romanick1e6a2c12012-02-29 08:29:39 -08001002
Paul Berry8dec1bf2012-08-04 10:29:49 -07001003 bool available_in_core = state->is_version(140, 300);
1004 if (state->ARB_draw_instanced_enable || available_in_core) {
Ian Romanick1e6a2c12012-02-29 08:29:39 -08001005 /* Originally ARB_draw_instanced only specified that ARB decorated name.
1006 * Since no vendor actually implemented that behavior and some apps use
1007 * the undecorated name, the extension now specifies that both names are
1008 * available.
1009 */
Eric Anholt177ccd22012-03-09 11:38:34 -08001010 ir_variable *inst =
1011 add_variable(instructions, state->symbols,
1012 "gl_InstanceID", glsl_type::int_type,
1013 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Ian Romanick1e6a2c12012-02-29 08:29:39 -08001014
Paul Berry8dec1bf2012-08-04 10:29:49 -07001015 if (!available_in_core && warn)
Ian Romanick1e6a2c12012-02-29 08:29:39 -08001016 inst->warn_extension = "GL_ARB_draw_instanced";
Brian Paul7ce18632010-12-08 18:25:38 -07001017 }
1018}
1019
1020
Dave Airlied9671862010-10-06 09:36:02 +10001021static void
1022generate_ARB_shader_stencil_export_variables(exec_list *instructions,
1023 struct _mesa_glsl_parse_state *state,
1024 bool warn)
1025{
1026 /* gl_FragStencilRefARB is only available in the fragment shader.
1027 */
1028 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -08001029 add_variable(instructions, state->symbols,
1030 "gl_FragStencilRefARB", glsl_type::int_type,
1031 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +10001032
1033 if (warn)
1034 fd->warn_extension = "GL_ARB_shader_stencil_export";
1035}
Ian Romanickc77b2572010-04-07 16:59:46 -07001036
Eric Anholtb3f743a2010-03-25 14:48:25 -07001037static void
Marek Olšák5ba2e7a2011-05-02 16:41:04 +02001038generate_AMD_shader_stencil_export_variables(exec_list *instructions,
1039 struct _mesa_glsl_parse_state *state,
1040 bool warn)
1041{
1042 /* gl_FragStencilRefAMD is only available in the fragment shader.
1043 */
1044 ir_variable *const fd =
1045 add_variable(instructions, state->symbols,
1046 "gl_FragStencilRefAMD", glsl_type::int_type,
1047 ir_var_out, FRAG_RESULT_STENCIL);
1048
1049 if (warn)
1050 fd->warn_extension = "GL_AMD_shader_stencil_export";
1051}
1052
1053static void
Eric Anholtb3f743a2010-03-25 14:48:25 -07001054generate_120_fs_variables(exec_list *instructions,
Eric Anholt177ccd22012-03-09 11:38:34 -08001055 struct _mesa_glsl_parse_state *state,
1056 bool add_deprecated)
Eric Anholtb3f743a2010-03-25 14:48:25 -07001057{
Eric Anholt177ccd22012-03-09 11:38:34 -08001058 generate_110_fs_variables(instructions, state, add_deprecated);
Eric Anholt152b55e2010-07-07 19:45:22 -07001059
1060 for (unsigned i = 0
1061 ; i < Elements(builtin_120_fs_variables)
1062 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -08001063 add_builtin_variable(instructions, state->symbols,
1064 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -07001065 }
Eric Anholtb3f743a2010-03-25 14:48:25 -07001066}
1067
1068static void
Eric Anholt177ccd22012-03-09 11:38:34 -08001069generate_fs_clipdistance(exec_list *instructions,
1070 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -07001071{
Paul Berryaf243b52011-08-11 15:03:19 -07001072 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
1073 * Variables):
1074 *
1075 * The built-in input variable gl_ClipDistance array contains linearly
1076 * interpolated values for the vertex values written by the vertex shader
1077 * to the gl_ClipDistance vertex output variable. This array must be
1078 * sized in the fragment shader either implicitly or explicitly to be the
1079 * same size as it was sized in the vertex shader.
1080 *
1081 * In other words, the array must be pre-declared as implicitly sized. We
1082 * represent this in Mesa by initially declaring the array as size 0.
1083 */
Ian Romanick8645a952010-04-07 16:47:44 -07001084 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -07001085 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -07001086
Ian Romanickdfdff942011-01-24 16:45:11 -08001087 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -07001088 "gl_ClipDistance", clip_distance_array_type, ir_var_in,
1089 FRAG_ATTRIB_CLIP_DIST0);
Eric Anholtb3f743a2010-03-25 14:48:25 -07001090}
1091
1092static void
Eric Anholt177ccd22012-03-09 11:38:34 -08001093generate_130_fs_variables(exec_list *instructions,
1094 struct _mesa_glsl_parse_state *state)
1095{
1096 generate_120_fs_variables(instructions, state, true);
1097
1098 generate_130_uniforms(instructions, state);
1099 generate_fs_clipdistance(instructions, state);
1100}
1101
1102
1103static void
1104generate_140_fs_variables(exec_list *instructions,
1105 struct _mesa_glsl_parse_state *state)
1106{
1107 generate_120_fs_variables(instructions, state, false);
1108
1109 generate_130_uniforms(instructions, state);
1110 generate_fs_clipdistance(instructions, state);
1111}
1112
1113static void
Eric Anholtb3f743a2010-03-25 14:48:25 -07001114initialize_fs_variables(exec_list *instructions,
1115 struct _mesa_glsl_parse_state *state)
1116{
Paul Berry8dec1bf2012-08-04 10:29:49 -07001117 if (state->es_shader) {
1118 switch (state->language_version) {
1119 case 100:
1120 generate_100ES_fs_variables(instructions, state);
1121 break;
1122 case 300:
1123 generate_300ES_fs_variables(instructions, state);
1124 break;
1125 default:
1126 assert(!"Unexpected language version");
1127 break;
1128 }
1129 } else {
1130 switch (state->language_version) {
1131 case 110:
1132 generate_110_fs_variables(instructions, state, true);
1133 break;
1134 case 120:
1135 generate_120_fs_variables(instructions, state, true);
1136 break;
1137 case 130:
1138 generate_130_fs_variables(instructions, state);
1139 break;
1140 case 140:
1141 generate_140_fs_variables(instructions, state);
1142 break;
1143 default:
1144 assert(!"Unexpected language version");
1145 break;
1146 }
Eric Anholtb3f743a2010-03-25 14:48:25 -07001147 }
Dave Airlied9671862010-10-06 09:36:02 +10001148
1149 if (state->ARB_shader_stencil_export_enable)
1150 generate_ARB_shader_stencil_export_variables(instructions, state,
1151 state->ARB_shader_stencil_export_warn);
Marek Olšák5ba2e7a2011-05-02 16:41:04 +02001152
1153 if (state->AMD_shader_stencil_export_enable)
1154 generate_AMD_shader_stencil_export_variables(instructions, state,
1155 state->AMD_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -07001156}
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001157
1158void
1159_mesa_glsl_initialize_variables(exec_list *instructions,
1160 struct _mesa_glsl_parse_state *state)
1161{
1162 switch (state->target) {
1163 case vertex_shader:
1164 initialize_vs_variables(instructions, state);
1165 break;
1166 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -07001167 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001168 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -07001169 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001170 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001171 }
1172}