Merge branch 'origin' into glsl-compiler-1
[mesa.git] / src / mesa / shader / slang / slang_builtin.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_builtin.c
27 * Resolve built-in uniform vars.
28 * \author Brian Paul
29 */
30
31 #include "imports.h"
32 #include "slang_builtin.h"
33 #include "slang_ir.h"
34 #include "mtypes.h"
35 #include "program.h"
36 #include "prog_instruction.h"
37 #include "prog_parameter.h"
38 #include "prog_statevars.h"
39
40
41 /**
42 * Lookup GL state given a variable name, 0, 1 or 2 indexes and a field.
43 * Allocate room for the state in the given param list and return position
44 * in the list.
45 * Yes, this is kind of ugly, but it works.
46 */
47 static GLint
48 lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
49 GLuint *swizzleOut,
50 struct gl_program_parameter_list *paramList)
51 {
52 gl_state_index tokens[STATE_LENGTH];
53 GLuint i;
54 GLboolean isMatrix = GL_FALSE;
55
56 for (i = 0; i < STATE_LENGTH; i++) {
57 tokens[i] = 0;
58 }
59 *swizzleOut = SWIZZLE_NOOP;
60
61 if (strcmp(var, "gl_ModelViewMatrix") == 0) {
62 tokens[0] = STATE_MODELVIEW_MATRIX;
63 isMatrix = GL_TRUE;
64 }
65 else if (strcmp(var, "gl_ModelProjectionMatrix") == 0) {
66 tokens[0] = STATE_PROJECTION_MATRIX;
67 isMatrix = GL_TRUE;
68 }
69 else if (strcmp(var, "gl_ModelViewProjectionMatrix") == 0) {
70 tokens[0] = STATE_MVP_MATRIX;
71 isMatrix = GL_TRUE;
72 }
73 else if (strcmp(var, "gl_NormalMatrix") == 0) {
74 tokens[0] = STATE_MODELVIEW_MATRIX;
75 isMatrix = GL_TRUE;
76 }
77 else if (strcmp(var, "gl_TextureMatrix") == 0) {
78 tokens[0] = STATE_TEXTURE_MATRIX;
79 if (index2 >= 0)
80 tokens[1] = index2;
81 isMatrix = GL_TRUE;
82 }
83 else if (strcmp(var, "gl_DepthRange") == 0) {
84 tokens[0] = STATE_DEPTH_RANGE;
85 if (strcmp(field, "near") == 0) {
86 *swizzleOut = SWIZZLE_XXXX;
87 }
88 else if (strcmp(field, "far") == 0) {
89 *swizzleOut = SWIZZLE_YYYY;
90 }
91 else if (strcmp(field, "diff") == 0) {
92 *swizzleOut = SWIZZLE_ZZZZ;
93 }
94 else {
95 return -1;
96 }
97 }
98 else if (strcmp(var, "gl_ClipPlane") == 0) {
99 tokens[0] = STATE_CLIPPLANE;
100 tokens[1] = index1;
101 }
102 else if (strcmp(var, "gl_FrontMaterial") == 0 ||
103 strcmp(var, "gl_BackMaterial") == 0) {
104 tokens[0] = STATE_MATERIAL;
105 if (strcmp(var, "gl_FrontMaterial") == 0)
106 tokens[1] = 0;
107 else
108 tokens[1] = 1;
109 if (strcmp(field, "emission") == 0) {
110 tokens[2] = STATE_EMISSION;
111 }
112 else if (strcmp(field, "ambient") == 0) {
113 tokens[2] = STATE_AMBIENT;
114 }
115 else if (strcmp(field, "diffuse") == 0) {
116 tokens[2] = STATE_DIFFUSE;
117 }
118 else if (strcmp(field, "specular") == 0) {
119 tokens[2] = STATE_SPECULAR;
120 }
121 else if (strcmp(field, "shininess") == 0) {
122 tokens[2] = STATE_SHININESS;
123 *swizzleOut = SWIZZLE_XXXX;
124 }
125 else {
126 return -1;
127 }
128 }
129 else if (strcmp(var, "gl_LightSource") == 0) {
130 tokens[0] = STATE_LIGHT;
131 tokens[1] = index1;
132 if (strcmp(field, "ambient") == 0) {
133 tokens[2] = STATE_AMBIENT;
134 }
135 else if (strcmp(field, "diffuse") == 0) {
136 tokens[2] = STATE_DIFFUSE;
137 }
138 else if (strcmp(field, "specular") == 0) {
139 tokens[2] = STATE_SPECULAR;
140 }
141 else if (strcmp(field, "position") == 0) {
142 tokens[2] = STATE_POSITION;
143 }
144 else if (strcmp(field, "halfVector") == 0) {
145 tokens[2] = STATE_HALF_VECTOR;
146 }
147 else if (strcmp(field, "spotDirection") == 0) {
148 tokens[2] = STATE_SPOT_DIRECTION;
149 }
150 else if (strcmp(field, "spotCosCutoff") == 0) {
151 tokens[2] = STATE_SPOT_DIRECTION;
152 *swizzleOut = SWIZZLE_WWWW;
153 }
154 else if (strcmp(field, "spotCutoff") == 0) {
155 tokens[2] = STATE_SPOT_CUTOFF;
156 *swizzleOut = SWIZZLE_XXXX;
157 }
158 else if (strcmp(field, "spotExponent") == 0) {
159 tokens[2] = STATE_ATTENUATION;
160 *swizzleOut = SWIZZLE_WWWW;
161 }
162 else if (strcmp(field, "constantAttenuation") == 0) {
163 tokens[2] = STATE_ATTENUATION;
164 *swizzleOut = SWIZZLE_XXXX;
165 }
166 else if (strcmp(field, "linearAttenuation") == 0) {
167 tokens[2] = STATE_ATTENUATION;
168 *swizzleOut = SWIZZLE_YYYY;
169 }
170 else if (strcmp(field, "quadraticAttenuation") == 0) {
171 tokens[2] = STATE_ATTENUATION;
172 *swizzleOut = SWIZZLE_ZZZZ;
173 }
174 else {
175 return -1;
176 }
177 }
178 else if (strcmp(var, "gl_LightModel") == 0) {
179 if (strcmp(field, "ambient") == 0) {
180 tokens[0] = STATE_LIGHTMODEL_AMBIENT;
181 }
182 else {
183 return -1;
184 }
185 }
186 else if (strcmp(var, "gl_FrontLightModelProduct") == 0) {
187 if (strcmp(field, "ambient") == 0) {
188 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
189 tokens[1] = 0;
190 }
191 else {
192 return -1;
193 }
194 }
195 else if (strcmp(var, "gl_BackLightModelProduct") == 0) {
196 if (strcmp(field, "ambient") == 0) {
197 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
198 tokens[1] = 1;
199 }
200 else {
201 return -1;
202 }
203 }
204 else if (strcmp(var, "gl_FrontLightProduct") == 0 ||
205 strcmp(var, "gl_BackLightProduct") == 0) {
206 tokens[0] = STATE_LIGHTPROD;
207 tokens[1] = index1; /* light number */
208 if (strcmp(var, "gl_FrontLightProduct") == 0) {
209 tokens[2] = 0; /* front */
210 }
211 else {
212 tokens[2] = 1; /* back */
213 }
214 if (strcmp(field, "ambient") == 0) {
215 tokens[3] = STATE_AMBIENT;
216 }
217 else if (strcmp(field, "diffuset") == 0) {
218 tokens[3] = STATE_DIFFUSE;
219 }
220 else if (strcmp(field, "specular") == 0) {
221 tokens[3] = STATE_SPECULAR;
222 }
223 else {
224 return -1;
225 }
226 }
227 else if (strcmp(var, "gl_TextureEnvColor") == 0) {
228 tokens[0] = STATE_TEXENV_COLOR;
229 tokens[1] = index1;
230 }
231 else if (strcmp(var, "gl_EyePlaneS") == 0) {
232 tokens[0] = STATE_TEXGEN;
233 tokens[1] = index1; /* tex unit */
234 tokens[2] = STATE_TEXGEN_EYE_S;
235 }
236 else if (strcmp(var, "gl_EyePlaneT") == 0) {
237 tokens[0] = STATE_TEXGEN;
238 tokens[1] = index1; /* tex unit */
239 tokens[2] = STATE_TEXGEN_EYE_T;
240 }
241 else if (strcmp(var, "gl_EyePlaneR") == 0) {
242 tokens[0] = STATE_TEXGEN;
243 tokens[1] = index1; /* tex unit */
244 tokens[2] = STATE_TEXGEN_EYE_R;
245 }
246 else if (strcmp(var, "gl_EyePlaneQ") == 0) {
247 tokens[0] = STATE_TEXGEN;
248 tokens[1] = index1; /* tex unit */
249 tokens[2] = STATE_TEXGEN_EYE_Q;
250 }
251 else if (strcmp(var, "gl_ObjectPlaneS") == 0) {
252 tokens[0] = STATE_TEXGEN;
253 tokens[1] = index1; /* tex unit */
254 tokens[2] = STATE_TEXGEN_OBJECT_S;
255 }
256 else if (strcmp(var, "gl_ObjectPlaneT") == 0) {
257 tokens[0] = STATE_TEXGEN;
258 tokens[1] = index1; /* tex unit */
259 tokens[2] = STATE_TEXGEN_OBJECT_T;
260 }
261 else if (strcmp(var, "gl_ObjectPlaneR") == 0) {
262 tokens[0] = STATE_TEXGEN;
263 tokens[1] = index1; /* tex unit */
264 tokens[2] = STATE_TEXGEN_OBJECT_R;
265 }
266 else if (strcmp(var, "gl_ObjectPlaneQ") == 0) {
267 tokens[0] = STATE_TEXGEN;
268 tokens[1] = index1; /* tex unit */
269 tokens[2] = STATE_TEXGEN_OBJECT_Q;
270 }
271 else if (strcmp(var, "gl_Fog") == 0) {
272 tokens[0] = STATE_FOG;
273 if (strcmp(field, "color") == 0) {
274 tokens[1] = STATE_FOG_COLOR;
275 }
276 else if (strcmp(field, "density") == 0) {
277 tokens[1] = STATE_FOG_PARAMS;
278 *swizzleOut = SWIZZLE_XXXX;
279 }
280 else if (strcmp(field, "start") == 0) {
281 tokens[1] = STATE_FOG_PARAMS;
282 *swizzleOut = SWIZZLE_YYYY;
283 }
284 else if (strcmp(field, "end") == 0) {
285 tokens[1] = STATE_FOG_PARAMS;
286 *swizzleOut = SWIZZLE_ZZZZ;
287 }
288 else if (strcmp(field, "scale") == 0) {
289 tokens[1] = STATE_FOG_PARAMS;
290 *swizzleOut = SWIZZLE_WWWW;
291 }
292 else {
293 return -1;
294 }
295 }
296 else {
297 return -1;
298 }
299
300 if (isMatrix) {
301 /* load all four columns of matrix */
302 GLint pos[4];
303 GLuint j;
304 for (j = 0; j < 4; j++) {
305 tokens[2] = tokens[3] = j; /* jth row of matrix */
306 pos[j] = _mesa_add_state_reference(paramList, (GLint *) tokens);
307 assert(pos[j] >= 0);
308 ASSERT(pos[j] >= 0);
309 }
310 return pos[0] + index1;
311 }
312 else {
313 /* allocate a single register */
314 GLint pos = _mesa_add_state_reference(paramList, (GLint *) tokens);
315 ASSERT(pos >= 0);
316 return pos;
317 }
318 }
319
320
321 /**
322 * Allocate storage for a pre-defined uniform (a GL state variable).
323 * As a memory-saving optimization, we try to only allocate storage for
324 * state vars that are actually used.
325 * For example, the "gl_LightSource" uniform is huge. If we only use
326 * a handful of gl_LightSource fields, we don't want to allocate storage
327 * for all of gl_LightSource.
328 *
329 * Currently, all pre-defined uniforms are in one of these forms:
330 * var
331 * var[i]
332 * var.field
333 * var[i].field
334 * var[i][j]
335 */
336 GLint
337 _slang_alloc_statevar(slang_ir_node *n,
338 struct gl_program_parameter_list *paramList)
339 {
340 const char *field = NULL, *var;
341 GLint index1 = -1, index2 = -1, pos;
342 GLuint swizzle;
343
344 if (n->Opcode == IR_FIELD) {
345 field = n->Target;
346 n = n->Children[0];
347 }
348
349 if (n->Opcode == IR_ELEMENT) {
350 /* XXX can only handle constant indexes for now */
351 assert(n->Children[1]->Opcode == IR_FLOAT);
352 index1 = (GLint) n->Children[1]->Value[0];
353 n = n->Children[0];
354 }
355
356 if (n->Opcode == IR_ELEMENT) {
357 /* XXX can only handle constant indexes for now */
358 assert(n->Children[1]->Opcode == IR_FLOAT);
359 index2 = (GLint) n->Children[1]->Value[0];
360 n = n->Children[0];
361 }
362
363 assert(n->Opcode == IR_VAR);
364 var = (char *) n->Var->a_name;
365
366 pos = lookup_statevar(var, index1, index2, field, &swizzle, paramList);
367 assert(pos >= 0);
368 if (pos >= 0) {
369 n->Store->Index = pos;
370 n->Store->Swizzle = swizzle;
371 }
372 return pos;
373 }
374