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 "prog_statevars.h"
38 #include "prog_parameter.h"
42 * Use the list of tokens in the state[] array to find global GL state
43 * and return it in <value>. Usually, four values are returned in <value>
44 * but matrix queries may return as many as 16 values.
45 * This function is used for ARB vertex/fragment programs.
46 * The program parser will produce the state[] values.
49 _mesa_fetch_state(GLcontext
*ctx
, const gl_state_index state
[],
55 /* state[1] is either 0=front or 1=back side */
56 const GLuint face
= (GLuint
) state
[1];
57 const struct gl_material
*mat
= &ctx
->Light
.Material
;
58 ASSERT(face
== 0 || face
== 1);
59 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
60 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
61 /* XXX we could get rid of this switch entirely with a little
62 * work in arbprogparse.c's parse_state_single_item().
64 /* state[2] is the material attribute */
67 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
70 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
73 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
76 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
79 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
85 _mesa_problem(ctx
, "Invalid material state in fetch_state");
91 /* state[1] is the light number */
92 const GLuint ln
= (GLuint
) state
[1];
93 /* state[2] is the light attribute */
96 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
99 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
102 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
105 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
107 case STATE_ATTENUATION
:
108 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
109 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
110 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
111 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
113 case STATE_SPOT_DIRECTION
:
114 COPY_3V(value
, ctx
->Light
.Light
[ln
].SpotDirection
);
115 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
117 case STATE_SPOT_CUTOFF
:
118 value
[0] = ctx
->Light
.Light
[ln
].SpotCutoff
;
120 case STATE_HALF_VECTOR
:
122 static const GLfloat eye_z
[] = {0, 0, 1};
124 /* Compute infinite half angle vector:
125 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
126 * light.EyePosition.w should be 0 for infinite lights.
128 COPY_3V(p
, ctx
->Light
.Light
[ln
].EyePosition
);
130 ADD_3V(value
, p
, eye_z
);
131 NORMALIZE_3FV(value
);
136 _mesa_problem(ctx
, "Invalid light state in fetch_state");
140 case STATE_LIGHTMODEL_AMBIENT
:
141 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
143 case STATE_LIGHTMODEL_SCENECOLOR
:
147 for (i
= 0; i
< 3; i
++) {
148 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
149 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
150 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
152 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
157 for (i
= 0; i
< 3; i
++) {
158 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
159 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
160 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
162 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
165 case STATE_LIGHTPROD
:
167 const GLuint ln
= (GLuint
) state
[1];
168 const GLuint face
= (GLuint
) state
[2];
170 ASSERT(face
== 0 || face
== 1);
173 for (i
= 0; i
< 3; i
++) {
174 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
175 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
177 /* [3] = material alpha */
178 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
181 for (i
= 0; i
< 3; i
++) {
182 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
183 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
185 /* [3] = material alpha */
186 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
189 for (i
= 0; i
< 3; i
++) {
190 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
191 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
193 /* [3] = material alpha */
194 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
197 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
203 /* state[1] is the texture unit */
204 const GLuint unit
= (GLuint
) state
[1];
205 /* state[2] is the texgen attribute */
207 case STATE_TEXGEN_EYE_S
:
208 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.EyePlane
);
210 case STATE_TEXGEN_EYE_T
:
211 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.EyePlane
);
213 case STATE_TEXGEN_EYE_R
:
214 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.EyePlane
);
216 case STATE_TEXGEN_EYE_Q
:
217 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.EyePlane
);
219 case STATE_TEXGEN_OBJECT_S
:
220 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.ObjectPlane
);
222 case STATE_TEXGEN_OBJECT_T
:
223 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.ObjectPlane
);
225 case STATE_TEXGEN_OBJECT_R
:
226 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.ObjectPlane
);
228 case STATE_TEXGEN_OBJECT_Q
:
229 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.ObjectPlane
);
232 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
236 case STATE_TEXENV_COLOR
:
238 /* state[1] is the texture unit */
239 const GLuint unit
= (GLuint
) state
[1];
240 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
243 case STATE_FOG_COLOR
:
244 COPY_4V(value
, ctx
->Fog
.Color
);
246 case STATE_FOG_PARAMS
:
247 value
[0] = ctx
->Fog
.Density
;
248 value
[1] = ctx
->Fog
.Start
;
249 value
[2] = ctx
->Fog
.End
;
250 value
[3] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
251 ? 1.0f
: (GLfloat
)(1.0 / (ctx
->Fog
.End
- ctx
->Fog
.Start
));
253 case STATE_CLIPPLANE
:
255 const GLuint plane
= (GLuint
) state
[1];
256 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
259 case STATE_POINT_SIZE
:
260 value
[0] = ctx
->Point
.Size
;
261 value
[1] = ctx
->Point
.MinSize
;
262 value
[2] = ctx
->Point
.MaxSize
;
263 value
[3] = ctx
->Point
.Threshold
;
265 case STATE_POINT_ATTENUATION
:
266 value
[0] = ctx
->Point
.Params
[0];
267 value
[1] = ctx
->Point
.Params
[1];
268 value
[2] = ctx
->Point
.Params
[2];
271 case STATE_MODELVIEW_MATRIX
:
272 case STATE_PROJECTION_MATRIX
:
273 case STATE_MVP_MATRIX
:
274 case STATE_TEXTURE_MATRIX
:
275 case STATE_PROGRAM_MATRIX
:
276 case STATE_COLOR_MATRIX
:
278 /* state[0] = modelview, projection, texture, etc. */
279 /* state[1] = which texture matrix or program matrix */
280 /* state[2] = first row to fetch */
281 /* state[3] = last row to fetch */
282 /* state[4] = transpose, inverse or invtrans */
283 const GLmatrix
*matrix
;
284 const gl_state_index mat
= state
[0];
285 const GLuint index
= (GLuint
) state
[1];
286 const GLuint firstRow
= (GLuint
) state
[2];
287 const GLuint lastRow
= (GLuint
) state
[3];
288 const gl_state_index modifier
= state
[4];
291 ASSERT(firstRow
>= 0);
292 ASSERT(firstRow
< 4);
293 ASSERT(lastRow
>= 0);
295 if (mat
== STATE_MODELVIEW_MATRIX
) {
296 matrix
= ctx
->ModelviewMatrixStack
.Top
;
298 else if (mat
== STATE_PROJECTION_MATRIX
) {
299 matrix
= ctx
->ProjectionMatrixStack
.Top
;
301 else if (mat
== STATE_MVP_MATRIX
) {
302 matrix
= &ctx
->_ModelProjectMatrix
;
304 else if (mat
== STATE_TEXTURE_MATRIX
) {
305 ASSERT(index
< Elements(ctx
->TextureMatrixStack
));
306 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
308 else if (mat
== STATE_PROGRAM_MATRIX
) {
309 ASSERT(index
< Elements(ctx
->ProgramMatrixStack
));
310 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
312 else if (mat
== STATE_COLOR_MATRIX
) {
313 matrix
= ctx
->ColorMatrixStack
.Top
;
316 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
319 if (modifier
== STATE_MATRIX_INVERSE
||
320 modifier
== STATE_MATRIX_INVTRANS
) {
321 /* Be sure inverse is up to date:
323 _math_matrix_alloc_inv( (GLmatrix
*) matrix
);
324 _math_matrix_analyse( (GLmatrix
*) matrix
);
330 if (modifier
== STATE_MATRIX_TRANSPOSE
||
331 modifier
== STATE_MATRIX_INVTRANS
) {
332 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
333 value
[i
++] = m
[row
* 4 + 0];
334 value
[i
++] = m
[row
* 4 + 1];
335 value
[i
++] = m
[row
* 4 + 2];
336 value
[i
++] = m
[row
* 4 + 3];
340 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
341 value
[i
++] = m
[row
+ 0];
342 value
[i
++] = m
[row
+ 4];
343 value
[i
++] = m
[row
+ 8];
344 value
[i
++] = m
[row
+ 12];
349 case STATE_DEPTH_RANGE
:
350 value
[0] = ctx
->Viewport
.Near
; /* near */
351 value
[1] = ctx
->Viewport
.Far
; /* far */
352 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
355 case STATE_FRAGMENT_PROGRAM
:
357 /* state[1] = {STATE_ENV, STATE_LOCAL} */
358 /* state[2] = parameter index */
359 const int idx
= (int) state
[2];
362 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
365 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
368 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
374 case STATE_VERTEX_PROGRAM
:
376 /* state[1] = {STATE_ENV, STATE_LOCAL} */
377 /* state[2] = parameter index */
378 const int idx
= (int) state
[2];
381 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
384 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
387 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
393 case STATE_NORMAL_SCALE
:
394 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
399 case STATE_CURRENT_ATTRIB
:
401 const GLuint idx
= (GLuint
) state
[2];
402 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
406 case STATE_NORMAL_SCALE
:
408 ctx
->_ModelViewInvScale
,
409 ctx
->_ModelViewInvScale
,
410 ctx
->_ModelViewInvScale
,
414 case STATE_TEXRECT_SCALE
:
415 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
416 * Used to convert unnormalized texcoords to normalized texcoords.
419 const int unit
= (int) state
[2];
420 const struct gl_texture_object
*texObj
421 = ctx
->Texture
.Unit
[unit
]._Current
;
423 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
425 (GLfloat
) (1.0 / texImage
->Width
),
426 (GLfloat
) (1.0 / texImage
->Height
),
432 case STATE_FOG_PARAMS_OPTIMIZED
:
433 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
434 * might be more expensive than EX2 on some hw, plus it needs
435 * another constant (e) anyway. Linear fog can now be done with a
437 * linear: fogcoord * -1/(end-start) + end/(end-start)
438 * exp: 2^-(density/ln(2) * fogcoord)
439 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
441 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
442 ? 1.0f
: (GLfloat
)(-1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
));
443 value
[1] = ctx
->Fog
.End
* -value
[0];
444 value
[2] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_LN2
);
445 value
[3] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
);
448 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
450 /* here, state[2] is the light number */
451 /* pre-normalize spot dir */
452 const GLuint ln
= (GLuint
) state
[2];
453 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormSpotDirection
);
454 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
458 case STATE_LIGHT_POSITION
:
460 const GLuint ln
= (GLuint
) state
[2];
461 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
465 case STATE_LIGHT_POSITION_NORMALIZED
:
467 const GLuint ln
= (GLuint
) state
[2];
468 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
469 NORMALIZE_3FV( value
);
473 case STATE_LIGHT_HALF_VECTOR
:
475 const GLuint ln
= (GLuint
) state
[2];
477 /* Compute infinite half angle vector:
478 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
479 * light.EyePosition.w should be 0 for infinite lights.
481 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
483 ADD_3V(value
, p
, ctx
->_EyeZDir
);
484 NORMALIZE_3FV(value
);
490 value
[0] = ctx
->Pixel
.RedScale
;
491 value
[1] = ctx
->Pixel
.GreenScale
;
492 value
[2] = ctx
->Pixel
.BlueScale
;
493 value
[3] = ctx
->Pixel
.AlphaScale
;
497 value
[0] = ctx
->Pixel
.RedBias
;
498 value
[1] = ctx
->Pixel
.GreenBias
;
499 value
[2] = ctx
->Pixel
.BlueBias
;
500 value
[3] = ctx
->Pixel
.AlphaBias
;
503 case STATE_PCM_SCALE
:
504 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixScale
);
508 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixBias
);
511 case STATE_SHADOW_AMBIENT
:
513 const int unit
= (int) state
[2];
514 const struct gl_texture_object
*texObj
515 = ctx
->Texture
.Unit
[unit
]._Current
;
520 value
[3] = texObj
->CompareFailValue
;
526 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
527 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
532 case STATE_ROT_MATRIX_0
:
534 const int unit
= (int) state
[2];
535 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
536 value
[0] = rotMat22
[0];
537 value
[1] = rotMat22
[2];
543 case STATE_ROT_MATRIX_1
:
545 const int unit
= (int) state
[2];
546 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
547 value
[0] = rotMat22
[1];
548 value
[1] = rotMat22
[3];
554 /* XXX: make sure new tokens added here are also handled in the
555 * _mesa_program_state_flags() switch, below.
558 /* Unknown state indexes are silently ignored here.
559 * Drivers may do something special.
566 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
573 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
574 * indicate that the given context state may have changed.
575 * The bitmask is used during validation to determine if we need to update
576 * vertex/fragment program parameters (like "state.material.color") when
577 * some GL state has changed.
580 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
585 case STATE_LIGHTMODEL_AMBIENT
:
586 case STATE_LIGHTMODEL_SCENECOLOR
:
587 case STATE_LIGHTPROD
:
591 case STATE_TEXENV_COLOR
:
594 case STATE_FOG_COLOR
:
595 case STATE_FOG_PARAMS
:
598 case STATE_CLIPPLANE
:
599 return _NEW_TRANSFORM
;
601 case STATE_POINT_SIZE
:
602 case STATE_POINT_ATTENUATION
:
605 case STATE_MODELVIEW_MATRIX
:
606 return _NEW_MODELVIEW
;
607 case STATE_PROJECTION_MATRIX
:
608 return _NEW_PROJECTION
;
609 case STATE_MVP_MATRIX
:
610 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
611 case STATE_TEXTURE_MATRIX
:
612 return _NEW_TEXTURE_MATRIX
;
613 case STATE_PROGRAM_MATRIX
:
614 return _NEW_TRACK_MATRIX
;
615 case STATE_COLOR_MATRIX
:
616 return _NEW_COLOR_MATRIX
;
618 case STATE_DEPTH_RANGE
:
619 return _NEW_VIEWPORT
;
621 case STATE_FRAGMENT_PROGRAM
:
622 case STATE_VERTEX_PROGRAM
:
625 case STATE_NORMAL_SCALE
:
626 return _NEW_MODELVIEW
;
630 case STATE_CURRENT_ATTRIB
:
631 return _NEW_CURRENT_ATTRIB
;
633 case STATE_NORMAL_SCALE
:
634 return _NEW_MODELVIEW
;
636 case STATE_TEXRECT_SCALE
:
637 case STATE_SHADOW_AMBIENT
:
638 case STATE_ROT_MATRIX_0
:
639 case STATE_ROT_MATRIX_1
:
641 case STATE_FOG_PARAMS_OPTIMIZED
:
643 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
644 case STATE_LIGHT_POSITION
:
645 case STATE_LIGHT_POSITION_NORMALIZED
:
646 case STATE_LIGHT_HALF_VECTOR
:
651 case STATE_PCM_SCALE
:
659 /* unknown state indexes are silently ignored and
660 * no flag set, since it is handled by the driver.
666 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
673 append(char *dst
, const char *src
)
684 * Convert token 'k' to a string, append it onto 'dst' string.
687 append_token(char *dst
, gl_state_index k
)
691 append(dst
, "material");
694 append(dst
, "light");
696 case STATE_LIGHTMODEL_AMBIENT
:
697 append(dst
, "lightmodel.ambient");
699 case STATE_LIGHTMODEL_SCENECOLOR
:
701 case STATE_LIGHTPROD
:
702 append(dst
, "lightprod");
705 append(dst
, "texgen");
707 case STATE_FOG_COLOR
:
708 append(dst
, "fog.color");
710 case STATE_FOG_PARAMS
:
711 append(dst
, "fog.params");
713 case STATE_CLIPPLANE
:
716 case STATE_POINT_SIZE
:
717 append(dst
, "point.size");
719 case STATE_POINT_ATTENUATION
:
720 append(dst
, "point.attenuation");
722 case STATE_MODELVIEW_MATRIX
:
723 append(dst
, "matrix.modelview");
725 case STATE_PROJECTION_MATRIX
:
726 append(dst
, "matrix.projection");
728 case STATE_MVP_MATRIX
:
729 append(dst
, "matrix.mvp");
731 case STATE_TEXTURE_MATRIX
:
732 append(dst
, "matrix.texture");
734 case STATE_PROGRAM_MATRIX
:
735 append(dst
, "matrix.program");
737 case STATE_COLOR_MATRIX
:
738 append(dst
, "matrix.color");
740 case STATE_MATRIX_INVERSE
:
741 append(dst
, ".inverse");
743 case STATE_MATRIX_TRANSPOSE
:
744 append(dst
, ".transpose");
746 case STATE_MATRIX_INVTRANS
:
747 append(dst
, ".invtrans");
750 append(dst
, ".ambient");
753 append(dst
, ".diffuse");
756 append(dst
, ".specular");
759 append(dst
, ".emission");
761 case STATE_SHININESS
:
762 append(dst
, "lshininess");
764 case STATE_HALF_VECTOR
:
765 append(dst
, ".half");
768 append(dst
, ".position");
770 case STATE_ATTENUATION
:
771 append(dst
, ".attenuation");
773 case STATE_SPOT_DIRECTION
:
774 append(dst
, ".spot.direction");
776 case STATE_SPOT_CUTOFF
:
777 append(dst
, ".spot.cutoff");
779 case STATE_TEXGEN_EYE_S
:
780 append(dst
, ".eye.s");
782 case STATE_TEXGEN_EYE_T
:
783 append(dst
, ".eye.t");
785 case STATE_TEXGEN_EYE_R
:
786 append(dst
, ".eye.r");
788 case STATE_TEXGEN_EYE_Q
:
789 append(dst
, ".eye.q");
791 case STATE_TEXGEN_OBJECT_S
:
792 append(dst
, ".object.s");
794 case STATE_TEXGEN_OBJECT_T
:
795 append(dst
, ".object.t");
797 case STATE_TEXGEN_OBJECT_R
:
798 append(dst
, ".object.r");
800 case STATE_TEXGEN_OBJECT_Q
:
801 append(dst
, ".object.q");
803 case STATE_TEXENV_COLOR
:
804 append(dst
, "texenv");
806 case STATE_DEPTH_RANGE
:
807 append(dst
, "depth.range");
809 case STATE_VERTEX_PROGRAM
:
810 case STATE_FRAGMENT_PROGRAM
:
816 append(dst
, "local");
818 /* BEGIN internal state vars */
820 append(dst
, ".internal.");
822 case STATE_CURRENT_ATTRIB
:
823 append(dst
, "current");
825 case STATE_NORMAL_SCALE
:
826 append(dst
, "normalScale");
828 case STATE_TEXRECT_SCALE
:
829 append(dst
, "texrectScale");
831 case STATE_FOG_PARAMS_OPTIMIZED
:
832 append(dst
, "fogParamsOptimized");
834 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
835 append(dst
, "lightSpotDirNormalized");
837 case STATE_LIGHT_POSITION
:
838 append(dst
, "lightPosition");
840 case STATE_LIGHT_POSITION_NORMALIZED
:
841 append(dst
, "light.position.normalized");
843 case STATE_LIGHT_HALF_VECTOR
:
844 append(dst
, "lightHalfVector");
847 append(dst
, "PTscale");
850 append(dst
, "PTbias");
852 case STATE_PCM_SCALE
:
853 append(dst
, "PCMscale");
856 append(dst
, "PCMbias");
858 case STATE_SHADOW_AMBIENT
:
859 append(dst
, "CompareFailValue");
862 append(dst
, "FbSize");
864 case STATE_ROT_MATRIX_0
:
865 append(dst
, "rotMatrixRow0");
867 case STATE_ROT_MATRIX_1
:
868 append(dst
, "rotMatrixRow1");
871 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
872 append(dst
, "driverState");
877 append_face(char *dst
, GLint face
)
880 append(dst
, "front.");
882 append(dst
, "back.");
886 append_index(char *dst
, GLint index
)
889 _mesa_sprintf(s
, "[%d]", index
);
894 * Make a string from the given state vector.
895 * For example, return "state.matrix.texture[2].inverse".
896 * Use _mesa_free() to deallocate the string.
899 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
904 append(str
, "state.");
905 append_token(str
, state
[0]);
909 append_face(str
, state
[1]);
910 append_token(str
, state
[2]);
913 append_index(str
, state
[1]); /* light number [i]. */
914 append_token(str
, state
[2]); /* coefficients */
916 case STATE_LIGHTMODEL_AMBIENT
:
917 append(str
, "lightmodel.ambient");
919 case STATE_LIGHTMODEL_SCENECOLOR
:
921 append(str
, "lightmodel.front.scenecolor");
924 append(str
, "lightmodel.back.scenecolor");
927 case STATE_LIGHTPROD
:
928 append_index(str
, state
[1]); /* light number [i]. */
929 append_face(str
, state
[2]);
930 append_token(str
, state
[3]);
933 append_index(str
, state
[1]); /* tex unit [i] */
934 append_token(str
, state
[2]); /* plane coef */
936 case STATE_TEXENV_COLOR
:
937 append_index(str
, state
[1]); /* tex unit [i] */
938 append(str
, "color");
940 case STATE_CLIPPLANE
:
941 append_index(str
, state
[1]); /* plane [i] */
942 append(str
, ".plane");
944 case STATE_MODELVIEW_MATRIX
:
945 case STATE_PROJECTION_MATRIX
:
946 case STATE_MVP_MATRIX
:
947 case STATE_TEXTURE_MATRIX
:
948 case STATE_PROGRAM_MATRIX
:
949 case STATE_COLOR_MATRIX
:
951 /* state[0] = modelview, projection, texture, etc. */
952 /* state[1] = which texture matrix or program matrix */
953 /* state[2] = first row to fetch */
954 /* state[3] = last row to fetch */
955 /* state[4] = transpose, inverse or invtrans */
956 const gl_state_index mat
= state
[0];
957 const GLuint index
= (GLuint
) state
[1];
958 const GLuint firstRow
= (GLuint
) state
[2];
959 const GLuint lastRow
= (GLuint
) state
[3];
960 const gl_state_index modifier
= state
[4];
962 mat
== STATE_TEXTURE_MATRIX
||
963 mat
== STATE_PROGRAM_MATRIX
)
964 append_index(str
, index
);
966 append_token(str
, modifier
);
967 if (firstRow
== lastRow
)
968 _mesa_sprintf(tmp
, ".row[%d]", firstRow
);
970 _mesa_sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
974 case STATE_POINT_SIZE
:
976 case STATE_POINT_ATTENUATION
:
978 case STATE_FOG_PARAMS
:
980 case STATE_FOG_COLOR
:
982 case STATE_DEPTH_RANGE
:
984 case STATE_FRAGMENT_PROGRAM
:
985 case STATE_VERTEX_PROGRAM
:
986 /* state[1] = {STATE_ENV, STATE_LOCAL} */
987 /* state[2] = parameter index */
988 append_token(str
, state
[1]);
989 append_index(str
, state
[2]);
992 append_token(str
, state
[1]);
993 if (state
[1] == STATE_CURRENT_ATTRIB
)
994 append_index(str
, state
[2]);
997 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1001 return _mesa_strdup(str
);
1006 * Loop over all the parameters in a parameter list. If the parameter
1007 * is a GL state reference, look up the current value of that state
1008 * variable and put it into the parameter's Value[4] array.
1009 * This would be called at glBegin time when using a fragment program.
1012 _mesa_load_state_parameters(GLcontext
*ctx
,
1013 struct gl_program_parameter_list
*paramList
)
1020 /*assert(ctx->Driver.NeedFlush == 0);*/
1022 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1023 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1024 _mesa_fetch_state(ctx
,
1025 (gl_state_index
*) paramList
->Parameters
[i
].StateIndexes
,
1026 paramList
->ParameterValues
[i
]);
1033 * Copy the 16 elements of a matrix into four consecutive program
1034 * registers starting at 'pos'.
1037 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
1040 for (i
= 0; i
< 4; i
++) {
1041 registers
[pos
+ i
][0] = mat
[0 + i
];
1042 registers
[pos
+ i
][1] = mat
[4 + i
];
1043 registers
[pos
+ i
][2] = mat
[8 + i
];
1044 registers
[pos
+ i
][3] = mat
[12 + i
];
1050 * As above, but transpose the matrix.
1053 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
1054 const GLfloat mat
[16])
1056 MEMCPY(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
1061 * Load current vertex program's parameter registers with tracked
1062 * matrices (if NV program). This only needs to be done per
1063 * glBegin/glEnd, not per-vertex.
1066 _mesa_load_tracked_matrices(GLcontext
*ctx
)
1070 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
1071 /* point 'mat' at source matrix */
1073 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
1074 mat
= ctx
->ModelviewMatrixStack
.Top
;
1076 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
1077 mat
= ctx
->ProjectionMatrixStack
.Top
;
1079 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
1080 GLuint unit
= MIN2(ctx
->Texture
.CurrentUnit
,
1081 Elements(ctx
->TextureMatrixStack
) - 1);
1082 mat
= ctx
->TextureMatrixStack
[unit
].Top
;
1084 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_COLOR
) {
1085 mat
= ctx
->ColorMatrixStack
.Top
;
1087 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
1088 /* XXX verify the combined matrix is up to date */
1089 mat
= &ctx
->_ModelProjectMatrix
;
1091 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
1092 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
1093 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
1094 ASSERT(n
< Elements(ctx
->ProgramMatrixStack
));
1095 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
1098 /* no matrix is tracked, but we leave the register values as-is */
1099 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
1103 /* load the matrix values into sequential registers */
1104 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
1105 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1107 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
1108 _math_matrix_analyse(mat
); /* update the inverse */
1109 ASSERT(!_math_matrix_is_dirty(mat
));
1110 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
1112 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
1113 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1116 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
1117 == GL_INVERSE_TRANSPOSE_NV
);
1118 _math_matrix_analyse(mat
); /* update the inverse */
1119 ASSERT(!_math_matrix_is_dirty(mat
));
1120 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);