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.
38 #include "prog_statevars.h"
39 #include "prog_parameter.h"
40 #include "nvvertparse.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(GLcontext
*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
].EyeDirection
);
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
);
137 case STATE_POSITION_NORMALIZED
:
138 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
139 NORMALIZE_3FV( value
);
142 _mesa_problem(ctx
, "Invalid light state in fetch_state");
146 case STATE_LIGHTMODEL_AMBIENT
:
147 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
149 case STATE_LIGHTMODEL_SCENECOLOR
:
153 for (i
= 0; i
< 3; i
++) {
154 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
155 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
156 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
158 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
163 for (i
= 0; i
< 3; i
++) {
164 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
165 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
166 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
168 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
171 case STATE_LIGHTPROD
:
173 const GLuint ln
= (GLuint
) state
[1];
174 const GLuint face
= (GLuint
) state
[2];
176 ASSERT(face
== 0 || face
== 1);
179 for (i
= 0; i
< 3; i
++) {
180 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
181 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
183 /* [3] = material alpha */
184 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
187 for (i
= 0; i
< 3; i
++) {
188 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
189 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
191 /* [3] = material alpha */
192 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
195 for (i
= 0; i
< 3; i
++) {
196 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
197 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
199 /* [3] = material alpha */
200 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
203 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
209 /* state[1] is the texture unit */
210 const GLuint unit
= (GLuint
) state
[1];
211 /* state[2] is the texgen attribute */
213 case STATE_TEXGEN_EYE_S
:
214 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneS
);
216 case STATE_TEXGEN_EYE_T
:
217 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
219 case STATE_TEXGEN_EYE_R
:
220 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
222 case STATE_TEXGEN_EYE_Q
:
223 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
225 case STATE_TEXGEN_OBJECT_S
:
226 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
228 case STATE_TEXGEN_OBJECT_T
:
229 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
231 case STATE_TEXGEN_OBJECT_R
:
232 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
234 case STATE_TEXGEN_OBJECT_Q
:
235 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
238 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
242 case STATE_TEXENV_COLOR
:
244 /* state[1] is the texture unit */
245 const GLuint unit
= (GLuint
) state
[1];
246 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
249 case STATE_FOG_COLOR
:
250 COPY_4V(value
, ctx
->Fog
.Color
);
252 case STATE_FOG_PARAMS
:
253 value
[0] = ctx
->Fog
.Density
;
254 value
[1] = ctx
->Fog
.Start
;
255 value
[2] = ctx
->Fog
.End
;
256 value
[3] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
257 ? 1.0 : 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
259 case STATE_CLIPPLANE
:
261 const GLuint plane
= (GLuint
) state
[1];
262 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
265 case STATE_POINT_SIZE
:
266 value
[0] = ctx
->Point
.Size
;
267 value
[1] = ctx
->Point
.MinSize
;
268 value
[2] = ctx
->Point
.MaxSize
;
269 value
[3] = ctx
->Point
.Threshold
;
271 case STATE_POINT_ATTENUATION
:
272 value
[0] = ctx
->Point
.Params
[0];
273 value
[1] = ctx
->Point
.Params
[1];
274 value
[2] = ctx
->Point
.Params
[2];
277 case STATE_MODELVIEW_MATRIX
:
278 case STATE_PROJECTION_MATRIX
:
279 case STATE_MVP_MATRIX
:
280 case STATE_TEXTURE_MATRIX
:
281 case STATE_PROGRAM_MATRIX
:
282 case STATE_COLOR_MATRIX
:
284 /* state[0] = modelview, projection, texture, etc. */
285 /* state[1] = which texture matrix or program matrix */
286 /* state[2] = first row to fetch */
287 /* state[3] = last row to fetch */
288 /* state[4] = transpose, inverse or invtrans */
289 const GLmatrix
*matrix
;
290 const gl_state_index mat
= state
[0];
291 const GLuint index
= (GLuint
) state
[1];
292 const GLuint firstRow
= (GLuint
) state
[2];
293 const GLuint lastRow
= (GLuint
) state
[3];
294 const gl_state_index modifier
= state
[4];
297 ASSERT(firstRow
>= 0);
298 ASSERT(firstRow
< 4);
299 ASSERT(lastRow
>= 0);
301 if (mat
== STATE_MODELVIEW_MATRIX
) {
302 matrix
= ctx
->ModelviewMatrixStack
.Top
;
304 else if (mat
== STATE_PROJECTION_MATRIX
) {
305 matrix
= ctx
->ProjectionMatrixStack
.Top
;
307 else if (mat
== STATE_MVP_MATRIX
) {
308 matrix
= &ctx
->_ModelProjectMatrix
;
310 else if (mat
== STATE_TEXTURE_MATRIX
) {
311 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
313 else if (mat
== STATE_PROGRAM_MATRIX
) {
314 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
316 else if (mat
== STATE_COLOR_MATRIX
) {
317 matrix
= ctx
->ColorMatrixStack
.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_alloc_inv( (GLmatrix
*) matrix
);
328 _math_matrix_analyse( (GLmatrix
*) matrix
);
334 if (modifier
== STATE_MATRIX_TRANSPOSE
||
335 modifier
== STATE_MATRIX_INVTRANS
) {
336 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
337 value
[i
++] = m
[row
* 4 + 0];
338 value
[i
++] = m
[row
* 4 + 1];
339 value
[i
++] = m
[row
* 4 + 2];
340 value
[i
++] = m
[row
* 4 + 3];
344 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
345 value
[i
++] = m
[row
+ 0];
346 value
[i
++] = m
[row
+ 4];
347 value
[i
++] = m
[row
+ 8];
348 value
[i
++] = m
[row
+ 12];
353 case STATE_DEPTH_RANGE
:
354 value
[0] = ctx
->Viewport
.Near
; /* near */
355 value
[1] = ctx
->Viewport
.Far
; /* far */
356 value
[2] = ctx
->Viewport
.Far
- ctx
->Viewport
.Near
; /* far - near */
359 case STATE_FRAGMENT_PROGRAM
:
361 /* state[1] = {STATE_ENV, STATE_LOCAL} */
362 /* state[2] = parameter index */
363 const int idx
= (int) state
[2];
366 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
369 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
372 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
378 case STATE_VERTEX_PROGRAM
:
380 /* state[1] = {STATE_ENV, STATE_LOCAL} */
381 /* state[2] = parameter index */
382 const int idx
= (int) state
[2];
385 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
388 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
391 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
397 case STATE_NORMAL_SCALE
:
398 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
403 case STATE_NORMAL_SCALE
:
404 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
406 case STATE_TEXRECT_SCALE
:
408 const int unit
= (int) state
[2];
409 const struct gl_texture_object
*texObj
410 = ctx
->Texture
.Unit
[unit
]._Current
;
412 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
413 ASSIGN_4V(value
, 1.0 / texImage
->Width
,
414 1.0 / texImage
->Height
,
419 case STATE_FOG_PARAMS_OPTIMIZED
:
420 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
421 * might be more expensive than EX2 on some hw, plus it needs
422 * another constant (e) anyway. Linear fog can now be done with a
424 * linear: fogcoord * -1/(end-start) + end/(end-start)
425 * exp: 2^-(density/ln(2) * fogcoord)
426 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
428 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
429 ? 1.0 : -1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
430 value
[1] = ctx
->Fog
.End
* -value
[0];
431 value
[2] = ctx
->Fog
.Density
* ONE_DIV_LN2
;
432 value
[3] = ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
;
434 case STATE_SPOT_DIR_NORMALIZED
: {
435 /* here, state[2] is the light number */
436 /* pre-normalize spot dir */
437 const GLuint ln
= (GLuint
) state
[2];
438 COPY_3V(value
, ctx
->Light
.Light
[ln
].EyeDirection
);
439 NORMALIZE_3FV(value
);
440 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
444 value
[0] = ctx
->Pixel
.RedScale
;
445 value
[1] = ctx
->Pixel
.GreenScale
;
446 value
[2] = ctx
->Pixel
.BlueScale
;
447 value
[3] = ctx
->Pixel
.AlphaScale
;
450 value
[0] = ctx
->Pixel
.RedBias
;
451 value
[1] = ctx
->Pixel
.GreenBias
;
452 value
[2] = ctx
->Pixel
.BlueBias
;
453 value
[3] = ctx
->Pixel
.AlphaBias
;
455 case STATE_PCM_SCALE
:
456 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixScale
);
459 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixBias
);
462 /* unknown state indexes are silently ignored
463 * should be handled by the driver.
470 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
477 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
478 * indicate that the given context state may have changed.
479 * The bitmask is used during validation to determine if we need to update
480 * vertex/fragment program parameters (like "state.material.color") when
481 * some GL state has changed.
484 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
489 case STATE_LIGHTMODEL_AMBIENT
:
490 case STATE_LIGHTMODEL_SCENECOLOR
:
491 case STATE_LIGHTPROD
:
495 case STATE_TEXENV_COLOR
:
498 case STATE_FOG_COLOR
:
499 case STATE_FOG_PARAMS
:
502 case STATE_CLIPPLANE
:
503 return _NEW_TRANSFORM
;
505 case STATE_POINT_SIZE
:
506 case STATE_POINT_ATTENUATION
:
509 case STATE_MODELVIEW_MATRIX
:
510 return _NEW_MODELVIEW
;
511 case STATE_PROJECTION_MATRIX
:
512 return _NEW_PROJECTION
;
513 case STATE_MVP_MATRIX
:
514 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
515 case STATE_TEXTURE_MATRIX
:
516 return _NEW_TEXTURE_MATRIX
;
517 case STATE_PROGRAM_MATRIX
:
518 return _NEW_TRACK_MATRIX
;
519 case STATE_COLOR_MATRIX
:
520 return _NEW_COLOR_MATRIX
;
522 case STATE_DEPTH_RANGE
:
523 return _NEW_VIEWPORT
;
525 case STATE_FRAGMENT_PROGRAM
:
526 case STATE_VERTEX_PROGRAM
:
529 case STATE_NORMAL_SCALE
:
530 return _NEW_MODELVIEW
;
534 case STATE_TEXRECT_SCALE
:
536 case STATE_FOG_PARAMS_OPTIMIZED
:
539 /* unknown state indexes are silently ignored and
540 * no flag set, since it is handled by the driver.
546 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
553 append(char *dst
, const char *src
)
564 append_token(char *dst
, gl_state_index k
)
568 append(dst
, "material");
571 append(dst
, "light");
573 case STATE_LIGHTMODEL_AMBIENT
:
574 append(dst
, "lightmodel.ambient");
576 case STATE_LIGHTMODEL_SCENECOLOR
:
578 case STATE_LIGHTPROD
:
579 append(dst
, "lightprod");
582 append(dst
, "texgen");
584 case STATE_FOG_COLOR
:
585 append(dst
, "fog.color");
587 case STATE_FOG_PARAMS
:
588 append(dst
, "fog.params");
590 case STATE_CLIPPLANE
:
593 case STATE_POINT_SIZE
:
594 append(dst
, "point.size");
596 case STATE_POINT_ATTENUATION
:
597 append(dst
, "point.attenuation");
599 case STATE_MODELVIEW_MATRIX
:
600 append(dst
, "matrix.modelview");
602 case STATE_PROJECTION_MATRIX
:
603 append(dst
, "matrix.projection");
605 case STATE_MVP_MATRIX
:
606 append(dst
, "matrix.mvp");
608 case STATE_TEXTURE_MATRIX
:
609 append(dst
, "matrix.texture");
611 case STATE_PROGRAM_MATRIX
:
612 append(dst
, "matrix.program");
614 case STATE_COLOR_MATRIX
:
615 append(dst
, "matrix.color");
617 case STATE_MATRIX_INVERSE
:
618 append(dst
, ".inverse");
620 case STATE_MATRIX_TRANSPOSE
:
621 append(dst
, ".transpose");
623 case STATE_MATRIX_INVTRANS
:
624 append(dst
, ".invtrans");
627 append(dst
, ".ambient");
630 append(dst
, ".diffuse");
633 append(dst
, ".specular");
636 append(dst
, ".emission");
638 case STATE_SHININESS
:
639 append(dst
, "lshininess");
641 case STATE_HALF_VECTOR
:
642 append(dst
, ".half");
645 append(dst
, ".position");
647 case STATE_ATTENUATION
:
648 append(dst
, ".attenuation");
650 case STATE_SPOT_DIRECTION
:
651 append(dst
, ".spot.direction");
653 case STATE_SPOT_CUTOFF
:
654 append(dst
, ".spot.cutoff");
656 case STATE_TEXGEN_EYE_S
:
657 append(dst
, "eye.s");
659 case STATE_TEXGEN_EYE_T
:
660 append(dst
, "eye.t");
662 case STATE_TEXGEN_EYE_R
:
663 append(dst
, "eye.r");
665 case STATE_TEXGEN_EYE_Q
:
666 append(dst
, "eye.q");
668 case STATE_TEXGEN_OBJECT_S
:
669 append(dst
, "object.s");
671 case STATE_TEXGEN_OBJECT_T
:
672 append(dst
, "object.t");
674 case STATE_TEXGEN_OBJECT_R
:
675 append(dst
, "object.r");
677 case STATE_TEXGEN_OBJECT_Q
:
678 append(dst
, "object.q");
680 case STATE_TEXENV_COLOR
:
681 append(dst
, "texenv");
683 case STATE_DEPTH_RANGE
:
684 append(dst
, "depth.range");
686 case STATE_VERTEX_PROGRAM
:
687 case STATE_FRAGMENT_PROGRAM
:
693 append(dst
, "local");
695 case STATE_NORMAL_SCALE
:
696 append(dst
, "normalScale");
699 case STATE_POSITION_NORMALIZED
:
700 append(dst
, "(internal)");
703 append(dst
, "PTscale");
706 append(dst
, "PTbias");
708 case STATE_PCM_SCALE
:
709 append(dst
, "PCMscale");
712 append(dst
, "PCMbias");
720 append_face(char *dst
, GLint face
)
723 append(dst
, "front.");
725 append(dst
, "back.");
729 append_index(char *dst
, GLint index
)
732 _mesa_sprintf(s
, "[%d]", index
);
737 * Make a string from the given state vector.
738 * For example, return "state.matrix.texture[2].inverse".
739 * Use _mesa_free() to deallocate the string.
742 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
747 append(str
, "state.");
748 append_token(str
, (gl_state_index
) state
[0]);
752 append_face(str
, state
[1]);
753 append_token(str
, (gl_state_index
) state
[2]);
756 append_index(str
, state
[1]); /* light number [i]. */
757 append_token(str
, (gl_state_index
) state
[2]); /* coefficients */
759 case STATE_LIGHTMODEL_AMBIENT
:
760 append(str
, "lightmodel.ambient");
762 case STATE_LIGHTMODEL_SCENECOLOR
:
764 append(str
, "lightmodel.front.scenecolor");
767 append(str
, "lightmodel.back.scenecolor");
770 case STATE_LIGHTPROD
:
771 append_index(str
, state
[1]); /* light number [i]. */
772 append_face(str
, state
[2]);
773 append_token(str
, (gl_state_index
) state
[3]);
776 append_index(str
, state
[1]); /* tex unit [i] */
777 append_token(str
, (gl_state_index
) state
[2]); /* plane coef */
779 case STATE_TEXENV_COLOR
:
780 append_index(str
, state
[1]); /* tex unit [i] */
781 append(str
, "color");
783 case STATE_CLIPPLANE
:
784 append_index(str
, state
[1]); /* plane [i] */
785 append(str
, ".plane");
787 case STATE_MODELVIEW_MATRIX
:
788 case STATE_PROJECTION_MATRIX
:
789 case STATE_MVP_MATRIX
:
790 case STATE_TEXTURE_MATRIX
:
791 case STATE_PROGRAM_MATRIX
:
792 case STATE_COLOR_MATRIX
:
794 /* state[0] = modelview, projection, texture, etc. */
795 /* state[1] = which texture matrix or program matrix */
796 /* state[2] = first row to fetch */
797 /* state[3] = last row to fetch */
798 /* state[4] = transpose, inverse or invtrans */
799 const gl_state_index mat
= (gl_state_index
) state
[0];
800 const GLuint index
= (GLuint
) state
[1];
801 const GLuint firstRow
= (GLuint
) state
[2];
802 const GLuint lastRow
= (GLuint
) state
[3];
803 const gl_state_index modifier
= (gl_state_index
) state
[4];
805 mat
== STATE_TEXTURE_MATRIX
||
806 mat
== STATE_PROGRAM_MATRIX
)
807 append_index(str
, index
);
809 append_token(str
, modifier
);
810 if (firstRow
== lastRow
)
811 _mesa_sprintf(tmp
, ".row[%d]", firstRow
);
813 _mesa_sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
817 case STATE_POINT_SIZE
:
819 case STATE_POINT_ATTENUATION
:
821 case STATE_FOG_PARAMS
:
823 case STATE_FOG_COLOR
:
825 case STATE_DEPTH_RANGE
:
827 case STATE_FRAGMENT_PROGRAM
:
828 case STATE_VERTEX_PROGRAM
:
829 /* state[1] = {STATE_ENV, STATE_LOCAL} */
830 /* state[2] = parameter index */
831 append_token(str
, (gl_state_index
) state
[1]);
832 append_index(str
, state
[2]);
837 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
841 return _mesa_strdup(str
);
846 * Loop over all the parameters in a parameter list. If the parameter
847 * is a GL state reference, look up the current value of that state
848 * variable and put it into the parameter's Value[4] array.
849 * This would be called at glBegin time when using a fragment program.
852 _mesa_load_state_parameters(GLcontext
*ctx
,
853 struct gl_program_parameter_list
*paramList
)
860 /*assert(ctx->Driver.NeedFlush == 0);*/
862 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
863 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
864 _mesa_fetch_state(ctx
,
865 (gl_state_index
*) paramList
->Parameters
[i
].StateIndexes
,
866 paramList
->ParameterValues
[i
]);
873 * Copy the 16 elements of a matrix into four consecutive program
874 * registers starting at 'pos'.
877 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
880 for (i
= 0; i
< 4; i
++) {
881 registers
[pos
+ i
][0] = mat
[0 + i
];
882 registers
[pos
+ i
][1] = mat
[4 + i
];
883 registers
[pos
+ i
][2] = mat
[8 + i
];
884 registers
[pos
+ i
][3] = mat
[12 + i
];
890 * As above, but transpose the matrix.
893 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
894 const GLfloat mat
[16])
896 MEMCPY(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
901 * Load current vertex program's parameter registers with tracked
902 * matrices (if NV program). This only needs to be done per
903 * glBegin/glEnd, not per-vertex.
906 _mesa_load_tracked_matrices(GLcontext
*ctx
)
910 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
911 /* point 'mat' at source matrix */
913 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
914 mat
= ctx
->ModelviewMatrixStack
.Top
;
916 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
917 mat
= ctx
->ProjectionMatrixStack
.Top
;
919 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
920 mat
= ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
].Top
;
922 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_COLOR
) {
923 mat
= ctx
->ColorMatrixStack
.Top
;
925 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
926 /* XXX verify the combined matrix is up to date */
927 mat
= &ctx
->_ModelProjectMatrix
;
929 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
930 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
931 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
932 ASSERT(n
< MAX_PROGRAM_MATRICES
);
933 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
936 /* no matrix is tracked, but we leave the register values as-is */
937 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
941 /* load the matrix values into sequential registers */
942 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
943 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
945 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
946 _math_matrix_analyse(mat
); /* update the inverse */
947 ASSERT(!_math_matrix_is_dirty(mat
));
948 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
950 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
951 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
954 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
955 == GL_INVERSE_TRANSPOSE_NV
);
956 _math_matrix_analyse(mat
); /* update the inverse */
957 ASSERT(!_math_matrix_is_dirty(mat
));
958 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);