2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_builtin.c
27 * Resolve built-in uniform vars.
32 #include "slang_builtin.h"
36 #include "prog_instruction.h"
37 #include "prog_parameter.h"
38 #include "prog_statevars.h"
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
45 * Yes, this is kind of ugly, but it works.
48 lookup_statevar(const char *var
, GLint index1
, GLint index2
, const char *field
,
50 struct gl_program_parameter_list
*paramList
)
52 gl_state_index tokens
[STATE_LENGTH
];
54 GLboolean isMatrix
= GL_FALSE
;
56 for (i
= 0; i
< STATE_LENGTH
; i
++) {
59 *swizzleOut
= SWIZZLE_NOOP
;
61 if (strcmp(var
, "gl_ModelViewMatrix") == 0) {
62 tokens
[0] = STATE_MODELVIEW_MATRIX
;
65 else if (strcmp(var
, "gl_ModelProjectionMatrix") == 0) {
66 tokens
[0] = STATE_PROJECTION_MATRIX
;
69 else if (strcmp(var
, "gl_ModelViewProjectionMatrix") == 0) {
70 tokens
[0] = STATE_MVP_MATRIX
;
73 else if (strcmp(var
, "gl_NormalMatrix") == 0) {
74 tokens
[0] = STATE_MODELVIEW_MATRIX
;
77 else if (strcmp(var
, "gl_TextureMatrix") == 0) {
78 tokens
[0] = STATE_TEXTURE_MATRIX
;
83 else if (strcmp(var
, "gl_DepthRange") == 0) {
84 tokens
[0] = STATE_DEPTH_RANGE
;
85 if (strcmp(field
, "near") == 0) {
86 *swizzleOut
= SWIZZLE_XXXX
;
88 else if (strcmp(field
, "far") == 0) {
89 *swizzleOut
= SWIZZLE_YYYY
;
91 else if (strcmp(field
, "diff") == 0) {
92 *swizzleOut
= SWIZZLE_ZZZZ
;
98 else if (strcmp(var
, "gl_ClipPlane") == 0) {
99 tokens
[0] = STATE_CLIPPLANE
;
102 else if (strcmp(var
, "gl_Point") == 0) {
103 if (strcmp(field
, "size") == 0) {
104 tokens
[0] = STATE_POINT_SIZE
;
105 *swizzleOut
= SWIZZLE_XXXX
;
107 else if (strcmp(field
, "sizeMin") == 0) {
108 tokens
[0] = STATE_POINT_SIZE
;
109 *swizzleOut
= SWIZZLE_YYYY
;
111 else if (strcmp(field
, "sizeMax") == 0) {
112 tokens
[0] = STATE_POINT_SIZE
;
113 *swizzleOut
= SWIZZLE_ZZZZ
;
115 else if (strcmp(field
, "fadeThresholdSize") == 0) {
116 tokens
[0] = STATE_POINT_SIZE
;
117 *swizzleOut
= SWIZZLE_WWWW
;
119 else if (strcmp(field
, "distanceConstantAttenuation") == 0) {
120 tokens
[0] = STATE_POINT_ATTENUATION
;
121 *swizzleOut
= SWIZZLE_XXXX
;
123 else if (strcmp(field
, "distanceLinearAttenuation") == 0) {
124 tokens
[0] = STATE_POINT_ATTENUATION
;
125 *swizzleOut
= SWIZZLE_YYYY
;
127 else if (strcmp(field
, "distanceQuadraticAttenuation") == 0) {
128 tokens
[0] = STATE_POINT_ATTENUATION
;
129 *swizzleOut
= SWIZZLE_ZZZZ
;
135 else if (strcmp(var
, "gl_FrontMaterial") == 0 ||
136 strcmp(var
, "gl_BackMaterial") == 0) {
137 tokens
[0] = STATE_MATERIAL
;
138 if (strcmp(var
, "gl_FrontMaterial") == 0)
142 if (strcmp(field
, "emission") == 0) {
143 tokens
[2] = STATE_EMISSION
;
145 else if (strcmp(field
, "ambient") == 0) {
146 tokens
[2] = STATE_AMBIENT
;
148 else if (strcmp(field
, "diffuse") == 0) {
149 tokens
[2] = STATE_DIFFUSE
;
151 else if (strcmp(field
, "specular") == 0) {
152 tokens
[2] = STATE_SPECULAR
;
154 else if (strcmp(field
, "shininess") == 0) {
155 tokens
[2] = STATE_SHININESS
;
156 *swizzleOut
= SWIZZLE_XXXX
;
162 else if (strcmp(var
, "gl_LightSource") == 0) {
163 tokens
[0] = STATE_LIGHT
;
165 if (strcmp(field
, "ambient") == 0) {
166 tokens
[2] = STATE_AMBIENT
;
168 else if (strcmp(field
, "diffuse") == 0) {
169 tokens
[2] = STATE_DIFFUSE
;
171 else if (strcmp(field
, "specular") == 0) {
172 tokens
[2] = STATE_SPECULAR
;
174 else if (strcmp(field
, "position") == 0) {
175 tokens
[2] = STATE_POSITION
;
177 else if (strcmp(field
, "halfVector") == 0) {
178 tokens
[2] = STATE_HALF_VECTOR
;
180 else if (strcmp(field
, "spotDirection") == 0) {
181 tokens
[2] = STATE_SPOT_DIRECTION
;
183 else if (strcmp(field
, "spotCosCutoff") == 0) {
184 tokens
[2] = STATE_SPOT_DIRECTION
;
185 *swizzleOut
= SWIZZLE_WWWW
;
187 else if (strcmp(field
, "spotCutoff") == 0) {
188 tokens
[2] = STATE_SPOT_CUTOFF
;
189 *swizzleOut
= SWIZZLE_XXXX
;
191 else if (strcmp(field
, "spotExponent") == 0) {
192 tokens
[2] = STATE_ATTENUATION
;
193 *swizzleOut
= SWIZZLE_WWWW
;
195 else if (strcmp(field
, "constantAttenuation") == 0) {
196 tokens
[2] = STATE_ATTENUATION
;
197 *swizzleOut
= SWIZZLE_XXXX
;
199 else if (strcmp(field
, "linearAttenuation") == 0) {
200 tokens
[2] = STATE_ATTENUATION
;
201 *swizzleOut
= SWIZZLE_YYYY
;
203 else if (strcmp(field
, "quadraticAttenuation") == 0) {
204 tokens
[2] = STATE_ATTENUATION
;
205 *swizzleOut
= SWIZZLE_ZZZZ
;
211 else if (strcmp(var
, "gl_LightModel") == 0) {
212 if (strcmp(field
, "ambient") == 0) {
213 tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
219 else if (strcmp(var
, "gl_FrontLightModelProduct") == 0) {
220 if (strcmp(field
, "ambient") == 0) {
221 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
228 else if (strcmp(var
, "gl_BackLightModelProduct") == 0) {
229 if (strcmp(field
, "ambient") == 0) {
230 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
237 else if (strcmp(var
, "gl_FrontLightProduct") == 0 ||
238 strcmp(var
, "gl_BackLightProduct") == 0) {
239 tokens
[0] = STATE_LIGHTPROD
;
240 tokens
[1] = index1
; /* light number */
241 if (strcmp(var
, "gl_FrontLightProduct") == 0) {
242 tokens
[2] = 0; /* front */
245 tokens
[2] = 1; /* back */
247 if (strcmp(field
, "ambient") == 0) {
248 tokens
[3] = STATE_AMBIENT
;
250 else if (strcmp(field
, "diffuse") == 0) {
251 tokens
[3] = STATE_DIFFUSE
;
253 else if (strcmp(field
, "specular") == 0) {
254 tokens
[3] = STATE_SPECULAR
;
260 else if (strcmp(var
, "gl_TextureEnvColor") == 0) {
261 tokens
[0] = STATE_TEXENV_COLOR
;
264 else if (strcmp(var
, "gl_EyePlaneS") == 0) {
265 tokens
[0] = STATE_TEXGEN
;
266 tokens
[1] = index1
; /* tex unit */
267 tokens
[2] = STATE_TEXGEN_EYE_S
;
269 else if (strcmp(var
, "gl_EyePlaneT") == 0) {
270 tokens
[0] = STATE_TEXGEN
;
271 tokens
[1] = index1
; /* tex unit */
272 tokens
[2] = STATE_TEXGEN_EYE_T
;
274 else if (strcmp(var
, "gl_EyePlaneR") == 0) {
275 tokens
[0] = STATE_TEXGEN
;
276 tokens
[1] = index1
; /* tex unit */
277 tokens
[2] = STATE_TEXGEN_EYE_R
;
279 else if (strcmp(var
, "gl_EyePlaneQ") == 0) {
280 tokens
[0] = STATE_TEXGEN
;
281 tokens
[1] = index1
; /* tex unit */
282 tokens
[2] = STATE_TEXGEN_EYE_Q
;
284 else if (strcmp(var
, "gl_ObjectPlaneS") == 0) {
285 tokens
[0] = STATE_TEXGEN
;
286 tokens
[1] = index1
; /* tex unit */
287 tokens
[2] = STATE_TEXGEN_OBJECT_S
;
289 else if (strcmp(var
, "gl_ObjectPlaneT") == 0) {
290 tokens
[0] = STATE_TEXGEN
;
291 tokens
[1] = index1
; /* tex unit */
292 tokens
[2] = STATE_TEXGEN_OBJECT_T
;
294 else if (strcmp(var
, "gl_ObjectPlaneR") == 0) {
295 tokens
[0] = STATE_TEXGEN
;
296 tokens
[1] = index1
; /* tex unit */
297 tokens
[2] = STATE_TEXGEN_OBJECT_R
;
299 else if (strcmp(var
, "gl_ObjectPlaneQ") == 0) {
300 tokens
[0] = STATE_TEXGEN
;
301 tokens
[1] = index1
; /* tex unit */
302 tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
304 else if (strcmp(var
, "gl_Fog") == 0) {
305 if (strcmp(field
, "color") == 0) {
306 tokens
[0] = STATE_FOG_COLOR
;
308 else if (strcmp(field
, "density") == 0) {
309 tokens
[0] = STATE_FOG_PARAMS
;
310 *swizzleOut
= SWIZZLE_XXXX
;
312 else if (strcmp(field
, "start") == 0) {
313 tokens
[0] = STATE_FOG_PARAMS
;
314 *swizzleOut
= SWIZZLE_YYYY
;
316 else if (strcmp(field
, "end") == 0) {
317 tokens
[0] = STATE_FOG_PARAMS
;
318 *swizzleOut
= SWIZZLE_ZZZZ
;
320 else if (strcmp(field
, "scale") == 0) {
321 tokens
[0] = STATE_FOG_PARAMS
;
322 *swizzleOut
= SWIZZLE_WWWW
;
333 /* load all four columns of matrix */
336 for (j
= 0; j
< 4; j
++) {
337 tokens
[2] = tokens
[3] = j
; /* jth row of matrix */
338 pos
[j
] = _mesa_add_state_reference(paramList
, tokens
);
342 return pos
[0] + index1
;
345 /* allocate a single register */
346 GLint pos
= _mesa_add_state_reference(paramList
, tokens
);
354 * Allocate storage for a pre-defined uniform (a GL state variable).
355 * As a memory-saving optimization, we try to only allocate storage for
356 * state vars that are actually used.
357 * For example, the "gl_LightSource" uniform is huge. If we only use
358 * a handful of gl_LightSource fields, we don't want to allocate storage
359 * for all of gl_LightSource.
361 * Currently, all pre-defined uniforms are in one of these forms:
369 _slang_alloc_statevar(slang_ir_node
*n
,
370 struct gl_program_parameter_list
*paramList
)
372 slang_ir_node
*n0
= n
;
373 const char *field
= NULL
, *var
;
374 GLint index1
= -1, index2
= -1, pos
;
377 if (n
->Opcode
== IR_FIELD
) {
382 if (n
->Opcode
== IR_ELEMENT
) {
383 /* XXX can only handle constant indexes for now */
384 assert(n
->Children
[1]->Opcode
== IR_FLOAT
);
385 index1
= (GLint
) n
->Children
[1]->Value
[0];
389 if (n
->Opcode
== IR_ELEMENT
) {
390 /* XXX can only handle constant indexes for now */
391 assert(n
->Children
[1]->Opcode
== IR_FLOAT
);
392 index2
= (GLint
) n
->Children
[1]->Value
[0];
396 assert(n
->Opcode
== IR_VAR
);
397 var
= (char *) n
->Var
->a_name
;
399 pos
= lookup_statevar(var
, index1
, index2
, field
, &swizzle
, paramList
);
402 n0
->Store
->Index
= pos
;
403 n0
->Store
->Swizzle
= swizzle
;