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
;
555 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
556 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
561 case STATE_FB_WPOS_Y_TRANSFORM
:
562 /* A driver may negate this conditional by using ZW swizzle
563 * instead of XY (based on e.g. some other state). */
564 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
565 /* Identity (XY) followed by flipping Y upside down (ZW). */
569 value
[3] = (GLfloat
) ctx
->DrawBuffer
->Height
;
571 /* Flipping Y upside down (XY) followed by identity (ZW). */
573 value
[1] = (GLfloat
) ctx
->DrawBuffer
->Height
;
579 case STATE_ROT_MATRIX_0
:
581 const int unit
= (int) state
[2];
582 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
583 value
[0] = rotMat22
[0];
584 value
[1] = rotMat22
[2];
590 case STATE_ROT_MATRIX_1
:
592 const int unit
= (int) state
[2];
593 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
594 value
[0] = rotMat22
[1];
595 value
[1] = rotMat22
[3];
601 /* XXX: make sure new tokens added here are also handled in the
602 * _mesa_program_state_flags() switch, below.
605 /* Unknown state indexes are silently ignored here.
606 * Drivers may do something special.
613 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
620 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
621 * indicate that the given context state may have changed.
622 * The bitmask is used during validation to determine if we need to update
623 * vertex/fragment program parameters (like "state.material.color") when
624 * some GL state has changed.
627 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
631 case STATE_LIGHTPROD
:
632 case STATE_LIGHTMODEL_SCENECOLOR
:
633 /* these can be effected by glColor when colormaterial mode is used */
634 return _NEW_LIGHT
| _NEW_CURRENT_ATTRIB
;
637 case STATE_LIGHTMODEL_AMBIENT
:
642 case STATE_TEXENV_COLOR
:
643 return _NEW_TEXTURE
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
645 case STATE_FOG_COLOR
:
646 return _NEW_FOG
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
647 case STATE_FOG_PARAMS
:
650 case STATE_CLIPPLANE
:
651 return _NEW_TRANSFORM
;
653 case STATE_POINT_SIZE
:
654 case STATE_POINT_ATTENUATION
:
657 case STATE_MODELVIEW_MATRIX
:
658 return _NEW_MODELVIEW
;
659 case STATE_PROJECTION_MATRIX
:
660 return _NEW_PROJECTION
;
661 case STATE_MVP_MATRIX
:
662 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
663 case STATE_TEXTURE_MATRIX
:
664 return _NEW_TEXTURE_MATRIX
;
665 case STATE_PROGRAM_MATRIX
:
666 return _NEW_TRACK_MATRIX
;
668 case STATE_DEPTH_RANGE
:
669 return _NEW_VIEWPORT
;
671 case STATE_FRAGMENT_PROGRAM
:
672 case STATE_VERTEX_PROGRAM
:
675 case STATE_NORMAL_SCALE
:
676 return _NEW_MODELVIEW
;
680 case STATE_CURRENT_ATTRIB
:
681 return _NEW_CURRENT_ATTRIB
;
682 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
683 return _NEW_CURRENT_ATTRIB
| _NEW_LIGHT
| _NEW_BUFFERS
;
685 case STATE_NORMAL_SCALE
:
686 return _NEW_MODELVIEW
;
688 case STATE_TEXRECT_SCALE
:
689 case STATE_ROT_MATRIX_0
:
690 case STATE_ROT_MATRIX_1
:
692 case STATE_FOG_PARAMS_OPTIMIZED
:
694 case STATE_POINT_SIZE_CLAMPED
:
695 return _NEW_POINT
| _NEW_MULTISAMPLE
;
696 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
697 case STATE_LIGHT_POSITION
:
698 case STATE_LIGHT_POSITION_NORMALIZED
:
699 case STATE_LIGHT_HALF_VECTOR
:
707 case STATE_FB_WPOS_Y_TRANSFORM
:
711 /* unknown state indexes are silently ignored and
712 * no flag set, since it is handled by the driver.
718 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
725 append(char *dst
, const char *src
)
736 * Convert token 'k' to a string, append it onto 'dst' string.
739 append_token(char *dst
, gl_state_index k
)
743 append(dst
, "material");
746 append(dst
, "light");
748 case STATE_LIGHTMODEL_AMBIENT
:
749 append(dst
, "lightmodel.ambient");
751 case STATE_LIGHTMODEL_SCENECOLOR
:
753 case STATE_LIGHTPROD
:
754 append(dst
, "lightprod");
757 append(dst
, "texgen");
759 case STATE_FOG_COLOR
:
760 append(dst
, "fog.color");
762 case STATE_FOG_PARAMS
:
763 append(dst
, "fog.params");
765 case STATE_CLIPPLANE
:
768 case STATE_POINT_SIZE
:
769 append(dst
, "point.size");
771 case STATE_POINT_ATTENUATION
:
772 append(dst
, "point.attenuation");
774 case STATE_MODELVIEW_MATRIX
:
775 append(dst
, "matrix.modelview");
777 case STATE_PROJECTION_MATRIX
:
778 append(dst
, "matrix.projection");
780 case STATE_MVP_MATRIX
:
781 append(dst
, "matrix.mvp");
783 case STATE_TEXTURE_MATRIX
:
784 append(dst
, "matrix.texture");
786 case STATE_PROGRAM_MATRIX
:
787 append(dst
, "matrix.program");
789 case STATE_MATRIX_INVERSE
:
790 append(dst
, ".inverse");
792 case STATE_MATRIX_TRANSPOSE
:
793 append(dst
, ".transpose");
795 case STATE_MATRIX_INVTRANS
:
796 append(dst
, ".invtrans");
799 append(dst
, ".ambient");
802 append(dst
, ".diffuse");
805 append(dst
, ".specular");
808 append(dst
, ".emission");
810 case STATE_SHININESS
:
811 append(dst
, "lshininess");
813 case STATE_HALF_VECTOR
:
814 append(dst
, ".half");
817 append(dst
, ".position");
819 case STATE_ATTENUATION
:
820 append(dst
, ".attenuation");
822 case STATE_SPOT_DIRECTION
:
823 append(dst
, ".spot.direction");
825 case STATE_SPOT_CUTOFF
:
826 append(dst
, ".spot.cutoff");
828 case STATE_TEXGEN_EYE_S
:
829 append(dst
, ".eye.s");
831 case STATE_TEXGEN_EYE_T
:
832 append(dst
, ".eye.t");
834 case STATE_TEXGEN_EYE_R
:
835 append(dst
, ".eye.r");
837 case STATE_TEXGEN_EYE_Q
:
838 append(dst
, ".eye.q");
840 case STATE_TEXGEN_OBJECT_S
:
841 append(dst
, ".object.s");
843 case STATE_TEXGEN_OBJECT_T
:
844 append(dst
, ".object.t");
846 case STATE_TEXGEN_OBJECT_R
:
847 append(dst
, ".object.r");
849 case STATE_TEXGEN_OBJECT_Q
:
850 append(dst
, ".object.q");
852 case STATE_TEXENV_COLOR
:
853 append(dst
, "texenv");
855 case STATE_DEPTH_RANGE
:
856 append(dst
, "depth.range");
858 case STATE_VERTEX_PROGRAM
:
859 case STATE_FRAGMENT_PROGRAM
:
865 append(dst
, "local");
867 /* BEGIN internal state vars */
869 append(dst
, ".internal.");
871 case STATE_CURRENT_ATTRIB
:
872 append(dst
, "current");
874 case STATE_NORMAL_SCALE
:
875 append(dst
, "normalScale");
877 case STATE_TEXRECT_SCALE
:
878 append(dst
, "texrectScale");
880 case STATE_FOG_PARAMS_OPTIMIZED
:
881 append(dst
, "fogParamsOptimized");
883 case STATE_POINT_SIZE_CLAMPED
:
884 append(dst
, "pointSizeClamped");
886 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
887 append(dst
, "lightSpotDirNormalized");
889 case STATE_LIGHT_POSITION
:
890 append(dst
, "lightPosition");
892 case STATE_LIGHT_POSITION_NORMALIZED
:
893 append(dst
, "light.position.normalized");
895 case STATE_LIGHT_HALF_VECTOR
:
896 append(dst
, "lightHalfVector");
899 append(dst
, "PTscale");
902 append(dst
, "PTbias");
905 append(dst
, "FbSize");
907 case STATE_FB_WPOS_Y_TRANSFORM
:
908 append(dst
, "FbWposYTransform");
910 case STATE_ROT_MATRIX_0
:
911 append(dst
, "rotMatrixRow0");
913 case STATE_ROT_MATRIX_1
:
914 append(dst
, "rotMatrixRow1");
917 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
918 append(dst
, "driverState");
923 append_face(char *dst
, GLint face
)
926 append(dst
, "front.");
928 append(dst
, "back.");
932 append_index(char *dst
, GLint index
)
935 sprintf(s
, "[%d]", index
);
940 * Make a string from the given state vector.
941 * For example, return "state.matrix.texture[2].inverse".
942 * Use free() to deallocate the string.
945 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
950 append(str
, "state.");
951 append_token(str
, state
[0]);
955 append_face(str
, state
[1]);
956 append_token(str
, state
[2]);
959 append_index(str
, state
[1]); /* light number [i]. */
960 append_token(str
, state
[2]); /* coefficients */
962 case STATE_LIGHTMODEL_AMBIENT
:
963 append(str
, "lightmodel.ambient");
965 case STATE_LIGHTMODEL_SCENECOLOR
:
967 append(str
, "lightmodel.front.scenecolor");
970 append(str
, "lightmodel.back.scenecolor");
973 case STATE_LIGHTPROD
:
974 append_index(str
, state
[1]); /* light number [i]. */
975 append_face(str
, state
[2]);
976 append_token(str
, state
[3]);
979 append_index(str
, state
[1]); /* tex unit [i] */
980 append_token(str
, state
[2]); /* plane coef */
982 case STATE_TEXENV_COLOR
:
983 append_index(str
, state
[1]); /* tex unit [i] */
984 append(str
, "color");
986 case STATE_CLIPPLANE
:
987 append_index(str
, state
[1]); /* plane [i] */
988 append(str
, ".plane");
990 case STATE_MODELVIEW_MATRIX
:
991 case STATE_PROJECTION_MATRIX
:
992 case STATE_MVP_MATRIX
:
993 case STATE_TEXTURE_MATRIX
:
994 case STATE_PROGRAM_MATRIX
:
996 /* state[0] = modelview, projection, texture, etc. */
997 /* state[1] = which texture matrix or program matrix */
998 /* state[2] = first row to fetch */
999 /* state[3] = last row to fetch */
1000 /* state[4] = transpose, inverse or invtrans */
1001 const gl_state_index mat
= state
[0];
1002 const GLuint index
= (GLuint
) state
[1];
1003 const GLuint firstRow
= (GLuint
) state
[2];
1004 const GLuint lastRow
= (GLuint
) state
[3];
1005 const gl_state_index modifier
= state
[4];
1007 mat
== STATE_TEXTURE_MATRIX
||
1008 mat
== STATE_PROGRAM_MATRIX
)
1009 append_index(str
, index
);
1011 append_token(str
, modifier
);
1012 if (firstRow
== lastRow
)
1013 sprintf(tmp
, ".row[%d]", firstRow
);
1015 sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
1019 case STATE_POINT_SIZE
:
1021 case STATE_POINT_ATTENUATION
:
1023 case STATE_FOG_PARAMS
:
1025 case STATE_FOG_COLOR
:
1027 case STATE_DEPTH_RANGE
:
1029 case STATE_FRAGMENT_PROGRAM
:
1030 case STATE_VERTEX_PROGRAM
:
1031 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1032 /* state[2] = parameter index */
1033 append_token(str
, state
[1]);
1034 append_index(str
, state
[2]);
1036 case STATE_NORMAL_SCALE
:
1038 case STATE_INTERNAL
:
1039 append_token(str
, state
[1]);
1040 if (state
[1] == STATE_CURRENT_ATTRIB
)
1041 append_index(str
, state
[2]);
1044 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1048 return _mesa_strdup(str
);
1053 * Loop over all the parameters in a parameter list. If the parameter
1054 * is a GL state reference, look up the current value of that state
1055 * variable and put it into the parameter's Value[4] array.
1056 * Other parameter types never change or are explicitly set by the user
1057 * with glUniform() or glProgramParameter(), etc.
1058 * This would be called at glBegin time.
1061 _mesa_load_state_parameters(struct gl_context
*ctx
,
1062 struct gl_program_parameter_list
*paramList
)
1069 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1070 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1071 _mesa_fetch_state(ctx
,
1072 paramList
->Parameters
[i
].StateIndexes
,
1073 ¶mList
->ParameterValues
[i
][0].f
);
1080 * Copy the 16 elements of a matrix into four consecutive program
1081 * registers starting at 'pos'.
1084 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
1087 for (i
= 0; i
< 4; i
++) {
1088 registers
[pos
+ i
][0] = mat
[0 + i
];
1089 registers
[pos
+ i
][1] = mat
[4 + i
];
1090 registers
[pos
+ i
][2] = mat
[8 + i
];
1091 registers
[pos
+ i
][3] = mat
[12 + i
];
1097 * As above, but transpose the matrix.
1100 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
1101 const GLfloat mat
[16])
1103 memcpy(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
1108 * Load current vertex program's parameter registers with tracked
1109 * matrices (if NV program). This only needs to be done per
1110 * glBegin/glEnd, not per-vertex.
1113 _mesa_load_tracked_matrices(struct gl_context
*ctx
)
1117 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
1118 /* point 'mat' at source matrix */
1120 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
1121 mat
= ctx
->ModelviewMatrixStack
.Top
;
1123 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
1124 mat
= ctx
->ProjectionMatrixStack
.Top
;
1126 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
1127 GLuint unit
= MIN2(ctx
->Texture
.CurrentUnit
,
1128 Elements(ctx
->TextureMatrixStack
) - 1);
1129 mat
= ctx
->TextureMatrixStack
[unit
].Top
;
1131 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
1132 /* XXX verify the combined matrix is up to date */
1133 mat
= &ctx
->_ModelProjectMatrix
;
1135 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
1136 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
1137 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
1138 ASSERT(n
< Elements(ctx
->ProgramMatrixStack
));
1139 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
1142 /* no matrix is tracked, but we leave the register values as-is */
1143 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
1147 /* load the matrix values into sequential registers */
1148 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
1149 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1151 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
1152 _math_matrix_analyse(mat
); /* update the inverse */
1153 ASSERT(!_math_matrix_is_dirty(mat
));
1154 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
1156 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
1157 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1160 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
1161 == GL_INVERSE_TRANSPOSE_NV
);
1162 _math_matrix_analyse(mat
); /* update the inverse */
1163 ASSERT(!_math_matrix_is_dirty(mat
));
1164 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);