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/hash.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "prog_statevars.h"
39 #include "prog_parameter.h"
43 * Use the list of tokens in the state[] array to find global GL state
44 * and return it in <value>. Usually, four values are returned in <value>
45 * but matrix queries may return as many as 16 values.
46 * This function is used for ARB vertex/fragment programs.
47 * The program parser will produce the state[] values.
50 _mesa_fetch_state(GLcontext
*ctx
, const gl_state_index state
[],
56 /* state[1] is either 0=front or 1=back side */
57 const GLuint face
= (GLuint
) state
[1];
58 const struct gl_material
*mat
= &ctx
->Light
.Material
;
59 ASSERT(face
== 0 || face
== 1);
60 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
61 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
62 /* XXX we could get rid of this switch entirely with a little
63 * work in arbprogparse.c's parse_state_single_item().
65 /* state[2] is the material attribute */
68 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
71 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
74 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
77 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
80 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
86 _mesa_problem(ctx
, "Invalid material state in fetch_state");
92 /* state[1] is the light number */
93 const GLuint ln
= (GLuint
) state
[1];
94 /* state[2] is the light attribute */
97 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
100 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
103 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
106 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
108 case STATE_ATTENUATION
:
109 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
110 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
111 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
112 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
114 case STATE_SPOT_DIRECTION
:
115 COPY_3V(value
, ctx
->Light
.Light
[ln
].SpotDirection
);
116 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
118 case STATE_SPOT_CUTOFF
:
119 value
[0] = ctx
->Light
.Light
[ln
].SpotCutoff
;
121 case STATE_HALF_VECTOR
:
123 static const GLfloat eye_z
[] = {0, 0, 1};
125 /* Compute infinite half angle vector:
126 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
127 * light.EyePosition.w should be 0 for infinite lights.
129 COPY_3V(p
, ctx
->Light
.Light
[ln
].EyePosition
);
131 ADD_3V(value
, p
, eye_z
);
132 NORMALIZE_3FV(value
);
137 _mesa_problem(ctx
, "Invalid light state in fetch_state");
141 case STATE_LIGHTMODEL_AMBIENT
:
142 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
144 case STATE_LIGHTMODEL_SCENECOLOR
:
148 for (i
= 0; i
< 3; i
++) {
149 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
150 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
151 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
153 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
158 for (i
= 0; i
< 3; i
++) {
159 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
160 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
161 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
163 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
166 case STATE_LIGHTPROD
:
168 const GLuint ln
= (GLuint
) state
[1];
169 const GLuint face
= (GLuint
) state
[2];
171 ASSERT(face
== 0 || face
== 1);
174 for (i
= 0; i
< 3; i
++) {
175 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
176 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
178 /* [3] = material alpha */
179 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
182 for (i
= 0; i
< 3; i
++) {
183 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
184 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
186 /* [3] = material alpha */
187 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
190 for (i
= 0; i
< 3; i
++) {
191 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
192 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
194 /* [3] = material alpha */
195 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
198 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
204 /* state[1] is the texture unit */
205 const GLuint unit
= (GLuint
) state
[1];
206 /* state[2] is the texgen attribute */
208 case STATE_TEXGEN_EYE_S
:
209 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.EyePlane
);
211 case STATE_TEXGEN_EYE_T
:
212 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.EyePlane
);
214 case STATE_TEXGEN_EYE_R
:
215 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.EyePlane
);
217 case STATE_TEXGEN_EYE_Q
:
218 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.EyePlane
);
220 case STATE_TEXGEN_OBJECT_S
:
221 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.ObjectPlane
);
223 case STATE_TEXGEN_OBJECT_T
:
224 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.ObjectPlane
);
226 case STATE_TEXGEN_OBJECT_R
:
227 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.ObjectPlane
);
229 case STATE_TEXGEN_OBJECT_Q
:
230 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.ObjectPlane
);
233 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
237 case STATE_TEXENV_COLOR
:
239 /* state[1] is the texture unit */
240 const GLuint unit
= (GLuint
) state
[1];
241 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
244 case STATE_FOG_COLOR
:
245 COPY_4V(value
, ctx
->Fog
.Color
);
247 case STATE_FOG_PARAMS
:
248 value
[0] = ctx
->Fog
.Density
;
249 value
[1] = ctx
->Fog
.Start
;
250 value
[2] = ctx
->Fog
.End
;
251 value
[3] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
252 ? 1.0f
: (GLfloat
)(1.0 / (ctx
->Fog
.End
- ctx
->Fog
.Start
));
254 case STATE_CLIPPLANE
:
256 const GLuint plane
= (GLuint
) state
[1];
257 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
260 case STATE_POINT_SIZE
:
261 value
[0] = ctx
->Point
.Size
;
262 value
[1] = ctx
->Point
.MinSize
;
263 value
[2] = ctx
->Point
.MaxSize
;
264 value
[3] = ctx
->Point
.Threshold
;
266 case STATE_POINT_ATTENUATION
:
267 value
[0] = ctx
->Point
.Params
[0];
268 value
[1] = ctx
->Point
.Params
[1];
269 value
[2] = ctx
->Point
.Params
[2];
272 case STATE_MODELVIEW_MATRIX
:
273 case STATE_PROJECTION_MATRIX
:
274 case STATE_MVP_MATRIX
:
275 case STATE_TEXTURE_MATRIX
:
276 case STATE_PROGRAM_MATRIX
:
277 case STATE_COLOR_MATRIX
:
279 /* state[0] = modelview, projection, texture, etc. */
280 /* state[1] = which texture matrix or program matrix */
281 /* state[2] = first row to fetch */
282 /* state[3] = last row to fetch */
283 /* state[4] = transpose, inverse or invtrans */
284 const GLmatrix
*matrix
;
285 const gl_state_index mat
= state
[0];
286 const GLuint index
= (GLuint
) state
[1];
287 const GLuint firstRow
= (GLuint
) state
[2];
288 const GLuint lastRow
= (GLuint
) state
[3];
289 const gl_state_index modifier
= state
[4];
292 ASSERT(firstRow
>= 0);
293 ASSERT(firstRow
< 4);
294 ASSERT(lastRow
>= 0);
296 if (mat
== STATE_MODELVIEW_MATRIX
) {
297 matrix
= ctx
->ModelviewMatrixStack
.Top
;
299 else if (mat
== STATE_PROJECTION_MATRIX
) {
300 matrix
= ctx
->ProjectionMatrixStack
.Top
;
302 else if (mat
== STATE_MVP_MATRIX
) {
303 matrix
= &ctx
->_ModelProjectMatrix
;
305 else if (mat
== STATE_TEXTURE_MATRIX
) {
306 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
308 else if (mat
== STATE_PROGRAM_MATRIX
) {
309 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
311 else if (mat
== STATE_COLOR_MATRIX
) {
312 matrix
= ctx
->ColorMatrixStack
.Top
;
315 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
318 if (modifier
== STATE_MATRIX_INVERSE
||
319 modifier
== STATE_MATRIX_INVTRANS
) {
320 /* Be sure inverse is up to date:
322 _math_matrix_alloc_inv( (GLmatrix
*) matrix
);
323 _math_matrix_analyse( (GLmatrix
*) matrix
);
329 if (modifier
== STATE_MATRIX_TRANSPOSE
||
330 modifier
== STATE_MATRIX_INVTRANS
) {
331 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
332 value
[i
++] = m
[row
* 4 + 0];
333 value
[i
++] = m
[row
* 4 + 1];
334 value
[i
++] = m
[row
* 4 + 2];
335 value
[i
++] = m
[row
* 4 + 3];
339 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
340 value
[i
++] = m
[row
+ 0];
341 value
[i
++] = m
[row
+ 4];
342 value
[i
++] = m
[row
+ 8];
343 value
[i
++] = m
[row
+ 12];
348 case STATE_DEPTH_RANGE
:
349 value
[0] = ctx
->Viewport
.Near
; /* near */
350 value
[1] = ctx
->Viewport
.Far
; /* far */
351 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
354 case STATE_FRAGMENT_PROGRAM
:
356 /* state[1] = {STATE_ENV, STATE_LOCAL} */
357 /* state[2] = parameter index */
358 const int idx
= (int) state
[2];
361 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
364 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
367 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
373 case STATE_VERTEX_PROGRAM
:
375 /* state[1] = {STATE_ENV, STATE_LOCAL} */
376 /* state[2] = parameter index */
377 const int idx
= (int) state
[2];
380 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
383 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
386 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
392 case STATE_NORMAL_SCALE
:
393 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
398 case STATE_CURRENT_ATTRIB
:
400 const GLuint idx
= (GLuint
) state
[2];
401 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
405 case STATE_NORMAL_SCALE
:
407 ctx
->_ModelViewInvScale
,
408 ctx
->_ModelViewInvScale
,
409 ctx
->_ModelViewInvScale
,
413 case STATE_TEXRECT_SCALE
:
414 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
415 * Used to convert unnormalized texcoords to normalized texcoords.
418 const int unit
= (int) state
[2];
419 const struct gl_texture_object
*texObj
420 = ctx
->Texture
.Unit
[unit
]._Current
;
422 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
424 (GLfloat
) (1.0 / texImage
->Width
),
425 (GLfloat
) (1.0 / texImage
->Height
),
431 case STATE_FOG_PARAMS_OPTIMIZED
:
432 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
433 * might be more expensive than EX2 on some hw, plus it needs
434 * another constant (e) anyway. Linear fog can now be done with a
436 * linear: fogcoord * -1/(end-start) + end/(end-start)
437 * exp: 2^-(density/ln(2) * fogcoord)
438 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
440 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
441 ? 1.0f
: (GLfloat
)(-1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
));
442 value
[1] = ctx
->Fog
.End
* -value
[0];
443 value
[2] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_LN2
);
444 value
[3] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
);
447 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
449 /* here, state[2] is the light number */
450 /* pre-normalize spot dir */
451 const GLuint ln
= (GLuint
) state
[2];
452 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormSpotDirection
);
453 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
457 case STATE_LIGHT_POSITION
:
459 const GLuint ln
= (GLuint
) state
[2];
460 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
464 case STATE_LIGHT_POSITION_NORMALIZED
:
466 const GLuint ln
= (GLuint
) state
[2];
467 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
468 NORMALIZE_3FV( value
);
472 case STATE_LIGHT_HALF_VECTOR
:
474 const GLuint ln
= (GLuint
) state
[2];
476 /* Compute infinite half angle vector:
477 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
478 * light.EyePosition.w should be 0 for infinite lights.
480 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
482 ADD_3V(value
, p
, ctx
->_EyeZDir
);
483 NORMALIZE_3FV(value
);
489 value
[0] = ctx
->Pixel
.RedScale
;
490 value
[1] = ctx
->Pixel
.GreenScale
;
491 value
[2] = ctx
->Pixel
.BlueScale
;
492 value
[3] = ctx
->Pixel
.AlphaScale
;
496 value
[0] = ctx
->Pixel
.RedBias
;
497 value
[1] = ctx
->Pixel
.GreenBias
;
498 value
[2] = ctx
->Pixel
.BlueBias
;
499 value
[3] = ctx
->Pixel
.AlphaBias
;
502 case STATE_PCM_SCALE
:
503 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixScale
);
507 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixBias
);
510 case STATE_SHADOW_AMBIENT
:
512 const int unit
= (int) state
[2];
513 const struct gl_texture_object
*texObj
514 = ctx
->Texture
.Unit
[unit
]._Current
;
519 value
[3] = texObj
->CompareFailValue
;
525 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
526 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
531 case STATE_ROT_MATRIX_0
:
533 const int unit
= (int) state
[2];
534 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
535 value
[0] = rotMat22
[0];
536 value
[1] = rotMat22
[2];
542 case STATE_ROT_MATRIX_1
:
544 const int unit
= (int) state
[2];
545 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
546 value
[0] = rotMat22
[1];
547 value
[1] = rotMat22
[3];
553 /* XXX: make sure new tokens added here are also handled in the
554 * _mesa_program_state_flags() switch, below.
557 /* Unknown state indexes are silently ignored here.
558 * Drivers may do something special.
565 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
572 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
573 * indicate that the given context state may have changed.
574 * The bitmask is used during validation to determine if we need to update
575 * vertex/fragment program parameters (like "state.material.color") when
576 * some GL state has changed.
579 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
584 case STATE_LIGHTMODEL_AMBIENT
:
585 case STATE_LIGHTMODEL_SCENECOLOR
:
586 case STATE_LIGHTPROD
:
590 case STATE_TEXENV_COLOR
:
593 case STATE_FOG_COLOR
:
594 case STATE_FOG_PARAMS
:
597 case STATE_CLIPPLANE
:
598 return _NEW_TRANSFORM
;
600 case STATE_POINT_SIZE
:
601 case STATE_POINT_ATTENUATION
:
604 case STATE_MODELVIEW_MATRIX
:
605 return _NEW_MODELVIEW
;
606 case STATE_PROJECTION_MATRIX
:
607 return _NEW_PROJECTION
;
608 case STATE_MVP_MATRIX
:
609 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
610 case STATE_TEXTURE_MATRIX
:
611 return _NEW_TEXTURE_MATRIX
;
612 case STATE_PROGRAM_MATRIX
:
613 return _NEW_TRACK_MATRIX
;
614 case STATE_COLOR_MATRIX
:
615 return _NEW_COLOR_MATRIX
;
617 case STATE_DEPTH_RANGE
:
618 return _NEW_VIEWPORT
;
620 case STATE_FRAGMENT_PROGRAM
:
621 case STATE_VERTEX_PROGRAM
:
624 case STATE_NORMAL_SCALE
:
625 return _NEW_MODELVIEW
;
629 case STATE_CURRENT_ATTRIB
:
630 return _NEW_CURRENT_ATTRIB
;
632 case STATE_NORMAL_SCALE
:
633 return _NEW_MODELVIEW
;
635 case STATE_TEXRECT_SCALE
:
636 case STATE_SHADOW_AMBIENT
:
637 case STATE_ROT_MATRIX_0
:
638 case STATE_ROT_MATRIX_1
:
640 case STATE_FOG_PARAMS_OPTIMIZED
:
642 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
643 case STATE_LIGHT_POSITION
:
644 case STATE_LIGHT_POSITION_NORMALIZED
:
645 case STATE_LIGHT_HALF_VECTOR
:
650 case STATE_PCM_SCALE
:
658 /* unknown state indexes are silently ignored and
659 * no flag set, since it is handled by the driver.
665 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
672 append(char *dst
, const char *src
)
683 * Convert token 'k' to a string, append it onto 'dst' string.
686 append_token(char *dst
, gl_state_index k
)
690 append(dst
, "material");
693 append(dst
, "light");
695 case STATE_LIGHTMODEL_AMBIENT
:
696 append(dst
, "lightmodel.ambient");
698 case STATE_LIGHTMODEL_SCENECOLOR
:
700 case STATE_LIGHTPROD
:
701 append(dst
, "lightprod");
704 append(dst
, "texgen");
706 case STATE_FOG_COLOR
:
707 append(dst
, "fog.color");
709 case STATE_FOG_PARAMS
:
710 append(dst
, "fog.params");
712 case STATE_CLIPPLANE
:
715 case STATE_POINT_SIZE
:
716 append(dst
, "point.size");
718 case STATE_POINT_ATTENUATION
:
719 append(dst
, "point.attenuation");
721 case STATE_MODELVIEW_MATRIX
:
722 append(dst
, "matrix.modelview");
724 case STATE_PROJECTION_MATRIX
:
725 append(dst
, "matrix.projection");
727 case STATE_MVP_MATRIX
:
728 append(dst
, "matrix.mvp");
730 case STATE_TEXTURE_MATRIX
:
731 append(dst
, "matrix.texture");
733 case STATE_PROGRAM_MATRIX
:
734 append(dst
, "matrix.program");
736 case STATE_COLOR_MATRIX
:
737 append(dst
, "matrix.color");
739 case STATE_MATRIX_INVERSE
:
740 append(dst
, ".inverse");
742 case STATE_MATRIX_TRANSPOSE
:
743 append(dst
, ".transpose");
745 case STATE_MATRIX_INVTRANS
:
746 append(dst
, ".invtrans");
749 append(dst
, ".ambient");
752 append(dst
, ".diffuse");
755 append(dst
, ".specular");
758 append(dst
, ".emission");
760 case STATE_SHININESS
:
761 append(dst
, "lshininess");
763 case STATE_HALF_VECTOR
:
764 append(dst
, ".half");
767 append(dst
, ".position");
769 case STATE_ATTENUATION
:
770 append(dst
, ".attenuation");
772 case STATE_SPOT_DIRECTION
:
773 append(dst
, ".spot.direction");
775 case STATE_SPOT_CUTOFF
:
776 append(dst
, ".spot.cutoff");
778 case STATE_TEXGEN_EYE_S
:
779 append(dst
, ".eye.s");
781 case STATE_TEXGEN_EYE_T
:
782 append(dst
, ".eye.t");
784 case STATE_TEXGEN_EYE_R
:
785 append(dst
, ".eye.r");
787 case STATE_TEXGEN_EYE_Q
:
788 append(dst
, ".eye.q");
790 case STATE_TEXGEN_OBJECT_S
:
791 append(dst
, ".object.s");
793 case STATE_TEXGEN_OBJECT_T
:
794 append(dst
, ".object.t");
796 case STATE_TEXGEN_OBJECT_R
:
797 append(dst
, ".object.r");
799 case STATE_TEXGEN_OBJECT_Q
:
800 append(dst
, ".object.q");
802 case STATE_TEXENV_COLOR
:
803 append(dst
, "texenv");
805 case STATE_DEPTH_RANGE
:
806 append(dst
, "depth.range");
808 case STATE_VERTEX_PROGRAM
:
809 case STATE_FRAGMENT_PROGRAM
:
815 append(dst
, "local");
817 /* BEGIN internal state vars */
819 append(dst
, ".internal.");
821 case STATE_CURRENT_ATTRIB
:
822 append(dst
, "current");
824 case STATE_NORMAL_SCALE
:
825 append(dst
, "normalScale");
827 case STATE_TEXRECT_SCALE
:
828 append(dst
, "texrectScale");
830 case STATE_FOG_PARAMS_OPTIMIZED
:
831 append(dst
, "fogParamsOptimized");
833 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
834 append(dst
, "lightSpotDirNormalized");
836 case STATE_LIGHT_POSITION
:
837 append(dst
, "lightPosition");
839 case STATE_LIGHT_POSITION_NORMALIZED
:
840 append(dst
, "light.position.normalized");
842 case STATE_LIGHT_HALF_VECTOR
:
843 append(dst
, "lightHalfVector");
846 append(dst
, "PTscale");
849 append(dst
, "PTbias");
851 case STATE_PCM_SCALE
:
852 append(dst
, "PCMscale");
855 append(dst
, "PCMbias");
857 case STATE_SHADOW_AMBIENT
:
858 append(dst
, "CompareFailValue");
861 append(dst
, "FbSize");
863 case STATE_ROT_MATRIX_0
:
864 append(dst
, "rotMatrixRow0");
866 case STATE_ROT_MATRIX_1
:
867 append(dst
, "rotMatrixRow1");
870 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
871 append(dst
, "driverState");
876 append_face(char *dst
, GLint face
)
879 append(dst
, "front.");
881 append(dst
, "back.");
885 append_index(char *dst
, GLint index
)
888 _mesa_sprintf(s
, "[%d]", index
);
893 * Make a string from the given state vector.
894 * For example, return "state.matrix.texture[2].inverse".
895 * Use _mesa_free() to deallocate the string.
898 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
903 append(str
, "state.");
904 append_token(str
, state
[0]);
908 append_face(str
, state
[1]);
909 append_token(str
, state
[2]);
912 append_index(str
, state
[1]); /* light number [i]. */
913 append_token(str
, state
[2]); /* coefficients */
915 case STATE_LIGHTMODEL_AMBIENT
:
916 append(str
, "lightmodel.ambient");
918 case STATE_LIGHTMODEL_SCENECOLOR
:
920 append(str
, "lightmodel.front.scenecolor");
923 append(str
, "lightmodel.back.scenecolor");
926 case STATE_LIGHTPROD
:
927 append_index(str
, state
[1]); /* light number [i]. */
928 append_face(str
, state
[2]);
929 append_token(str
, state
[3]);
932 append_index(str
, state
[1]); /* tex unit [i] */
933 append_token(str
, state
[2]); /* plane coef */
935 case STATE_TEXENV_COLOR
:
936 append_index(str
, state
[1]); /* tex unit [i] */
937 append(str
, "color");
939 case STATE_CLIPPLANE
:
940 append_index(str
, state
[1]); /* plane [i] */
941 append(str
, ".plane");
943 case STATE_MODELVIEW_MATRIX
:
944 case STATE_PROJECTION_MATRIX
:
945 case STATE_MVP_MATRIX
:
946 case STATE_TEXTURE_MATRIX
:
947 case STATE_PROGRAM_MATRIX
:
948 case STATE_COLOR_MATRIX
:
950 /* state[0] = modelview, projection, texture, etc. */
951 /* state[1] = which texture matrix or program matrix */
952 /* state[2] = first row to fetch */
953 /* state[3] = last row to fetch */
954 /* state[4] = transpose, inverse or invtrans */
955 const gl_state_index mat
= state
[0];
956 const GLuint index
= (GLuint
) state
[1];
957 const GLuint firstRow
= (GLuint
) state
[2];
958 const GLuint lastRow
= (GLuint
) state
[3];
959 const gl_state_index modifier
= state
[4];
961 mat
== STATE_TEXTURE_MATRIX
||
962 mat
== STATE_PROGRAM_MATRIX
)
963 append_index(str
, index
);
965 append_token(str
, modifier
);
966 if (firstRow
== lastRow
)
967 _mesa_sprintf(tmp
, ".row[%d]", firstRow
);
969 _mesa_sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
973 case STATE_POINT_SIZE
:
975 case STATE_POINT_ATTENUATION
:
977 case STATE_FOG_PARAMS
:
979 case STATE_FOG_COLOR
:
981 case STATE_DEPTH_RANGE
:
983 case STATE_FRAGMENT_PROGRAM
:
984 case STATE_VERTEX_PROGRAM
:
985 /* state[1] = {STATE_ENV, STATE_LOCAL} */
986 /* state[2] = parameter index */
987 append_token(str
, state
[1]);
988 append_index(str
, state
[2]);
991 append_token(str
, state
[1]);
992 if (state
[1] == STATE_CURRENT_ATTRIB
)
993 append_index(str
, state
[2]);
996 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1000 return _mesa_strdup(str
);
1005 * Loop over all the parameters in a parameter list. If the parameter
1006 * is a GL state reference, look up the current value of that state
1007 * variable and put it into the parameter's Value[4] array.
1008 * This would be called at glBegin time when using a fragment program.
1011 _mesa_load_state_parameters(GLcontext
*ctx
,
1012 struct gl_program_parameter_list
*paramList
)
1019 /*assert(ctx->Driver.NeedFlush == 0);*/
1021 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1022 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1023 _mesa_fetch_state(ctx
,
1024 (gl_state_index
*) paramList
->Parameters
[i
].StateIndexes
,
1025 paramList
->ParameterValues
[i
]);
1032 * Copy the 16 elements of a matrix into four consecutive program
1033 * registers starting at 'pos'.
1036 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
1039 for (i
= 0; i
< 4; i
++) {
1040 registers
[pos
+ i
][0] = mat
[0 + i
];
1041 registers
[pos
+ i
][1] = mat
[4 + i
];
1042 registers
[pos
+ i
][2] = mat
[8 + i
];
1043 registers
[pos
+ i
][3] = mat
[12 + i
];
1049 * As above, but transpose the matrix.
1052 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
1053 const GLfloat mat
[16])
1055 MEMCPY(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
1060 * Load current vertex program's parameter registers with tracked
1061 * matrices (if NV program). This only needs to be done per
1062 * glBegin/glEnd, not per-vertex.
1065 _mesa_load_tracked_matrices(GLcontext
*ctx
)
1069 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
1070 /* point 'mat' at source matrix */
1072 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
1073 mat
= ctx
->ModelviewMatrixStack
.Top
;
1075 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
1076 mat
= ctx
->ProjectionMatrixStack
.Top
;
1078 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
1079 mat
= ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
].Top
;
1081 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_COLOR
) {
1082 mat
= ctx
->ColorMatrixStack
.Top
;
1084 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
1085 /* XXX verify the combined matrix is up to date */
1086 mat
= &ctx
->_ModelProjectMatrix
;
1088 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
1089 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
1090 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
1091 ASSERT(n
< MAX_PROGRAM_MATRICES
);
1092 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
1095 /* no matrix is tracked, but we leave the register values as-is */
1096 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
1100 /* load the matrix values into sequential registers */
1101 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
1102 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1104 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
1105 _math_matrix_analyse(mat
); /* update the inverse */
1106 ASSERT(!_math_matrix_is_dirty(mat
));
1107 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
1109 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
1110 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1113 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
1114 == GL_INVERSE_TRANSPOSE_NV
);
1115 _math_matrix_analyse(mat
); /* update the inverse */
1116 ASSERT(!_math_matrix_is_dirty(mat
));
1117 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);