replace GLint with gl_state_index
[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_Point") == 0) {
103 if (strcmp(field, "size") == 0) {
104 tokens[0] = STATE_POINT_SIZE;
105 *swizzleOut = SWIZZLE_XXXX;
106 }
107 /* XXX finish */
108 }
109 else if (strcmp(var, "gl_FrontMaterial") == 0 ||
110 strcmp(var, "gl_BackMaterial") == 0) {
111 tokens[0] = STATE_MATERIAL;
112 if (strcmp(var, "gl_FrontMaterial") == 0)
113 tokens[1] = 0;
114 else
115 tokens[1] = 1;
116 if (strcmp(field, "emission") == 0) {
117 tokens[2] = STATE_EMISSION;
118 }
119 else if (strcmp(field, "ambient") == 0) {
120 tokens[2] = STATE_AMBIENT;
121 }
122 else if (strcmp(field, "diffuse") == 0) {
123 tokens[2] = STATE_DIFFUSE;
124 }
125 else if (strcmp(field, "specular") == 0) {
126 tokens[2] = STATE_SPECULAR;
127 }
128 else if (strcmp(field, "shininess") == 0) {
129 tokens[2] = STATE_SHININESS;
130 *swizzleOut = SWIZZLE_XXXX;
131 }
132 else {
133 return -1;
134 }
135 }
136 else if (strcmp(var, "gl_LightSource") == 0) {
137 tokens[0] = STATE_LIGHT;
138 tokens[1] = index1;
139 if (strcmp(field, "ambient") == 0) {
140 tokens[2] = STATE_AMBIENT;
141 }
142 else if (strcmp(field, "diffuse") == 0) {
143 tokens[2] = STATE_DIFFUSE;
144 }
145 else if (strcmp(field, "specular") == 0) {
146 tokens[2] = STATE_SPECULAR;
147 }
148 else if (strcmp(field, "position") == 0) {
149 tokens[2] = STATE_POSITION;
150 }
151 else if (strcmp(field, "halfVector") == 0) {
152 tokens[2] = STATE_HALF_VECTOR;
153 }
154 else if (strcmp(field, "spotDirection") == 0) {
155 tokens[2] = STATE_SPOT_DIRECTION;
156 }
157 else if (strcmp(field, "spotCosCutoff") == 0) {
158 tokens[2] = STATE_SPOT_DIRECTION;
159 *swizzleOut = SWIZZLE_WWWW;
160 }
161 else if (strcmp(field, "spotCutoff") == 0) {
162 tokens[2] = STATE_SPOT_CUTOFF;
163 *swizzleOut = SWIZZLE_XXXX;
164 }
165 else if (strcmp(field, "spotExponent") == 0) {
166 tokens[2] = STATE_ATTENUATION;
167 *swizzleOut = SWIZZLE_WWWW;
168 }
169 else if (strcmp(field, "constantAttenuation") == 0) {
170 tokens[2] = STATE_ATTENUATION;
171 *swizzleOut = SWIZZLE_XXXX;
172 }
173 else if (strcmp(field, "linearAttenuation") == 0) {
174 tokens[2] = STATE_ATTENUATION;
175 *swizzleOut = SWIZZLE_YYYY;
176 }
177 else if (strcmp(field, "quadraticAttenuation") == 0) {
178 tokens[2] = STATE_ATTENUATION;
179 *swizzleOut = SWIZZLE_ZZZZ;
180 }
181 else {
182 return -1;
183 }
184 }
185 else if (strcmp(var, "gl_LightModel") == 0) {
186 if (strcmp(field, "ambient") == 0) {
187 tokens[0] = STATE_LIGHTMODEL_AMBIENT;
188 }
189 else {
190 return -1;
191 }
192 }
193 else if (strcmp(var, "gl_FrontLightModelProduct") == 0) {
194 if (strcmp(field, "ambient") == 0) {
195 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
196 tokens[1] = 0;
197 }
198 else {
199 return -1;
200 }
201 }
202 else if (strcmp(var, "gl_BackLightModelProduct") == 0) {
203 if (strcmp(field, "ambient") == 0) {
204 tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
205 tokens[1] = 1;
206 }
207 else {
208 return -1;
209 }
210 }
211 else if (strcmp(var, "gl_FrontLightProduct") == 0 ||
212 strcmp(var, "gl_BackLightProduct") == 0) {
213 tokens[0] = STATE_LIGHTPROD;
214 tokens[1] = index1; /* light number */
215 if (strcmp(var, "gl_FrontLightProduct") == 0) {
216 tokens[2] = 0; /* front */
217 }
218 else {
219 tokens[2] = 1; /* back */
220 }
221 if (strcmp(field, "ambient") == 0) {
222 tokens[3] = STATE_AMBIENT;
223 }
224 else if (strcmp(field, "diffuset") == 0) {
225 tokens[3] = STATE_DIFFUSE;
226 }
227 else if (strcmp(field, "specular") == 0) {
228 tokens[3] = STATE_SPECULAR;
229 }
230 else {
231 return -1;
232 }
233 }
234 else if (strcmp(var, "gl_TextureEnvColor") == 0) {
235 tokens[0] = STATE_TEXENV_COLOR;
236 tokens[1] = index1;
237 }
238 else if (strcmp(var, "gl_EyePlaneS") == 0) {
239 tokens[0] = STATE_TEXGEN;
240 tokens[1] = index1; /* tex unit */
241 tokens[2] = STATE_TEXGEN_EYE_S;
242 }
243 else if (strcmp(var, "gl_EyePlaneT") == 0) {
244 tokens[0] = STATE_TEXGEN;
245 tokens[1] = index1; /* tex unit */
246 tokens[2] = STATE_TEXGEN_EYE_T;
247 }
248 else if (strcmp(var, "gl_EyePlaneR") == 0) {
249 tokens[0] = STATE_TEXGEN;
250 tokens[1] = index1; /* tex unit */
251 tokens[2] = STATE_TEXGEN_EYE_R;
252 }
253 else if (strcmp(var, "gl_EyePlaneQ") == 0) {
254 tokens[0] = STATE_TEXGEN;
255 tokens[1] = index1; /* tex unit */
256 tokens[2] = STATE_TEXGEN_EYE_Q;
257 }
258 else if (strcmp(var, "gl_ObjectPlaneS") == 0) {
259 tokens[0] = STATE_TEXGEN;
260 tokens[1] = index1; /* tex unit */
261 tokens[2] = STATE_TEXGEN_OBJECT_S;
262 }
263 else if (strcmp(var, "gl_ObjectPlaneT") == 0) {
264 tokens[0] = STATE_TEXGEN;
265 tokens[1] = index1; /* tex unit */
266 tokens[2] = STATE_TEXGEN_OBJECT_T;
267 }
268 else if (strcmp(var, "gl_ObjectPlaneR") == 0) {
269 tokens[0] = STATE_TEXGEN;
270 tokens[1] = index1; /* tex unit */
271 tokens[2] = STATE_TEXGEN_OBJECT_R;
272 }
273 else if (strcmp(var, "gl_ObjectPlaneQ") == 0) {
274 tokens[0] = STATE_TEXGEN;
275 tokens[1] = index1; /* tex unit */
276 tokens[2] = STATE_TEXGEN_OBJECT_Q;
277 }
278 else if (strcmp(var, "gl_Fog") == 0) {
279 if (strcmp(field, "color") == 0) {
280 tokens[0] = STATE_FOG_COLOR;
281 }
282 else if (strcmp(field, "density") == 0) {
283 tokens[0] = STATE_FOG_PARAMS;
284 *swizzleOut = SWIZZLE_XXXX;
285 }
286 else if (strcmp(field, "start") == 0) {
287 tokens[0] = STATE_FOG_PARAMS;
288 *swizzleOut = SWIZZLE_YYYY;
289 }
290 else if (strcmp(field, "end") == 0) {
291 tokens[0] = STATE_FOG_PARAMS;
292 *swizzleOut = SWIZZLE_ZZZZ;
293 }
294 else if (strcmp(field, "scale") == 0) {
295 tokens[0] = STATE_FOG_PARAMS;
296 *swizzleOut = SWIZZLE_WWWW;
297 }
298 else {
299 return -1;
300 }
301 }
302 else {
303 return -1;
304 }
305
306 if (isMatrix) {
307 /* load all four columns of matrix */
308 GLint pos[4];
309 GLuint j;
310 for (j = 0; j < 4; j++) {
311 tokens[2] = tokens[3] = j; /* jth row of matrix */
312 pos[j] = _mesa_add_state_reference(paramList, tokens);
313 assert(pos[j] >= 0);
314 ASSERT(pos[j] >= 0);
315 }
316 return pos[0] + index1;
317 }
318 else {
319 /* allocate a single register */
320 GLint pos = _mesa_add_state_reference(paramList, tokens);
321 ASSERT(pos >= 0);
322 return pos;
323 }
324 }
325
326
327 /**
328 * Allocate storage for a pre-defined uniform (a GL state variable).
329 * As a memory-saving optimization, we try to only allocate storage for
330 * state vars that are actually used.
331 * For example, the "gl_LightSource" uniform is huge. If we only use
332 * a handful of gl_LightSource fields, we don't want to allocate storage
333 * for all of gl_LightSource.
334 *
335 * Currently, all pre-defined uniforms are in one of these forms:
336 * var
337 * var[i]
338 * var.field
339 * var[i].field
340 * var[i][j]
341 */
342 GLint
343 _slang_alloc_statevar(slang_ir_node *n,
344 struct gl_program_parameter_list *paramList)
345 {
346 const char *field = NULL, *var;
347 GLint index1 = -1, index2 = -1, pos;
348 GLuint swizzle;
349
350 if (n->Opcode == IR_FIELD) {
351 field = n->Field;
352 n = n->Children[0];
353 }
354
355 if (n->Opcode == IR_ELEMENT) {
356 /* XXX can only handle constant indexes for now */
357 assert(n->Children[1]->Opcode == IR_FLOAT);
358 index1 = (GLint) n->Children[1]->Value[0];
359 n = n->Children[0];
360 }
361
362 if (n->Opcode == IR_ELEMENT) {
363 /* XXX can only handle constant indexes for now */
364 assert(n->Children[1]->Opcode == IR_FLOAT);
365 index2 = (GLint) n->Children[1]->Value[0];
366 n = n->Children[0];
367 }
368
369 assert(n->Opcode == IR_VAR);
370 var = (char *) n->Var->a_name;
371
372 pos = lookup_statevar(var, index1, index2, field, &swizzle, paramList);
373 assert(pos >= 0);
374 if (pos >= 0) {
375 n->Store->Index = pos;
376 n->Store->Swizzle = swizzle;
377 }
378 return pos;
379 }
380