2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-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 prog_statevars.c
27 * Program state variable management.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/fbobject.h"
38 #include "prog_statevars.h"
39 #include "prog_parameter.h"
40 #include "main/samplerobj.h"
44 * Use the list of tokens in the state[] array to find global GL state
45 * and return it in <value>. Usually, four values are returned in <value>
46 * but matrix queries may return as many as 16 values.
47 * This function is used for ARB vertex/fragment programs.
48 * The program parser will produce the state[] values.
51 _mesa_fetch_state(struct gl_context
*ctx
, const gl_state_index state
[],
57 /* state[1] is either 0=front or 1=back side */
58 const GLuint face
= (GLuint
) state
[1];
59 const struct gl_material
*mat
= &ctx
->Light
.Material
;
60 ASSERT(face
== 0 || face
== 1);
61 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
62 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
63 /* XXX we could get rid of this switch entirely with a little
64 * work in arbprogparse.c's parse_state_single_item().
66 /* state[2] is the material attribute */
69 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
72 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
75 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
78 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
81 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
87 _mesa_problem(ctx
, "Invalid material state in fetch_state");
93 /* state[1] is the light number */
94 const GLuint ln
= (GLuint
) state
[1];
95 /* state[2] is the light attribute */
98 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
101 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
104 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
107 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
109 case STATE_ATTENUATION
:
110 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
111 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
112 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
113 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
115 case STATE_SPOT_DIRECTION
:
116 COPY_3V(value
, ctx
->Light
.Light
[ln
].SpotDirection
);
117 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
119 case STATE_SPOT_CUTOFF
:
120 value
[0] = ctx
->Light
.Light
[ln
].SpotCutoff
;
122 case STATE_HALF_VECTOR
:
124 static const GLfloat eye_z
[] = {0, 0, 1};
126 /* Compute infinite half angle vector:
127 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
128 * light.EyePosition.w should be 0 for infinite lights.
130 COPY_3V(p
, ctx
->Light
.Light
[ln
].EyePosition
);
132 ADD_3V(value
, p
, eye_z
);
133 NORMALIZE_3FV(value
);
138 _mesa_problem(ctx
, "Invalid light state in fetch_state");
142 case STATE_LIGHTMODEL_AMBIENT
:
143 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
145 case STATE_LIGHTMODEL_SCENECOLOR
:
149 for (i
= 0; i
< 3; i
++) {
150 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
151 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
152 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
154 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
159 for (i
= 0; i
< 3; i
++) {
160 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
161 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
162 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
164 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
167 case STATE_LIGHTPROD
:
169 const GLuint ln
= (GLuint
) state
[1];
170 const GLuint face
= (GLuint
) state
[2];
172 ASSERT(face
== 0 || face
== 1);
175 for (i
= 0; i
< 3; i
++) {
176 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
177 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
179 /* [3] = material alpha */
180 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
183 for (i
= 0; i
< 3; i
++) {
184 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
185 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
187 /* [3] = material alpha */
188 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
191 for (i
= 0; i
< 3; i
++) {
192 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
193 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
195 /* [3] = material alpha */
196 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
199 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
205 /* state[1] is the texture unit */
206 const GLuint unit
= (GLuint
) state
[1];
207 /* state[2] is the texgen attribute */
209 case STATE_TEXGEN_EYE_S
:
210 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.EyePlane
);
212 case STATE_TEXGEN_EYE_T
:
213 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.EyePlane
);
215 case STATE_TEXGEN_EYE_R
:
216 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.EyePlane
);
218 case STATE_TEXGEN_EYE_Q
:
219 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.EyePlane
);
221 case STATE_TEXGEN_OBJECT_S
:
222 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.ObjectPlane
);
224 case STATE_TEXGEN_OBJECT_T
:
225 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.ObjectPlane
);
227 case STATE_TEXGEN_OBJECT_R
:
228 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.ObjectPlane
);
230 case STATE_TEXGEN_OBJECT_Q
:
231 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.ObjectPlane
);
234 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
238 case STATE_TEXENV_COLOR
:
240 /* state[1] is the texture unit */
241 const GLuint unit
= (GLuint
) state
[1];
242 if(ctx
->Color
._ClampFragmentColor
)
243 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
245 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColorUnclamped
);
248 case STATE_FOG_COLOR
:
249 if(ctx
->Color
._ClampFragmentColor
)
250 COPY_4V(value
, ctx
->Fog
.Color
);
252 COPY_4V(value
, ctx
->Fog
.ColorUnclamped
);
254 case STATE_FOG_PARAMS
:
255 value
[0] = ctx
->Fog
.Density
;
256 value
[1] = ctx
->Fog
.Start
;
257 value
[2] = ctx
->Fog
.End
;
258 value
[3] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
259 ? 1.0f
: (GLfloat
)(1.0 / (ctx
->Fog
.End
- ctx
->Fog
.Start
));
261 case STATE_CLIPPLANE
:
263 const GLuint plane
= (GLuint
) state
[1];
264 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
267 case STATE_POINT_SIZE
:
268 value
[0] = ctx
->Point
.Size
;
269 value
[1] = ctx
->Point
.MinSize
;
270 value
[2] = ctx
->Point
.MaxSize
;
271 value
[3] = ctx
->Point
.Threshold
;
273 case STATE_POINT_ATTENUATION
:
274 value
[0] = ctx
->Point
.Params
[0];
275 value
[1] = ctx
->Point
.Params
[1];
276 value
[2] = ctx
->Point
.Params
[2];
279 case STATE_MODELVIEW_MATRIX
:
280 case STATE_PROJECTION_MATRIX
:
281 case STATE_MVP_MATRIX
:
282 case STATE_TEXTURE_MATRIX
:
283 case STATE_PROGRAM_MATRIX
:
285 /* state[0] = modelview, projection, texture, etc. */
286 /* state[1] = which texture matrix or program matrix */
287 /* state[2] = first row to fetch */
288 /* state[3] = last row to fetch */
289 /* state[4] = transpose, inverse or invtrans */
290 const GLmatrix
*matrix
;
291 const gl_state_index mat
= state
[0];
292 const GLuint index
= (GLuint
) state
[1];
293 const GLuint firstRow
= (GLuint
) state
[2];
294 const GLuint lastRow
= (GLuint
) state
[3];
295 const gl_state_index modifier
= state
[4];
298 ASSERT(firstRow
>= 0);
299 ASSERT(firstRow
< 4);
300 ASSERT(lastRow
>= 0);
302 if (mat
== STATE_MODELVIEW_MATRIX
) {
303 matrix
= ctx
->ModelviewMatrixStack
.Top
;
305 else if (mat
== STATE_PROJECTION_MATRIX
) {
306 matrix
= ctx
->ProjectionMatrixStack
.Top
;
308 else if (mat
== STATE_MVP_MATRIX
) {
309 matrix
= &ctx
->_ModelProjectMatrix
;
311 else if (mat
== STATE_TEXTURE_MATRIX
) {
312 ASSERT(index
< Elements(ctx
->TextureMatrixStack
));
313 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
315 else if (mat
== STATE_PROGRAM_MATRIX
) {
316 ASSERT(index
< Elements(ctx
->ProgramMatrixStack
));
317 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
320 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
323 if (modifier
== STATE_MATRIX_INVERSE
||
324 modifier
== STATE_MATRIX_INVTRANS
) {
325 /* Be sure inverse is up to date:
327 _math_matrix_analyse( (GLmatrix
*) matrix
);
333 if (modifier
== STATE_MATRIX_TRANSPOSE
||
334 modifier
== STATE_MATRIX_INVTRANS
) {
335 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
336 value
[i
++] = m
[row
* 4 + 0];
337 value
[i
++] = m
[row
* 4 + 1];
338 value
[i
++] = m
[row
* 4 + 2];
339 value
[i
++] = m
[row
* 4 + 3];
343 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
344 value
[i
++] = m
[row
+ 0];
345 value
[i
++] = m
[row
+ 4];
346 value
[i
++] = m
[row
+ 8];
347 value
[i
++] = m
[row
+ 12];
352 case STATE_DEPTH_RANGE
:
353 value
[0] = ctx
->Viewport
.Near
; /* near */
354 value
[1] = ctx
->Viewport
.Far
; /* far */
355 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
358 case STATE_FRAGMENT_PROGRAM
:
360 /* state[1] = {STATE_ENV, STATE_LOCAL} */
361 /* state[2] = parameter index */
362 const int idx
= (int) state
[2];
365 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
368 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
371 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
377 case STATE_VERTEX_PROGRAM
:
379 /* state[1] = {STATE_ENV, STATE_LOCAL} */
380 /* state[2] = parameter index */
381 const int idx
= (int) state
[2];
384 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
387 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
390 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
396 case STATE_NORMAL_SCALE
:
397 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
402 case STATE_CURRENT_ATTRIB
:
404 const GLuint idx
= (GLuint
) state
[2];
405 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
409 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
411 const GLuint idx
= (GLuint
) state
[2];
412 if(ctx
->Light
._ClampVertexColor
&&
413 (idx
== VERT_ATTRIB_COLOR0
||
414 idx
== VERT_ATTRIB_COLOR1
)) {
415 value
[0] = CLAMP(ctx
->Current
.Attrib
[idx
][0], 0.0f
, 1.0f
);
416 value
[1] = CLAMP(ctx
->Current
.Attrib
[idx
][1], 0.0f
, 1.0f
);
417 value
[2] = CLAMP(ctx
->Current
.Attrib
[idx
][2], 0.0f
, 1.0f
);
418 value
[3] = CLAMP(ctx
->Current
.Attrib
[idx
][3], 0.0f
, 1.0f
);
421 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
425 case STATE_NORMAL_SCALE
:
427 ctx
->_ModelViewInvScale
,
428 ctx
->_ModelViewInvScale
,
429 ctx
->_ModelViewInvScale
,
433 case STATE_TEXRECT_SCALE
:
434 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
435 * Used to convert unnormalized texcoords to normalized texcoords.
438 const int unit
= (int) state
[2];
439 const struct gl_texture_object
*texObj
440 = ctx
->Texture
.Unit
[unit
]._Current
;
442 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
444 (GLfloat
) (1.0 / texImage
->Width
),
445 (GLfloat
) (1.0 / texImage
->Height
),
451 case STATE_FOG_PARAMS_OPTIMIZED
:
452 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
453 * might be more expensive than EX2 on some hw, plus it needs
454 * another constant (e) anyway. Linear fog can now be done with a
456 * linear: fogcoord * -1/(end-start) + end/(end-start)
457 * exp: 2^-(density/ln(2) * fogcoord)
458 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
460 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
461 ? 1.0f
: (GLfloat
)(-1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
));
462 value
[1] = ctx
->Fog
.End
* -value
[0];
463 value
[2] = (GLfloat
)(ctx
->Fog
.Density
* M_LOG2E
); /* M_LOG2E == 1/ln(2) */
464 value
[3] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
);
467 case STATE_POINT_SIZE_CLAMPED
:
469 /* this includes implementation dependent limits, to avoid
470 * another potentially necessary clamp.
471 * Note: for sprites, point smooth (point AA) is ignored
472 * and we'll clamp to MinPointSizeAA and MaxPointSize, because we
473 * expect drivers will want to say their minimum for AA size is 0.0
474 * but for non-AA it's 1.0 (because normal points with size below 1.0
475 * need to get rounded up to 1.0, hence never disappear). GL does
476 * not specify max clamp size for sprites, other than it needs to be
477 * at least as large as max AA size, hence use non-AA size there.
481 if (ctx
->Point
.PointSprite
) {
482 minImplSize
= ctx
->Const
.MinPointSizeAA
;
483 maxImplSize
= ctx
->Const
.MaxPointSize
;
485 else if (ctx
->Point
.SmoothFlag
|| ctx
->Multisample
._Enabled
) {
486 minImplSize
= ctx
->Const
.MinPointSizeAA
;
487 maxImplSize
= ctx
->Const
.MaxPointSizeAA
;
490 minImplSize
= ctx
->Const
.MinPointSize
;
491 maxImplSize
= ctx
->Const
.MaxPointSize
;
493 value
[0] = ctx
->Point
.Size
;
494 value
[1] = ctx
->Point
.MinSize
>= minImplSize
? ctx
->Point
.MinSize
: minImplSize
;
495 value
[2] = ctx
->Point
.MaxSize
<= maxImplSize
? ctx
->Point
.MaxSize
: maxImplSize
;
496 value
[3] = ctx
->Point
.Threshold
;
499 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
501 /* here, state[2] is the light number */
502 /* pre-normalize spot dir */
503 const GLuint ln
= (GLuint
) state
[2];
504 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormSpotDirection
);
505 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
509 case STATE_LIGHT_POSITION
:
511 const GLuint ln
= (GLuint
) state
[2];
512 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
516 case STATE_LIGHT_POSITION_NORMALIZED
:
518 const GLuint ln
= (GLuint
) state
[2];
519 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
520 NORMALIZE_3FV( value
);
524 case STATE_LIGHT_HALF_VECTOR
:
526 const GLuint ln
= (GLuint
) state
[2];
528 /* Compute infinite half angle vector:
529 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
530 * light.EyePosition.w should be 0 for infinite lights.
532 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
534 ADD_3V(value
, p
, ctx
->_EyeZDir
);
535 NORMALIZE_3FV(value
);
541 value
[0] = ctx
->Pixel
.RedScale
;
542 value
[1] = ctx
->Pixel
.GreenScale
;
543 value
[2] = ctx
->Pixel
.BlueScale
;
544 value
[3] = ctx
->Pixel
.AlphaScale
;
548 value
[0] = ctx
->Pixel
.RedBias
;
549 value
[1] = ctx
->Pixel
.GreenBias
;
550 value
[2] = ctx
->Pixel
.BlueBias
;
551 value
[3] = ctx
->Pixel
.AlphaBias
;
554 case STATE_SHADOW_AMBIENT
:
556 const int unit
= (int) state
[2];
557 const struct gl_texture_object
*texObj
558 = ctx
->Texture
.Unit
[unit
]._Current
;
559 const struct gl_sampler_object
*samp
=
560 _mesa_get_samplerobj(ctx
, unit
);
565 value
[3] = samp
->CompareFailValue
;
571 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
572 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
577 case STATE_FB_WPOS_Y_TRANSFORM
:
578 /* A driver may negate this conditional by using ZW swizzle
579 * instead of XY (based on e.g. some other state). */
580 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
581 /* Identity (XY) followed by flipping Y upside down (ZW). */
585 value
[3] = (GLfloat
) ctx
->DrawBuffer
->Height
;
587 /* Flipping Y upside down (XY) followed by identity (ZW). */
589 value
[1] = (GLfloat
) ctx
->DrawBuffer
->Height
;
595 case STATE_ROT_MATRIX_0
:
597 const int unit
= (int) state
[2];
598 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
599 value
[0] = rotMat22
[0];
600 value
[1] = rotMat22
[2];
606 case STATE_ROT_MATRIX_1
:
608 const int unit
= (int) state
[2];
609 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
610 value
[0] = rotMat22
[1];
611 value
[1] = rotMat22
[3];
617 /* XXX: make sure new tokens added here are also handled in the
618 * _mesa_program_state_flags() switch, below.
621 /* Unknown state indexes are silently ignored here.
622 * Drivers may do something special.
629 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
636 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
637 * indicate that the given context state may have changed.
638 * The bitmask is used during validation to determine if we need to update
639 * vertex/fragment program parameters (like "state.material.color") when
640 * some GL state has changed.
643 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
647 case STATE_LIGHTPROD
:
648 case STATE_LIGHTMODEL_SCENECOLOR
:
649 /* these can be effected by glColor when colormaterial mode is used */
650 return _NEW_LIGHT
| _NEW_CURRENT_ATTRIB
;
653 case STATE_LIGHTMODEL_AMBIENT
:
658 case STATE_TEXENV_COLOR
:
659 return _NEW_TEXTURE
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
661 case STATE_FOG_COLOR
:
662 return _NEW_FOG
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
663 case STATE_FOG_PARAMS
:
666 case STATE_CLIPPLANE
:
667 return _NEW_TRANSFORM
;
669 case STATE_POINT_SIZE
:
670 case STATE_POINT_ATTENUATION
:
673 case STATE_MODELVIEW_MATRIX
:
674 return _NEW_MODELVIEW
;
675 case STATE_PROJECTION_MATRIX
:
676 return _NEW_PROJECTION
;
677 case STATE_MVP_MATRIX
:
678 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
679 case STATE_TEXTURE_MATRIX
:
680 return _NEW_TEXTURE_MATRIX
;
681 case STATE_PROGRAM_MATRIX
:
682 return _NEW_TRACK_MATRIX
;
684 case STATE_DEPTH_RANGE
:
685 return _NEW_VIEWPORT
;
687 case STATE_FRAGMENT_PROGRAM
:
688 case STATE_VERTEX_PROGRAM
:
691 case STATE_NORMAL_SCALE
:
692 return _NEW_MODELVIEW
;
696 case STATE_CURRENT_ATTRIB
:
697 return _NEW_CURRENT_ATTRIB
;
698 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
699 return _NEW_CURRENT_ATTRIB
| _NEW_LIGHT
| _NEW_BUFFERS
;
701 case STATE_NORMAL_SCALE
:
702 return _NEW_MODELVIEW
;
704 case STATE_TEXRECT_SCALE
:
705 case STATE_SHADOW_AMBIENT
:
706 case STATE_ROT_MATRIX_0
:
707 case STATE_ROT_MATRIX_1
:
709 case STATE_FOG_PARAMS_OPTIMIZED
:
711 case STATE_POINT_SIZE_CLAMPED
:
712 return _NEW_POINT
| _NEW_MULTISAMPLE
;
713 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
714 case STATE_LIGHT_POSITION
:
715 case STATE_LIGHT_POSITION_NORMALIZED
:
716 case STATE_LIGHT_HALF_VECTOR
:
724 case STATE_FB_WPOS_Y_TRANSFORM
:
728 /* unknown state indexes are silently ignored and
729 * no flag set, since it is handled by the driver.
735 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
742 append(char *dst
, const char *src
)
753 * Convert token 'k' to a string, append it onto 'dst' string.
756 append_token(char *dst
, gl_state_index k
)
760 append(dst
, "material");
763 append(dst
, "light");
765 case STATE_LIGHTMODEL_AMBIENT
:
766 append(dst
, "lightmodel.ambient");
768 case STATE_LIGHTMODEL_SCENECOLOR
:
770 case STATE_LIGHTPROD
:
771 append(dst
, "lightprod");
774 append(dst
, "texgen");
776 case STATE_FOG_COLOR
:
777 append(dst
, "fog.color");
779 case STATE_FOG_PARAMS
:
780 append(dst
, "fog.params");
782 case STATE_CLIPPLANE
:
785 case STATE_POINT_SIZE
:
786 append(dst
, "point.size");
788 case STATE_POINT_ATTENUATION
:
789 append(dst
, "point.attenuation");
791 case STATE_MODELVIEW_MATRIX
:
792 append(dst
, "matrix.modelview");
794 case STATE_PROJECTION_MATRIX
:
795 append(dst
, "matrix.projection");
797 case STATE_MVP_MATRIX
:
798 append(dst
, "matrix.mvp");
800 case STATE_TEXTURE_MATRIX
:
801 append(dst
, "matrix.texture");
803 case STATE_PROGRAM_MATRIX
:
804 append(dst
, "matrix.program");
806 case STATE_MATRIX_INVERSE
:
807 append(dst
, ".inverse");
809 case STATE_MATRIX_TRANSPOSE
:
810 append(dst
, ".transpose");
812 case STATE_MATRIX_INVTRANS
:
813 append(dst
, ".invtrans");
816 append(dst
, ".ambient");
819 append(dst
, ".diffuse");
822 append(dst
, ".specular");
825 append(dst
, ".emission");
827 case STATE_SHININESS
:
828 append(dst
, "lshininess");
830 case STATE_HALF_VECTOR
:
831 append(dst
, ".half");
834 append(dst
, ".position");
836 case STATE_ATTENUATION
:
837 append(dst
, ".attenuation");
839 case STATE_SPOT_DIRECTION
:
840 append(dst
, ".spot.direction");
842 case STATE_SPOT_CUTOFF
:
843 append(dst
, ".spot.cutoff");
845 case STATE_TEXGEN_EYE_S
:
846 append(dst
, ".eye.s");
848 case STATE_TEXGEN_EYE_T
:
849 append(dst
, ".eye.t");
851 case STATE_TEXGEN_EYE_R
:
852 append(dst
, ".eye.r");
854 case STATE_TEXGEN_EYE_Q
:
855 append(dst
, ".eye.q");
857 case STATE_TEXGEN_OBJECT_S
:
858 append(dst
, ".object.s");
860 case STATE_TEXGEN_OBJECT_T
:
861 append(dst
, ".object.t");
863 case STATE_TEXGEN_OBJECT_R
:
864 append(dst
, ".object.r");
866 case STATE_TEXGEN_OBJECT_Q
:
867 append(dst
, ".object.q");
869 case STATE_TEXENV_COLOR
:
870 append(dst
, "texenv");
872 case STATE_DEPTH_RANGE
:
873 append(dst
, "depth.range");
875 case STATE_VERTEX_PROGRAM
:
876 case STATE_FRAGMENT_PROGRAM
:
882 append(dst
, "local");
884 /* BEGIN internal state vars */
886 append(dst
, ".internal.");
888 case STATE_CURRENT_ATTRIB
:
889 append(dst
, "current");
891 case STATE_NORMAL_SCALE
:
892 append(dst
, "normalScale");
894 case STATE_TEXRECT_SCALE
:
895 append(dst
, "texrectScale");
897 case STATE_FOG_PARAMS_OPTIMIZED
:
898 append(dst
, "fogParamsOptimized");
900 case STATE_POINT_SIZE_CLAMPED
:
901 append(dst
, "pointSizeClamped");
903 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
904 append(dst
, "lightSpotDirNormalized");
906 case STATE_LIGHT_POSITION
:
907 append(dst
, "lightPosition");
909 case STATE_LIGHT_POSITION_NORMALIZED
:
910 append(dst
, "light.position.normalized");
912 case STATE_LIGHT_HALF_VECTOR
:
913 append(dst
, "lightHalfVector");
916 append(dst
, "PTscale");
919 append(dst
, "PTbias");
921 case STATE_SHADOW_AMBIENT
:
922 append(dst
, "CompareFailValue");
925 append(dst
, "FbSize");
927 case STATE_FB_WPOS_Y_TRANSFORM
:
928 append(dst
, "FbWposYTransform");
930 case STATE_ROT_MATRIX_0
:
931 append(dst
, "rotMatrixRow0");
933 case STATE_ROT_MATRIX_1
:
934 append(dst
, "rotMatrixRow1");
937 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
938 append(dst
, "driverState");
943 append_face(char *dst
, GLint face
)
946 append(dst
, "front.");
948 append(dst
, "back.");
952 append_index(char *dst
, GLint index
)
955 sprintf(s
, "[%d]", index
);
960 * Make a string from the given state vector.
961 * For example, return "state.matrix.texture[2].inverse".
962 * Use free() to deallocate the string.
965 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
970 append(str
, "state.");
971 append_token(str
, state
[0]);
975 append_face(str
, state
[1]);
976 append_token(str
, state
[2]);
979 append_index(str
, state
[1]); /* light number [i]. */
980 append_token(str
, state
[2]); /* coefficients */
982 case STATE_LIGHTMODEL_AMBIENT
:
983 append(str
, "lightmodel.ambient");
985 case STATE_LIGHTMODEL_SCENECOLOR
:
987 append(str
, "lightmodel.front.scenecolor");
990 append(str
, "lightmodel.back.scenecolor");
993 case STATE_LIGHTPROD
:
994 append_index(str
, state
[1]); /* light number [i]. */
995 append_face(str
, state
[2]);
996 append_token(str
, state
[3]);
999 append_index(str
, state
[1]); /* tex unit [i] */
1000 append_token(str
, state
[2]); /* plane coef */
1002 case STATE_TEXENV_COLOR
:
1003 append_index(str
, state
[1]); /* tex unit [i] */
1004 append(str
, "color");
1006 case STATE_CLIPPLANE
:
1007 append_index(str
, state
[1]); /* plane [i] */
1008 append(str
, ".plane");
1010 case STATE_MODELVIEW_MATRIX
:
1011 case STATE_PROJECTION_MATRIX
:
1012 case STATE_MVP_MATRIX
:
1013 case STATE_TEXTURE_MATRIX
:
1014 case STATE_PROGRAM_MATRIX
:
1016 /* state[0] = modelview, projection, texture, etc. */
1017 /* state[1] = which texture matrix or program matrix */
1018 /* state[2] = first row to fetch */
1019 /* state[3] = last row to fetch */
1020 /* state[4] = transpose, inverse or invtrans */
1021 const gl_state_index mat
= state
[0];
1022 const GLuint index
= (GLuint
) state
[1];
1023 const GLuint firstRow
= (GLuint
) state
[2];
1024 const GLuint lastRow
= (GLuint
) state
[3];
1025 const gl_state_index modifier
= state
[4];
1027 mat
== STATE_TEXTURE_MATRIX
||
1028 mat
== STATE_PROGRAM_MATRIX
)
1029 append_index(str
, index
);
1031 append_token(str
, modifier
);
1032 if (firstRow
== lastRow
)
1033 sprintf(tmp
, ".row[%d]", firstRow
);
1035 sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
1039 case STATE_POINT_SIZE
:
1041 case STATE_POINT_ATTENUATION
:
1043 case STATE_FOG_PARAMS
:
1045 case STATE_FOG_COLOR
:
1047 case STATE_DEPTH_RANGE
:
1049 case STATE_FRAGMENT_PROGRAM
:
1050 case STATE_VERTEX_PROGRAM
:
1051 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1052 /* state[2] = parameter index */
1053 append_token(str
, state
[1]);
1054 append_index(str
, state
[2]);
1056 case STATE_NORMAL_SCALE
:
1058 case STATE_INTERNAL
:
1059 append_token(str
, state
[1]);
1060 if (state
[1] == STATE_CURRENT_ATTRIB
)
1061 append_index(str
, state
[2]);
1064 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1068 return _mesa_strdup(str
);
1073 * Loop over all the parameters in a parameter list. If the parameter
1074 * is a GL state reference, look up the current value of that state
1075 * variable and put it into the parameter's Value[4] array.
1076 * Other parameter types never change or are explicitly set by the user
1077 * with glUniform() or glProgramParameter(), etc.
1078 * This would be called at glBegin time.
1081 _mesa_load_state_parameters(struct gl_context
*ctx
,
1082 struct gl_program_parameter_list
*paramList
)
1089 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1090 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1091 _mesa_fetch_state(ctx
,
1092 paramList
->Parameters
[i
].StateIndexes
,
1093 ¶mList
->ParameterValues
[i
][0].f
);
1100 * Copy the 16 elements of a matrix into four consecutive program
1101 * registers starting at 'pos'.
1104 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
1107 for (i
= 0; i
< 4; i
++) {
1108 registers
[pos
+ i
][0] = mat
[0 + i
];
1109 registers
[pos
+ i
][1] = mat
[4 + i
];
1110 registers
[pos
+ i
][2] = mat
[8 + i
];
1111 registers
[pos
+ i
][3] = mat
[12 + i
];
1117 * As above, but transpose the matrix.
1120 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
1121 const GLfloat mat
[16])
1123 memcpy(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
1128 * Load current vertex program's parameter registers with tracked
1129 * matrices (if NV program). This only needs to be done per
1130 * glBegin/glEnd, not per-vertex.
1133 _mesa_load_tracked_matrices(struct gl_context
*ctx
)
1137 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
1138 /* point 'mat' at source matrix */
1140 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
1141 mat
= ctx
->ModelviewMatrixStack
.Top
;
1143 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
1144 mat
= ctx
->ProjectionMatrixStack
.Top
;
1146 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
1147 GLuint unit
= MIN2(ctx
->Texture
.CurrentUnit
,
1148 Elements(ctx
->TextureMatrixStack
) - 1);
1149 mat
= ctx
->TextureMatrixStack
[unit
].Top
;
1151 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
1152 /* XXX verify the combined matrix is up to date */
1153 mat
= &ctx
->_ModelProjectMatrix
;
1155 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
1156 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
1157 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
1158 ASSERT(n
< Elements(ctx
->ProgramMatrixStack
));
1159 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
1162 /* no matrix is tracked, but we leave the register values as-is */
1163 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
1167 /* load the matrix values into sequential registers */
1168 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
1169 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1171 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
1172 _math_matrix_analyse(mat
); /* update the inverse */
1173 ASSERT(!_math_matrix_is_dirty(mat
));
1174 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
1176 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
1177 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1180 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
1181 == GL_INVERSE_TRANSPOSE_NV
);
1182 _math_matrix_analyse(mat
); /* update the inverse */
1183 ASSERT(!_math_matrix_is_dirty(mat
));
1184 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);