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
)
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...
59 gl_state_index matrix
;
60 gl_state_index modifier
;
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
},
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
},
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
},
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
},
82 /* XXX verify these!!! */
83 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
84 { "__NormalMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
88 gl_state_index tokens
[STATE_LENGTH
];
90 GLboolean isMatrix
= GL_FALSE
;
92 for (i
= 0; i
< STATE_LENGTH
; i
++) {
95 *swizzleOut
= SWIZZLE_NOOP
;
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
;
109 if (tokens
[0] == STATE_TEXTURE_MATRIX
) {
112 index1
= 0; /* prevent extra addition at end of function */
116 else if (strcmp(var
, "gl_DepthRange") == 0) {
117 tokens
[0] = STATE_DEPTH_RANGE
;
118 if (strcmp(field
, "near") == 0) {
119 *swizzleOut
= SWIZZLE_XXXX
;
121 else if (strcmp(field
, "far") == 0) {
122 *swizzleOut
= SWIZZLE_YYYY
;
124 else if (strcmp(field
, "diff") == 0) {
125 *swizzleOut
= SWIZZLE_ZZZZ
;
131 else if (strcmp(var
, "gl_ClipPlane") == 0) {
132 tokens
[0] = STATE_CLIPPLANE
;
135 else if (strcmp(var
, "gl_Point") == 0) {
136 if (strcmp(field
, "size") == 0) {
137 tokens
[0] = STATE_POINT_SIZE
;
138 *swizzleOut
= SWIZZLE_XXXX
;
140 else if (strcmp(field
, "sizeMin") == 0) {
141 tokens
[0] = STATE_POINT_SIZE
;
142 *swizzleOut
= SWIZZLE_YYYY
;
144 else if (strcmp(field
, "sizeMax") == 0) {
145 tokens
[0] = STATE_POINT_SIZE
;
146 *swizzleOut
= SWIZZLE_ZZZZ
;
148 else if (strcmp(field
, "fadeThresholdSize") == 0) {
149 tokens
[0] = STATE_POINT_SIZE
;
150 *swizzleOut
= SWIZZLE_WWWW
;
152 else if (strcmp(field
, "distanceConstantAttenuation") == 0) {
153 tokens
[0] = STATE_POINT_ATTENUATION
;
154 *swizzleOut
= SWIZZLE_XXXX
;
156 else if (strcmp(field
, "distanceLinearAttenuation") == 0) {
157 tokens
[0] = STATE_POINT_ATTENUATION
;
158 *swizzleOut
= SWIZZLE_YYYY
;
160 else if (strcmp(field
, "distanceQuadraticAttenuation") == 0) {
161 tokens
[0] = STATE_POINT_ATTENUATION
;
162 *swizzleOut
= SWIZZLE_ZZZZ
;
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)
175 if (strcmp(field
, "emission") == 0) {
176 tokens
[2] = STATE_EMISSION
;
178 else if (strcmp(field
, "ambient") == 0) {
179 tokens
[2] = STATE_AMBIENT
;
181 else if (strcmp(field
, "diffuse") == 0) {
182 tokens
[2] = STATE_DIFFUSE
;
184 else if (strcmp(field
, "specular") == 0) {
185 tokens
[2] = STATE_SPECULAR
;
187 else if (strcmp(field
, "shininess") == 0) {
188 tokens
[2] = STATE_SHININESS
;
189 *swizzleOut
= SWIZZLE_XXXX
;
195 else if (strcmp(var
, "gl_LightSource") == 0) {
196 tokens
[0] = STATE_LIGHT
;
198 if (strcmp(field
, "ambient") == 0) {
199 tokens
[2] = STATE_AMBIENT
;
201 else if (strcmp(field
, "diffuse") == 0) {
202 tokens
[2] = STATE_DIFFUSE
;
204 else if (strcmp(field
, "specular") == 0) {
205 tokens
[2] = STATE_SPECULAR
;
207 else if (strcmp(field
, "position") == 0) {
208 tokens
[2] = STATE_POSITION
;
210 else if (strcmp(field
, "halfVector") == 0) {
211 tokens
[2] = STATE_HALF_VECTOR
;
213 else if (strcmp(field
, "spotDirection") == 0) {
214 tokens
[2] = STATE_SPOT_DIRECTION
;
216 else if (strcmp(field
, "spotCosCutoff") == 0) {
217 tokens
[2] = STATE_SPOT_DIRECTION
;
218 *swizzleOut
= SWIZZLE_WWWW
;
220 else if (strcmp(field
, "spotCutoff") == 0) {
221 tokens
[2] = STATE_SPOT_CUTOFF
;
222 *swizzleOut
= SWIZZLE_XXXX
;
224 else if (strcmp(field
, "spotExponent") == 0) {
225 tokens
[2] = STATE_ATTENUATION
;
226 *swizzleOut
= SWIZZLE_WWWW
;
228 else if (strcmp(field
, "constantAttenuation") == 0) {
229 tokens
[2] = STATE_ATTENUATION
;
230 *swizzleOut
= SWIZZLE_XXXX
;
232 else if (strcmp(field
, "linearAttenuation") == 0) {
233 tokens
[2] = STATE_ATTENUATION
;
234 *swizzleOut
= SWIZZLE_YYYY
;
236 else if (strcmp(field
, "quadraticAttenuation") == 0) {
237 tokens
[2] = STATE_ATTENUATION
;
238 *swizzleOut
= SWIZZLE_ZZZZ
;
244 else if (strcmp(var
, "gl_LightModel") == 0) {
245 if (strcmp(field
, "ambient") == 0) {
246 tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
252 else if (strcmp(var
, "gl_FrontLightModelProduct") == 0) {
253 if (strcmp(field
, "ambient") == 0) {
254 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
261 else if (strcmp(var
, "gl_BackLightModelProduct") == 0) {
262 if (strcmp(field
, "ambient") == 0) {
263 tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
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 */
278 tokens
[2] = 1; /* back */
280 if (strcmp(field
, "ambient") == 0) {
281 tokens
[3] = STATE_AMBIENT
;
283 else if (strcmp(field
, "diffuse") == 0) {
284 tokens
[3] = STATE_DIFFUSE
;
286 else if (strcmp(field
, "specular") == 0) {
287 tokens
[3] = STATE_SPECULAR
;
293 else if (strcmp(var
, "gl_TextureEnvColor") == 0) {
294 tokens
[0] = STATE_TEXENV_COLOR
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
337 else if (strcmp(var
, "gl_Fog") == 0) {
338 if (strcmp(field
, "color") == 0) {
339 tokens
[0] = STATE_FOG_COLOR
;
341 else if (strcmp(field
, "density") == 0) {
342 tokens
[0] = STATE_FOG_PARAMS
;
343 *swizzleOut
= SWIZZLE_XXXX
;
345 else if (strcmp(field
, "start") == 0) {
346 tokens
[0] = STATE_FOG_PARAMS
;
347 *swizzleOut
= SWIZZLE_YYYY
;
349 else if (strcmp(field
, "end") == 0) {
350 tokens
[0] = STATE_FOG_PARAMS
;
351 *swizzleOut
= SWIZZLE_ZZZZ
;
353 else if (strcmp(field
, "scale") == 0) {
354 tokens
[0] = STATE_FOG_PARAMS
;
355 *swizzleOut
= SWIZZLE_WWWW
;
366 /* load all four columns of matrix */
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
);
375 return pos
[0] + index1
;
378 /* allocate a single register */
379 GLint pos
= _mesa_add_state_reference(paramList
, tokens
);
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.
394 * Currently, all pre-defined uniforms are in one of these forms:
402 _slang_alloc_statevar(slang_ir_node
*n
,
403 struct gl_program_parameter_list
*paramList
)
405 slang_ir_node
*n0
= n
;
406 const char *field
= NULL
, *var
;
407 GLint index1
= -1, index2
= -1, pos
;
410 if (n
->Opcode
== IR_FIELD
) {
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];
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];
429 assert(n
->Opcode
== IR_VAR
);
430 var
= (char *) n
->Var
->a_name
;
432 pos
= lookup_statevar(var
, index1
, index2
, field
, &swizzle
, paramList
);
435 n0
->Store
->Index
= pos
;
436 n0
->Store
->Swizzle
= swizzle
;