Merge branch 'master' of git+ssh://znh@git.freedesktop.org/git/mesa/mesa into 965...
[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 "main/imports.h"
32 #include "main/mtypes.h"
33 #include "shader/program.h"
34 #include "shader/prog_instruction.h"
35 #include "shader/prog_parameter.h"
36 #include "shader/prog_statevars.h"
37 #include "shader/slang/slang_ir.h"
38 #include "shader/slang/slang_builtin.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 /*
53 * NOTE: The ARB_vertex_program extension specified that matrices get
54 * loaded in registers in row-major order. With GLSL, we want column-
55 * major order. So, we need to transpose all matrices here...
56 */
57 static const struct {
58 const char *name;
59 gl_state_index matrix;
60 gl_state_index modifier;
61 } matrices[] = {
62 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE },
63 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS },
64 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX, 0 },
65 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE },
66
67 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE },
68 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS },
69 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX, 0 },
70 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE },
71
72 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE },
73 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS },
74 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX, 0 },
75 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX, STATE_MATRIX_INVERSE },
76
77 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE },
78 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS },
79 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX, 0 },
80 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE },
81
82 /* XXX verify these!!! */
83 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE },
84 { "__NormalMatrixTranspose", STATE_MODELVIEW_MATRIX, 0 },
85
86 { NULL, 0, 0 }
87 };
88 gl_state_index tokens[STATE_LENGTH];
89 GLuint i;
90 GLboolean isMatrix = GL_FALSE;
91
92 for (i = 0; i < STATE_LENGTH; i++) {
93 tokens[i] = 0;
94 }
95 *swizzleOut = SWIZZLE_NOOP;
96
97 /* first, look if var is a pre-defined matrix */
98 for (i = 0; matrices[i].name; i++) {
99 if (strcmp(var, matrices[i].name) == 0) {
100 tokens[0] = matrices[i].matrix;
101 /* tokens[1], [2] and [3] filled below */
102 tokens[4] = matrices[i].modifier;
103 isMatrix = GL_TRUE;
104 break;
105 }
106 }
107
108 if (isMatrix) {
109 if (tokens[0] == STATE_TEXTURE_MATRIX) {
110 if (index1 >= 0) {
111 tokens[1] = index1;
112 index1 = 0; /* prevent extra addition at end of function */
113 }
114 }
115 }
116 else if (strcmp(var, "gl_DepthRange") == 0) {
117 tokens[0] = STATE_DEPTH_RANGE;
118 if (strcmp(field, "near") == 0) {
119 *swizzleOut = SWIZZLE_XXXX;
120 }
121 else if (strcmp(field, "far") == 0) {
122 *swizzleOut = SWIZZLE_YYYY;
123 }
124 else if (strcmp(field, "diff") == 0) {
125 *swizzleOut = SWIZZLE_ZZZZ;
126 }
127 else {
128 return -1;
129 }
130 }
131 else if (strcmp(var, "gl_ClipPlane") == 0) {
132 tokens[0] = STATE_CLIPPLANE;
133 tokens[1] = index1;
134 }
135 else if (strcmp(var, "gl_Point") == 0) {
136 if (strcmp(field, "size") == 0) {
137 tokens[0] = STATE_POINT_SIZE;
138 *swizzleOut = SWIZZLE_XXXX;
139 }
140 else if (strcmp(field, "sizeMin") == 0) {
141 tokens[0] = STATE_POINT_SIZE;
142 *swizzleOut = SWIZZLE_YYYY;
143 }
144 else if (strcmp(field, "sizeMax") == 0) {
145 tokens[0] = STATE_POINT_SIZE;
146 *swizzleOut = SWIZZLE_ZZZZ;
147 }
148 else if (strcmp(field, "fadeThresholdSize") == 0) {
149 tokens[0] = STATE_POINT_SIZE;
150 *swizzleOut = SWIZZLE_WWWW;
151 }
152 else if (strcmp(field, "distanceConstantAttenuation") == 0) {
153 tokens[0] = STATE_POINT_ATTENUATION;
154 *swizzleOut = SWIZZLE_XXXX;
155 }
156 else if (strcmp(field, "distanceLinearAttenuation") == 0) {
157 tokens[0] = STATE_POINT_ATTENUATION;
158 *swizzleOut = SWIZZLE_YYYY;
159 }
160 else if (strcmp(field, "distanceQuadraticAttenuation") == 0) {
161 tokens[0] = STATE_POINT_ATTENUATION;
162 *swizzleOut = SWIZZLE_ZZZZ;
163 }
164 else {
165 return -1;
166 }
167 }
168 else if (strcmp(var, "gl_FrontMaterial") == 0 ||
169 strcmp(var, "gl_BackMaterial") == 0) {
170 tokens[0] = STATE_MATERIAL;
171 if (strcmp(var, "gl_FrontMaterial") == 0)
172 tokens[1] = 0;
173 else
174 tokens[1] = 1;
175 if (strcmp(field, "emission") == 0) {
176 tokens[2] = STATE_EMISSION;
177 }
178 else if (strcmp(field, "ambient") == 0) {
179 tokens[2] = STATE_AMBIENT;
180 }
181 else if (strcmp(field, "diffuse") == 0) {
182 tokens[2] = STATE_DIFFUSE;
183 }
184 else if (strcmp(field, "specular") == 0) {
185 tokens[2] = STATE_SPECULAR;
186 }
187 else if (strcmp(field, "shininess") == 0) {
188 tokens[2] = STATE_SHININESS;
189 *swizzleOut = SWIZZLE_XXXX;
190 }
191 else {
192 return -1;
193 }
194 }
195 else if (strcmp(var, "gl_LightSource") == 0) {
196 tokens[0] = STATE_LIGHT;
197 tokens[1] = index1;
198 if (strcmp(field, "ambient") == 0) {
199 tokens[2] = STATE_AMBIENT;
200 }
201 else if (strcmp(field, "diffuse") == 0) {
202 tokens[2] = STATE_DIFFUSE;
203 }
204 else if (strcmp(field, "specular") == 0) {
205 tokens[2] = STATE_SPECULAR;
206 }
207 else if (strcmp(field, "position") == 0) {
208 tokens[2] = STATE_POSITION;
209 }
210 else if (strcmp(field, "halfVector") == 0) {
211 tokens[2] = STATE_HALF_VECTOR;
212 }
213 else if (strcmp(field, "spotDirection") == 0) {
214 tokens[2] = STATE_SPOT_DIRECTION;
215 }
216 else if (strcmp(field, "spotCosCutoff") == 0) {
217 tokens[2] = STATE_SPOT_DIRECTION;
218 *swizzleOut = SWIZZLE_WWWW;
219 }
220 else if (strcmp(field, "spotCutoff") == 0) {
221 tokens[2] = STATE_SPOT_CUTOFF;
222 *swizzleOut = SWIZZLE_XXXX;
223 }
224 else if (strcmp(field, "spotExponent") == 0) {
225 tokens[2] = STATE_ATTENUATION;
226 *swizzleOut = SWIZZLE_WWWW;
227 }
228 else if (strcmp(field, "constantAttenuation") == 0) {
229 tokens[2] = STATE_ATTENUATION;
230 *swizzleOut = SWIZZLE_XXXX;
231 }
232 else if (strcmp(field, "linearAttenuation") == 0) {
233 tokens[2] = STATE_ATTENUATION;
234 *swizzleOut = SWIZZLE_YYYY;
235 }
236 else if (strcmp(field, "quadraticAttenuation") == 0) {
237 tokens[2] = STATE_ATTENUATION;
238 *swizzleOut = SWIZZLE_ZZZZ;
239 }
240 else {
241 return -1;
242 }
243 }
244 else if (strcmp(var, "gl_LightModel") == 0) {
245 if (strcmp(field, "ambient") == 0) {
246 tokens[0] = STATE_LIGHTMODEL_AMBIENT;
247 }
248 else {
249 return -1;
250 }
251 }
252 else if (strcmp(var, "gl_FrontLightModelProduct") == 0) {
253 if (strcmp(field, "ambient") == 0) {
254 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
255 tokens[1] = 0;
256 }
257 else {
258 return -1;
259 }
260 }
261 else if (strcmp(var, "gl_BackLightModelProduct") == 0) {
262 if (strcmp(field, "ambient") == 0) {
263 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
264 tokens[1] = 1;
265 }
266 else {
267 return -1;
268 }
269 }
270 else if (strcmp(var, "gl_FrontLightProduct") == 0 ||
271 strcmp(var, "gl_BackLightProduct") == 0) {
272 tokens[0] = STATE_LIGHTPROD;
273 tokens[1] = index1; /* light number */
274 if (strcmp(var, "gl_FrontLightProduct") == 0) {
275 tokens[2] = 0; /* front */
276 }
277 else {
278 tokens[2] = 1; /* back */
279 }
280 if (strcmp(field, "ambient") == 0) {
281 tokens[3] = STATE_AMBIENT;
282 }
283 else if (strcmp(field, "diffuse") == 0) {
284 tokens[3] = STATE_DIFFUSE;
285 }
286 else if (strcmp(field, "specular") == 0) {
287 tokens[3] = STATE_SPECULAR;
288 }
289 else {
290 return -1;
291 }
292 }
293 else if (strcmp(var, "gl_TextureEnvColor") == 0) {
294 tokens[0] = STATE_TEXENV_COLOR;
295 tokens[1] = index1;
296 }
297 else if (strcmp(var, "gl_EyePlaneS") == 0) {
298 tokens[0] = STATE_TEXGEN;
299 tokens[1] = index1; /* tex unit */
300 tokens[2] = STATE_TEXGEN_EYE_S;
301 }
302 else if (strcmp(var, "gl_EyePlaneT") == 0) {
303 tokens[0] = STATE_TEXGEN;
304 tokens[1] = index1; /* tex unit */
305 tokens[2] = STATE_TEXGEN_EYE_T;
306 }
307 else if (strcmp(var, "gl_EyePlaneR") == 0) {
308 tokens[0] = STATE_TEXGEN;
309 tokens[1] = index1; /* tex unit */
310 tokens[2] = STATE_TEXGEN_EYE_R;
311 }
312 else if (strcmp(var, "gl_EyePlaneQ") == 0) {
313 tokens[0] = STATE_TEXGEN;
314 tokens[1] = index1; /* tex unit */
315 tokens[2] = STATE_TEXGEN_EYE_Q;
316 }
317 else if (strcmp(var, "gl_ObjectPlaneS") == 0) {
318 tokens[0] = STATE_TEXGEN;
319 tokens[1] = index1; /* tex unit */
320 tokens[2] = STATE_TEXGEN_OBJECT_S;
321 }
322 else if (strcmp(var, "gl_ObjectPlaneT") == 0) {
323 tokens[0] = STATE_TEXGEN;
324 tokens[1] = index1; /* tex unit */
325 tokens[2] = STATE_TEXGEN_OBJECT_T;
326 }
327 else if (strcmp(var, "gl_ObjectPlaneR") == 0) {
328 tokens[0] = STATE_TEXGEN;
329 tokens[1] = index1; /* tex unit */
330 tokens[2] = STATE_TEXGEN_OBJECT_R;
331 }
332 else if (strcmp(var, "gl_ObjectPlaneQ") == 0) {
333 tokens[0] = STATE_TEXGEN;
334 tokens[1] = index1; /* tex unit */
335 tokens[2] = STATE_TEXGEN_OBJECT_Q;
336 }
337 else if (strcmp(var, "gl_Fog") == 0) {
338 if (strcmp(field, "color") == 0) {
339 tokens[0] = STATE_FOG_COLOR;
340 }
341 else if (strcmp(field, "density") == 0) {
342 tokens[0] = STATE_FOG_PARAMS;
343 *swizzleOut = SWIZZLE_XXXX;
344 }
345 else if (strcmp(field, "start") == 0) {
346 tokens[0] = STATE_FOG_PARAMS;
347 *swizzleOut = SWIZZLE_YYYY;
348 }
349 else if (strcmp(field, "end") == 0) {
350 tokens[0] = STATE_FOG_PARAMS;
351 *swizzleOut = SWIZZLE_ZZZZ;
352 }
353 else if (strcmp(field, "scale") == 0) {
354 tokens[0] = STATE_FOG_PARAMS;
355 *swizzleOut = SWIZZLE_WWWW;
356 }
357 else {
358 return -1;
359 }
360 }
361 else {
362 return -1;
363 }
364
365 if (isMatrix) {
366 /* load all four columns of matrix */
367 GLint pos[4];
368 GLuint j;
369 for (j = 0; j < 4; j++) {
370 tokens[2] = tokens[3] = j; /* jth row of matrix */
371 pos[j] = _mesa_add_state_reference(paramList, tokens);
372 assert(pos[j] >= 0);
373 ASSERT(pos[j] >= 0);
374 }
375 return pos[0] + index1;
376 }
377 else {
378 /* allocate a single register */
379 GLint pos = _mesa_add_state_reference(paramList, tokens);
380 ASSERT(pos >= 0);
381 return pos;
382 }
383 }
384
385
386 /**
387 * Allocate storage for a pre-defined uniform (a GL state variable).
388 * As a memory-saving optimization, we try to only allocate storage for
389 * state vars that are actually used.
390 * For example, the "gl_LightSource" uniform is huge. If we only use
391 * a handful of gl_LightSource fields, we don't want to allocate storage
392 * for all of gl_LightSource.
393 *
394 * Currently, all pre-defined uniforms are in one of these forms:
395 * var
396 * var[i]
397 * var.field
398 * var[i].field
399 * var[i][j]
400 */
401 GLint
402 _slang_alloc_statevar(slang_ir_node *n,
403 struct gl_program_parameter_list *paramList)
404 {
405 slang_ir_node *n0 = n;
406 const char *field = NULL, *var;
407 GLint index1 = -1, index2 = -1, pos;
408 GLuint swizzle;
409
410 if (n->Opcode == IR_FIELD) {
411 field = n->Field;
412 n = n->Children[0];
413 }
414
415 if (n->Opcode == IR_ELEMENT) {
416 /* XXX can only handle constant indexes for now */
417 assert(n->Children[1]->Opcode == IR_FLOAT);
418 index1 = (GLint) n->Children[1]->Value[0];
419 n = n->Children[0];
420 }
421
422 if (n->Opcode == IR_ELEMENT) {
423 /* XXX can only handle constant indexes for now */
424 assert(n->Children[1]->Opcode == IR_FLOAT);
425 index2 = (GLint) n->Children[1]->Value[0];
426 n = n->Children[0];
427 }
428
429 assert(n->Opcode == IR_VAR);
430 var = (char *) n->Var->a_name;
431
432 pos = lookup_statevar(var, index1, index2, field, &swizzle, paramList);
433 assert(pos >= 0);
434 if (pos >= 0) {
435 n0->Store->Index = pos;
436 n0->Store->Swizzle = swizzle;
437 }
438 return pos;
439 }
440