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"
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
);
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
].EyePlaneS
);
212 case STATE_TEXGEN_EYE_T
:
213 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneT
);
215 case STATE_TEXGEN_EYE_R
:
216 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneR
);
218 case STATE_TEXGEN_EYE_Q
:
219 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EyePlaneQ
);
221 case STATE_TEXGEN_OBJECT_S
:
222 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneS
);
224 case STATE_TEXGEN_OBJECT_T
:
225 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneT
);
227 case STATE_TEXGEN_OBJECT_R
:
228 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneR
);
230 case STATE_TEXGEN_OBJECT_Q
:
231 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].ObjectPlaneQ
);
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 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
245 case STATE_FOG_COLOR
:
246 COPY_4V(value
, ctx
->Fog
.Color
);
248 case STATE_FOG_PARAMS
:
249 value
[0] = ctx
->Fog
.Density
;
250 value
[1] = ctx
->Fog
.Start
;
251 value
[2] = ctx
->Fog
.End
;
252 value
[3] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
253 ? 1.0f
: (GLfloat
)(1.0 / (ctx
->Fog
.End
- ctx
->Fog
.Start
));
255 case STATE_CLIPPLANE
:
257 const GLuint plane
= (GLuint
) state
[1];
258 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
261 case STATE_POINT_SIZE
:
262 value
[0] = ctx
->Point
.Size
;
263 value
[1] = ctx
->Point
.MinSize
;
264 value
[2] = ctx
->Point
.MaxSize
;
265 value
[3] = ctx
->Point
.Threshold
;
267 case STATE_POINT_ATTENUATION
:
268 value
[0] = ctx
->Point
.Params
[0];
269 value
[1] = ctx
->Point
.Params
[1];
270 value
[2] = ctx
->Point
.Params
[2];
273 case STATE_MODELVIEW_MATRIX
:
274 case STATE_PROJECTION_MATRIX
:
275 case STATE_MVP_MATRIX
:
276 case STATE_TEXTURE_MATRIX
:
277 case STATE_PROGRAM_MATRIX
:
278 case STATE_COLOR_MATRIX
:
280 /* state[0] = modelview, projection, texture, etc. */
281 /* state[1] = which texture matrix or program matrix */
282 /* state[2] = first row to fetch */
283 /* state[3] = last row to fetch */
284 /* state[4] = transpose, inverse or invtrans */
285 const GLmatrix
*matrix
;
286 const gl_state_index mat
= state
[0];
287 const GLuint index
= (GLuint
) state
[1];
288 const GLuint firstRow
= (GLuint
) state
[2];
289 const GLuint lastRow
= (GLuint
) state
[3];
290 const gl_state_index modifier
= state
[4];
293 ASSERT(firstRow
>= 0);
294 ASSERT(firstRow
< 4);
295 ASSERT(lastRow
>= 0);
297 if (mat
== STATE_MODELVIEW_MATRIX
) {
298 matrix
= ctx
->ModelviewMatrixStack
.Top
;
300 else if (mat
== STATE_PROJECTION_MATRIX
) {
301 matrix
= ctx
->ProjectionMatrixStack
.Top
;
303 else if (mat
== STATE_MVP_MATRIX
) {
304 matrix
= &ctx
->_ModelProjectMatrix
;
306 else if (mat
== STATE_TEXTURE_MATRIX
) {
307 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
309 else if (mat
== STATE_PROGRAM_MATRIX
) {
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_NORMAL_SCALE
:
401 ctx
->_ModelViewInvScale
,
402 ctx
->_ModelViewInvScale
,
403 ctx
->_ModelViewInvScale
,
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
, (GLfloat
) (1.0 / texImage
->Width
),
414 (GLfloat
)(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.0f
: (GLfloat
)(-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
;
435 case STATE_LIGHT_SPOT_DIR_NORMALIZED
: {
436 /* here, state[2] is the light number */
437 /* pre-normalize spot dir */
438 const GLuint ln
= (GLuint
) state
[2];
439 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormDirection
);
440 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
444 case STATE_LIGHT_POSITION
: {
445 const GLuint ln
= (GLuint
) state
[2];
446 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
450 case STATE_LIGHT_POSITION_NORMALIZED
: {
451 const GLuint ln
= (GLuint
) state
[2];
452 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
453 NORMALIZE_3FV( value
);
457 case STATE_LIGHT_HALF_VECTOR
: {
458 const GLuint ln
= (GLuint
) state
[2];
460 /* Compute infinite half angle vector:
461 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
462 * light.EyePosition.w should be 0 for infinite lights.
464 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
466 ADD_3V(value
, p
, ctx
->_EyeZDir
);
467 NORMALIZE_3FV(value
);
474 value
[0] = ctx
->Pixel
.RedScale
;
475 value
[1] = ctx
->Pixel
.GreenScale
;
476 value
[2] = ctx
->Pixel
.BlueScale
;
477 value
[3] = ctx
->Pixel
.AlphaScale
;
480 value
[0] = ctx
->Pixel
.RedBias
;
481 value
[1] = ctx
->Pixel
.GreenBias
;
482 value
[2] = ctx
->Pixel
.BlueBias
;
483 value
[3] = ctx
->Pixel
.AlphaBias
;
485 case STATE_PCM_SCALE
:
486 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixScale
);
489 COPY_4V(value
, ctx
->Pixel
.PostColorMatrixBias
);
491 case STATE_SHADOW_AMBIENT
:
493 const int unit
= (int) state
[2];
494 const struct gl_texture_object
*texObj
495 = ctx
->Texture
.Unit
[unit
]._Current
;
497 value
[0] = texObj
->ShadowAmbient
;
498 value
[1] = texObj
->ShadowAmbient
;
499 value
[2] = texObj
->ShadowAmbient
;
500 value
[3] = texObj
->ShadowAmbient
;
506 /* unknown state indexes are silently ignored
507 * should be handled by the driver.
514 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
521 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
522 * indicate that the given context state may have changed.
523 * The bitmask is used during validation to determine if we need to update
524 * vertex/fragment program parameters (like "state.material.color") when
525 * some GL state has changed.
528 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
533 case STATE_LIGHTMODEL_AMBIENT
:
534 case STATE_LIGHTMODEL_SCENECOLOR
:
535 case STATE_LIGHTPROD
:
539 case STATE_TEXENV_COLOR
:
542 case STATE_FOG_COLOR
:
543 case STATE_FOG_PARAMS
:
546 case STATE_CLIPPLANE
:
547 return _NEW_TRANSFORM
;
549 case STATE_POINT_SIZE
:
550 case STATE_POINT_ATTENUATION
:
553 case STATE_MODELVIEW_MATRIX
:
554 return _NEW_MODELVIEW
;
555 case STATE_PROJECTION_MATRIX
:
556 return _NEW_PROJECTION
;
557 case STATE_MVP_MATRIX
:
558 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
559 case STATE_TEXTURE_MATRIX
:
560 return _NEW_TEXTURE_MATRIX
;
561 case STATE_PROGRAM_MATRIX
:
562 return _NEW_TRACK_MATRIX
;
563 case STATE_COLOR_MATRIX
:
564 return _NEW_COLOR_MATRIX
;
566 case STATE_DEPTH_RANGE
:
567 return _NEW_VIEWPORT
;
569 case STATE_FRAGMENT_PROGRAM
:
570 case STATE_VERTEX_PROGRAM
:
573 case STATE_NORMAL_SCALE
:
574 return _NEW_MODELVIEW
;
578 case STATE_TEXRECT_SCALE
:
579 case STATE_SHADOW_AMBIENT
:
581 case STATE_FOG_PARAMS_OPTIMIZED
:
584 /* unknown state indexes are silently ignored and
585 * no flag set, since it is handled by the driver.
591 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
598 append(char *dst
, const char *src
)
609 append_token(char *dst
, gl_state_index k
)
613 append(dst
, "material");
616 append(dst
, "light");
618 case STATE_LIGHTMODEL_AMBIENT
:
619 append(dst
, "lightmodel.ambient");
621 case STATE_LIGHTMODEL_SCENECOLOR
:
623 case STATE_LIGHTPROD
:
624 append(dst
, "lightprod");
627 append(dst
, "texgen");
629 case STATE_FOG_COLOR
:
630 append(dst
, "fog.color");
632 case STATE_FOG_PARAMS
:
633 append(dst
, "fog.params");
635 case STATE_CLIPPLANE
:
638 case STATE_POINT_SIZE
:
639 append(dst
, "point.size");
641 case STATE_POINT_ATTENUATION
:
642 append(dst
, "point.attenuation");
644 case STATE_MODELVIEW_MATRIX
:
645 append(dst
, "matrix.modelview");
647 case STATE_PROJECTION_MATRIX
:
648 append(dst
, "matrix.projection");
650 case STATE_MVP_MATRIX
:
651 append(dst
, "matrix.mvp");
653 case STATE_TEXTURE_MATRIX
:
654 append(dst
, "matrix.texture");
656 case STATE_PROGRAM_MATRIX
:
657 append(dst
, "matrix.program");
659 case STATE_COLOR_MATRIX
:
660 append(dst
, "matrix.color");
662 case STATE_MATRIX_INVERSE
:
663 append(dst
, ".inverse");
665 case STATE_MATRIX_TRANSPOSE
:
666 append(dst
, ".transpose");
668 case STATE_MATRIX_INVTRANS
:
669 append(dst
, ".invtrans");
672 append(dst
, ".ambient");
675 append(dst
, ".diffuse");
678 append(dst
, ".specular");
681 append(dst
, ".emission");
683 case STATE_SHININESS
:
684 append(dst
, "lshininess");
686 case STATE_HALF_VECTOR
:
687 append(dst
, ".half");
690 append(dst
, ".position");
692 case STATE_ATTENUATION
:
693 append(dst
, ".attenuation");
695 case STATE_SPOT_DIRECTION
:
696 append(dst
, ".spot.direction");
698 case STATE_SPOT_CUTOFF
:
699 append(dst
, ".spot.cutoff");
701 case STATE_TEXGEN_EYE_S
:
702 append(dst
, "eye.s");
704 case STATE_TEXGEN_EYE_T
:
705 append(dst
, "eye.t");
707 case STATE_TEXGEN_EYE_R
:
708 append(dst
, "eye.r");
710 case STATE_TEXGEN_EYE_Q
:
711 append(dst
, "eye.q");
713 case STATE_TEXGEN_OBJECT_S
:
714 append(dst
, "object.s");
716 case STATE_TEXGEN_OBJECT_T
:
717 append(dst
, "object.t");
719 case STATE_TEXGEN_OBJECT_R
:
720 append(dst
, "object.r");
722 case STATE_TEXGEN_OBJECT_Q
:
723 append(dst
, "object.q");
725 case STATE_TEXENV_COLOR
:
726 append(dst
, "texenv");
728 case STATE_DEPTH_RANGE
:
729 append(dst
, "depth.range");
731 case STATE_VERTEX_PROGRAM
:
732 case STATE_FRAGMENT_PROGRAM
:
738 append(dst
, "local");
740 case STATE_NORMAL_SCALE
:
741 append(dst
, "normalScale");
744 append(dst
, "(internal)");
747 append(dst
, "PTscale");
750 append(dst
, "PTbias");
752 case STATE_PCM_SCALE
:
753 append(dst
, "PCMscale");
756 append(dst
, "PCMbias");
758 case STATE_SHADOW_AMBIENT
:
759 append(dst
, "ShadowAmbient");
767 append_face(char *dst
, GLint face
)
770 append(dst
, "front.");
772 append(dst
, "back.");
776 append_index(char *dst
, GLint index
)
779 _mesa_sprintf(s
, "[%d]", index
);
784 * Make a string from the given state vector.
785 * For example, return "state.matrix.texture[2].inverse".
786 * Use _mesa_free() to deallocate the string.
789 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
794 append(str
, "state.");
795 append_token(str
, (gl_state_index
) state
[0]);
799 append_face(str
, state
[1]);
800 append_token(str
, (gl_state_index
) state
[2]);
803 append_index(str
, state
[1]); /* light number [i]. */
804 append_token(str
, (gl_state_index
) state
[2]); /* coefficients */
806 case STATE_LIGHTMODEL_AMBIENT
:
807 append(str
, "lightmodel.ambient");
809 case STATE_LIGHTMODEL_SCENECOLOR
:
811 append(str
, "lightmodel.front.scenecolor");
814 append(str
, "lightmodel.back.scenecolor");
817 case STATE_LIGHTPROD
:
818 append_index(str
, state
[1]); /* light number [i]. */
819 append_face(str
, state
[2]);
820 append_token(str
, (gl_state_index
) state
[3]);
823 append_index(str
, state
[1]); /* tex unit [i] */
824 append_token(str
, (gl_state_index
) state
[2]); /* plane coef */
826 case STATE_TEXENV_COLOR
:
827 append_index(str
, state
[1]); /* tex unit [i] */
828 append(str
, "color");
830 case STATE_CLIPPLANE
:
831 append_index(str
, state
[1]); /* plane [i] */
832 append(str
, ".plane");
834 case STATE_MODELVIEW_MATRIX
:
835 case STATE_PROJECTION_MATRIX
:
836 case STATE_MVP_MATRIX
:
837 case STATE_TEXTURE_MATRIX
:
838 case STATE_PROGRAM_MATRIX
:
839 case STATE_COLOR_MATRIX
:
841 /* state[0] = modelview, projection, texture, etc. */
842 /* state[1] = which texture matrix or program matrix */
843 /* state[2] = first row to fetch */
844 /* state[3] = last row to fetch */
845 /* state[4] = transpose, inverse or invtrans */
846 const gl_state_index mat
= (gl_state_index
) state
[0];
847 const GLuint index
= (GLuint
) state
[1];
848 const GLuint firstRow
= (GLuint
) state
[2];
849 const GLuint lastRow
= (GLuint
) state
[3];
850 const gl_state_index modifier
= (gl_state_index
) state
[4];
852 mat
== STATE_TEXTURE_MATRIX
||
853 mat
== STATE_PROGRAM_MATRIX
)
854 append_index(str
, index
);
856 append_token(str
, modifier
);
857 if (firstRow
== lastRow
)
858 _mesa_sprintf(tmp
, ".row[%d]", firstRow
);
860 _mesa_sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
864 case STATE_POINT_SIZE
:
866 case STATE_POINT_ATTENUATION
:
868 case STATE_FOG_PARAMS
:
870 case STATE_FOG_COLOR
:
872 case STATE_DEPTH_RANGE
:
874 case STATE_FRAGMENT_PROGRAM
:
875 case STATE_VERTEX_PROGRAM
:
876 /* state[1] = {STATE_ENV, STATE_LOCAL} */
877 /* state[2] = parameter index */
878 append_token(str
, (gl_state_index
) state
[1]);
879 append_index(str
, state
[2]);
884 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
888 return _mesa_strdup(str
);
893 * Loop over all the parameters in a parameter list. If the parameter
894 * is a GL state reference, look up the current value of that state
895 * variable and put it into the parameter's Value[4] array.
896 * This would be called at glBegin time when using a fragment program.
899 _mesa_load_state_parameters(GLcontext
*ctx
,
900 struct gl_program_parameter_list
*paramList
)
907 /*assert(ctx->Driver.NeedFlush == 0);*/
909 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
910 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
911 _mesa_fetch_state(ctx
,
912 (gl_state_index
*) paramList
->Parameters
[i
].StateIndexes
,
913 paramList
->ParameterValues
[i
]);
920 * Copy the 16 elements of a matrix into four consecutive program
921 * registers starting at 'pos'.
924 load_matrix(GLfloat registers
[][4], GLuint pos
, const GLfloat mat
[16])
927 for (i
= 0; i
< 4; i
++) {
928 registers
[pos
+ i
][0] = mat
[0 + i
];
929 registers
[pos
+ i
][1] = mat
[4 + i
];
930 registers
[pos
+ i
][2] = mat
[8 + i
];
931 registers
[pos
+ i
][3] = mat
[12 + i
];
937 * As above, but transpose the matrix.
940 load_transpose_matrix(GLfloat registers
[][4], GLuint pos
,
941 const GLfloat mat
[16])
943 MEMCPY(registers
[pos
], mat
, 16 * sizeof(GLfloat
));
948 * Load current vertex program's parameter registers with tracked
949 * matrices (if NV program). This only needs to be done per
950 * glBegin/glEnd, not per-vertex.
953 _mesa_load_tracked_matrices(GLcontext
*ctx
)
957 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
/ 4; i
++) {
958 /* point 'mat' at source matrix */
960 if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_MODELVIEW
) {
961 mat
= ctx
->ModelviewMatrixStack
.Top
;
963 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_PROJECTION
) {
964 mat
= ctx
->ProjectionMatrixStack
.Top
;
966 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_TEXTURE
) {
967 mat
= ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
].Top
;
969 else if (ctx
->VertexProgram
.TrackMatrix
[i
] == GL_COLOR
) {
970 mat
= ctx
->ColorMatrixStack
.Top
;
972 else if (ctx
->VertexProgram
.TrackMatrix
[i
]==GL_MODELVIEW_PROJECTION_NV
) {
973 /* XXX verify the combined matrix is up to date */
974 mat
= &ctx
->_ModelProjectMatrix
;
976 else if (ctx
->VertexProgram
.TrackMatrix
[i
] >= GL_MATRIX0_NV
&&
977 ctx
->VertexProgram
.TrackMatrix
[i
] <= GL_MATRIX7_NV
) {
978 GLuint n
= ctx
->VertexProgram
.TrackMatrix
[i
] - GL_MATRIX0_NV
;
979 ASSERT(n
< MAX_PROGRAM_MATRICES
);
980 mat
= ctx
->ProgramMatrixStack
[n
].Top
;
983 /* no matrix is tracked, but we leave the register values as-is */
984 assert(ctx
->VertexProgram
.TrackMatrix
[i
] == GL_NONE
);
988 /* load the matrix values into sequential registers */
989 if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_IDENTITY_NV
) {
990 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
992 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_INVERSE_NV
) {
993 _math_matrix_analyse(mat
); /* update the inverse */
994 ASSERT(!_math_matrix_is_dirty(mat
));
995 load_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);
997 else if (ctx
->VertexProgram
.TrackMatrixTransform
[i
] == GL_TRANSPOSE_NV
) {
998 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->m
);
1001 assert(ctx
->VertexProgram
.TrackMatrixTransform
[i
]
1002 == GL_INVERSE_TRANSPOSE_NV
);
1003 _math_matrix_analyse(mat
); /* update the inverse */
1004 ASSERT(!_math_matrix_is_dirty(mat
));
1005 load_transpose_matrix(ctx
->VertexProgram
.Parameters
, i
*4, mat
->inv
);