2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/blend.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "main/fbobject.h"
39 #include "prog_statevars.h"
40 #include "prog_parameter.h"
41 #include "main/samplerobj.h"
45 * Use the list of tokens in the state[] array to find global GL state
46 * and return it in <value>. Usually, four values are returned in <value>
47 * but matrix queries may return as many as 16 values.
48 * This function is used for ARB vertex/fragment programs.
49 * The program parser will produce the state[] values.
52 _mesa_fetch_state(struct gl_context
*ctx
, const gl_state_index state
[],
58 /* state[1] is either 0=front or 1=back side */
59 const GLuint face
= (GLuint
) state
[1];
60 const struct gl_material
*mat
= &ctx
->Light
.Material
;
61 ASSERT(face
== 0 || face
== 1);
62 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
63 ASSERT(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
64 /* XXX we could get rid of this switch entirely with a little
65 * work in arbprogparse.c's parse_state_single_item().
67 /* state[2] is the material attribute */
70 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
73 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
76 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
79 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
82 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
88 _mesa_problem(ctx
, "Invalid material state in fetch_state");
94 /* state[1] is the light number */
95 const GLuint ln
= (GLuint
) state
[1];
96 /* state[2] is the light attribute */
99 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
102 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
105 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
108 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
110 case STATE_ATTENUATION
:
111 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
112 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
113 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
114 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
116 case STATE_SPOT_DIRECTION
:
117 COPY_3V(value
, ctx
->Light
.Light
[ln
].SpotDirection
);
118 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
120 case STATE_SPOT_CUTOFF
:
121 value
[0] = ctx
->Light
.Light
[ln
].SpotCutoff
;
123 case STATE_HALF_VECTOR
:
125 static const GLfloat eye_z
[] = {0, 0, 1};
127 /* Compute infinite half angle vector:
128 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
129 * light.EyePosition.w should be 0 for infinite lights.
131 COPY_3V(p
, ctx
->Light
.Light
[ln
].EyePosition
);
133 ADD_3V(value
, p
, eye_z
);
134 NORMALIZE_3FV(value
);
139 _mesa_problem(ctx
, "Invalid light state in fetch_state");
143 case STATE_LIGHTMODEL_AMBIENT
:
144 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
146 case STATE_LIGHTMODEL_SCENECOLOR
:
150 for (i
= 0; i
< 3; i
++) {
151 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
152 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
153 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
155 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
160 for (i
= 0; i
< 3; i
++) {
161 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
162 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
163 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
165 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
168 case STATE_LIGHTPROD
:
170 const GLuint ln
= (GLuint
) state
[1];
171 const GLuint face
= (GLuint
) state
[2];
173 ASSERT(face
== 0 || face
== 1);
176 for (i
= 0; i
< 3; i
++) {
177 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
178 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
180 /* [3] = material alpha */
181 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
184 for (i
= 0; i
< 3; i
++) {
185 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
186 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
188 /* [3] = material alpha */
189 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
192 for (i
= 0; i
< 3; i
++) {
193 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
194 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
196 /* [3] = material alpha */
197 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
200 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
206 /* state[1] is the texture unit */
207 const GLuint unit
= (GLuint
) state
[1];
208 /* state[2] is the texgen attribute */
210 case STATE_TEXGEN_EYE_S
:
211 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.EyePlane
);
213 case STATE_TEXGEN_EYE_T
:
214 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.EyePlane
);
216 case STATE_TEXGEN_EYE_R
:
217 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.EyePlane
);
219 case STATE_TEXGEN_EYE_Q
:
220 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.EyePlane
);
222 case STATE_TEXGEN_OBJECT_S
:
223 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.ObjectPlane
);
225 case STATE_TEXGEN_OBJECT_T
:
226 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.ObjectPlane
);
228 case STATE_TEXGEN_OBJECT_R
:
229 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.ObjectPlane
);
231 case STATE_TEXGEN_OBJECT_Q
:
232 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.ObjectPlane
);
235 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
239 case STATE_TEXENV_COLOR
:
241 /* state[1] is the texture unit */
242 const GLuint unit
= (GLuint
) state
[1];
243 if (_mesa_get_clamp_fragment_color(ctx
))
244 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
246 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColorUnclamped
);
249 case STATE_FOG_COLOR
:
250 if (_mesa_get_clamp_fragment_color(ctx
))
251 COPY_4V(value
, ctx
->Fog
.Color
);
253 COPY_4V(value
, ctx
->Fog
.ColorUnclamped
);
255 case STATE_FOG_PARAMS
:
256 value
[0] = ctx
->Fog
.Density
;
257 value
[1] = ctx
->Fog
.Start
;
258 value
[2] = ctx
->Fog
.End
;
259 value
[3] = 1.0f
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
261 case STATE_CLIPPLANE
:
263 const GLuint plane
= (GLuint
) state
[1];
264 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
267 case STATE_POINT_SIZE
:
268 value
[0] = ctx
->Point
.Size
;
269 value
[1] = ctx
->Point
.MinSize
;
270 value
[2] = ctx
->Point
.MaxSize
;
271 value
[3] = ctx
->Point
.Threshold
;
273 case STATE_POINT_ATTENUATION
:
274 value
[0] = ctx
->Point
.Params
[0];
275 value
[1] = ctx
->Point
.Params
[1];
276 value
[2] = ctx
->Point
.Params
[2];
279 case STATE_MODELVIEW_MATRIX
:
280 case STATE_PROJECTION_MATRIX
:
281 case STATE_MVP_MATRIX
:
282 case STATE_TEXTURE_MATRIX
:
283 case STATE_PROGRAM_MATRIX
:
285 /* state[0] = modelview, projection, texture, etc. */
286 /* state[1] = which texture matrix or program matrix */
287 /* state[2] = first row to fetch */
288 /* state[3] = last row to fetch */
289 /* state[4] = transpose, inverse or invtrans */
290 const GLmatrix
*matrix
;
291 const gl_state_index mat
= state
[0];
292 const GLuint index
= (GLuint
) state
[1];
293 const GLuint firstRow
= (GLuint
) state
[2];
294 const GLuint lastRow
= (GLuint
) state
[3];
295 const gl_state_index modifier
= state
[4];
298 ASSERT(firstRow
>= 0);
299 ASSERT(firstRow
< 4);
300 ASSERT(lastRow
>= 0);
302 if (mat
== STATE_MODELVIEW_MATRIX
) {
303 matrix
= ctx
->ModelviewMatrixStack
.Top
;
305 else if (mat
== STATE_PROJECTION_MATRIX
) {
306 matrix
= ctx
->ProjectionMatrixStack
.Top
;
308 else if (mat
== STATE_MVP_MATRIX
) {
309 matrix
= &ctx
->_ModelProjectMatrix
;
311 else if (mat
== STATE_TEXTURE_MATRIX
) {
312 ASSERT(index
< Elements(ctx
->TextureMatrixStack
));
313 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
315 else if (mat
== STATE_PROGRAM_MATRIX
) {
316 ASSERT(index
< Elements(ctx
->ProgramMatrixStack
));
317 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
320 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
323 if (modifier
== STATE_MATRIX_INVERSE
||
324 modifier
== STATE_MATRIX_INVTRANS
) {
325 /* Be sure inverse is up to date:
327 _math_matrix_analyse( (GLmatrix
*) matrix
);
333 if (modifier
== STATE_MATRIX_TRANSPOSE
||
334 modifier
== STATE_MATRIX_INVTRANS
) {
335 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
336 value
[i
++] = m
[row
* 4 + 0];
337 value
[i
++] = m
[row
* 4 + 1];
338 value
[i
++] = m
[row
* 4 + 2];
339 value
[i
++] = m
[row
* 4 + 3];
343 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
344 value
[i
++] = m
[row
+ 0];
345 value
[i
++] = m
[row
+ 4];
346 value
[i
++] = m
[row
+ 8];
347 value
[i
++] = m
[row
+ 12];
352 case STATE_NUM_SAMPLES
:
353 ((int *)value
)[0] = ctx
->DrawBuffer
->Visual
.samples
;
355 case STATE_DEPTH_RANGE
:
356 value
[0] = ctx
->ViewportArray
[0].Near
; /* near */
357 value
[1] = ctx
->ViewportArray
[0].Far
; /* far */
358 value
[2] = ctx
->ViewportArray
[0].Far
- ctx
->ViewportArray
[0].Near
; /* far - near */
361 case STATE_FRAGMENT_PROGRAM
:
363 /* state[1] = {STATE_ENV, STATE_LOCAL} */
364 /* state[2] = parameter index */
365 const int idx
= (int) state
[2];
368 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
371 if (!ctx
->FragmentProgram
.Current
->Base
.LocalParams
) {
372 ctx
->FragmentProgram
.Current
->Base
.LocalParams
=
373 calloc(MAX_PROGRAM_LOCAL_PARAMS
, sizeof(float[4]));
374 if (!ctx
->FragmentProgram
.Current
->Base
.LocalParams
)
378 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
381 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
387 case STATE_VERTEX_PROGRAM
:
389 /* state[1] = {STATE_ENV, STATE_LOCAL} */
390 /* state[2] = parameter index */
391 const int idx
= (int) state
[2];
394 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
397 if (!ctx
->VertexProgram
.Current
->Base
.LocalParams
) {
398 ctx
->VertexProgram
.Current
->Base
.LocalParams
=
399 calloc(MAX_PROGRAM_LOCAL_PARAMS
, sizeof(float[4]));
400 if (!ctx
->VertexProgram
.Current
->Base
.LocalParams
)
404 COPY_4V(value
, ctx
->VertexProgram
.Current
->Base
.LocalParams
[idx
]);
407 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
413 case STATE_NORMAL_SCALE
:
414 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
419 case STATE_CURRENT_ATTRIB
:
421 const GLuint idx
= (GLuint
) state
[2];
422 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
426 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
428 const GLuint idx
= (GLuint
) state
[2];
429 if(ctx
->Light
._ClampVertexColor
&&
430 (idx
== VERT_ATTRIB_COLOR0
||
431 idx
== VERT_ATTRIB_COLOR1
)) {
432 value
[0] = CLAMP(ctx
->Current
.Attrib
[idx
][0], 0.0f
, 1.0f
);
433 value
[1] = CLAMP(ctx
->Current
.Attrib
[idx
][1], 0.0f
, 1.0f
);
434 value
[2] = CLAMP(ctx
->Current
.Attrib
[idx
][2], 0.0f
, 1.0f
);
435 value
[3] = CLAMP(ctx
->Current
.Attrib
[idx
][3], 0.0f
, 1.0f
);
438 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
442 case STATE_NORMAL_SCALE
:
444 ctx
->_ModelViewInvScale
,
445 ctx
->_ModelViewInvScale
,
446 ctx
->_ModelViewInvScale
,
450 case STATE_TEXRECT_SCALE
:
451 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
452 * Used to convert unnormalized texcoords to normalized texcoords.
455 const int unit
= (int) state
[2];
456 const struct gl_texture_object
*texObj
457 = ctx
->Texture
.Unit
[unit
]._Current
;
459 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
461 (GLfloat
) (1.0 / texImage
->Width
),
462 (GLfloat
) (1.0 / texImage
->Height
),
468 case STATE_FOG_PARAMS_OPTIMIZED
:
469 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
470 * might be more expensive than EX2 on some hw, plus it needs
471 * another constant (e) anyway. Linear fog can now be done with a
473 * linear: fogcoord * -1/(end-start) + end/(end-start)
474 * exp: 2^-(density/ln(2) * fogcoord)
475 * exp2: 2^-((density/(ln(2)^2) * fogcoord)^2)
477 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
478 ? 1.0f
: (GLfloat
)(-1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
));
479 value
[1] = ctx
->Fog
.End
* -value
[0];
480 value
[2] = (GLfloat
)(ctx
->Fog
.Density
* M_LOG2E
); /* M_LOG2E == 1/ln(2) */
481 value
[3] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
);
484 case STATE_POINT_SIZE_CLAMPED
:
486 /* this includes implementation dependent limits, to avoid
487 * another potentially necessary clamp.
488 * Note: for sprites, point smooth (point AA) is ignored
489 * and we'll clamp to MinPointSizeAA and MaxPointSize, because we
490 * expect drivers will want to say their minimum for AA size is 0.0
491 * but for non-AA it's 1.0 (because normal points with size below 1.0
492 * need to get rounded up to 1.0, hence never disappear). GL does
493 * not specify max clamp size for sprites, other than it needs to be
494 * at least as large as max AA size, hence use non-AA size there.
498 if (ctx
->Point
.PointSprite
) {
499 minImplSize
= ctx
->Const
.MinPointSizeAA
;
500 maxImplSize
= ctx
->Const
.MaxPointSize
;
502 else if (ctx
->Point
.SmoothFlag
|| ctx
->Multisample
._Enabled
) {
503 minImplSize
= ctx
->Const
.MinPointSizeAA
;
504 maxImplSize
= ctx
->Const
.MaxPointSizeAA
;
507 minImplSize
= ctx
->Const
.MinPointSize
;
508 maxImplSize
= ctx
->Const
.MaxPointSize
;
510 value
[0] = ctx
->Point
.Size
;
511 value
[1] = ctx
->Point
.MinSize
>= minImplSize
? ctx
->Point
.MinSize
: minImplSize
;
512 value
[2] = ctx
->Point
.MaxSize
<= maxImplSize
? ctx
->Point
.MaxSize
: maxImplSize
;
513 value
[3] = ctx
->Point
.Threshold
;
516 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
518 /* here, state[2] is the light number */
519 /* pre-normalize spot dir */
520 const GLuint ln
= (GLuint
) state
[2];
521 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormSpotDirection
);
522 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
526 case STATE_LIGHT_POSITION
:
528 const GLuint ln
= (GLuint
) state
[2];
529 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
533 case STATE_LIGHT_POSITION_NORMALIZED
:
535 const GLuint ln
= (GLuint
) state
[2];
536 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
537 NORMALIZE_3FV( value
);
541 case STATE_LIGHT_HALF_VECTOR
:
543 const GLuint ln
= (GLuint
) state
[2];
545 /* Compute infinite half angle vector:
546 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
547 * light.EyePosition.w should be 0 for infinite lights.
549 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
551 ADD_3V(value
, p
, ctx
->_EyeZDir
);
552 NORMALIZE_3FV(value
);
558 value
[0] = ctx
->Pixel
.RedScale
;
559 value
[1] = ctx
->Pixel
.GreenScale
;
560 value
[2] = ctx
->Pixel
.BlueScale
;
561 value
[3] = ctx
->Pixel
.AlphaScale
;
565 value
[0] = ctx
->Pixel
.RedBias
;
566 value
[1] = ctx
->Pixel
.GreenBias
;
567 value
[2] = ctx
->Pixel
.BlueBias
;
568 value
[3] = ctx
->Pixel
.AlphaBias
;
572 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
573 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
578 case STATE_FB_WPOS_Y_TRANSFORM
:
579 /* A driver may negate this conditional by using ZW swizzle
580 * instead of XY (based on e.g. some other state). */
581 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
582 /* Identity (XY) followed by flipping Y upside down (ZW). */
586 value
[3] = (GLfloat
) ctx
->DrawBuffer
->Height
;
588 /* Flipping Y upside down (XY) followed by identity (ZW). */
590 value
[1] = (GLfloat
) ctx
->DrawBuffer
->Height
;
596 case STATE_ROT_MATRIX_0
:
598 const int unit
= (int) state
[2];
599 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
600 value
[0] = rotMat22
[0];
601 value
[1] = rotMat22
[2];
607 case STATE_ROT_MATRIX_1
:
609 const int unit
= (int) state
[2];
610 GLfloat
*rotMat22
= ctx
->Texture
.Unit
[unit
].RotMatrix
;
611 value
[0] = rotMat22
[1];
612 value
[1] = rotMat22
[3];
618 /* XXX: make sure new tokens added here are also handled in the
619 * _mesa_program_state_flags() switch, below.
622 /* Unknown state indexes are silently ignored here.
623 * Drivers may do something special.
630 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
637 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
638 * indicate that the given context state may have changed.
639 * The bitmask is used during validation to determine if we need to update
640 * vertex/fragment program parameters (like "state.material.color") when
641 * some GL state has changed.
644 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
648 case STATE_LIGHTPROD
:
649 case STATE_LIGHTMODEL_SCENECOLOR
:
650 /* these can be effected by glColor when colormaterial mode is used */
651 return _NEW_LIGHT
| _NEW_CURRENT_ATTRIB
;
654 case STATE_LIGHTMODEL_AMBIENT
:
659 case STATE_TEXENV_COLOR
:
660 return _NEW_TEXTURE
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
662 case STATE_FOG_COLOR
:
663 return _NEW_FOG
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
664 case STATE_FOG_PARAMS
:
667 case STATE_CLIPPLANE
:
668 return _NEW_TRANSFORM
;
670 case STATE_POINT_SIZE
:
671 case STATE_POINT_ATTENUATION
:
674 case STATE_MODELVIEW_MATRIX
:
675 return _NEW_MODELVIEW
;
676 case STATE_PROJECTION_MATRIX
:
677 return _NEW_PROJECTION
;
678 case STATE_MVP_MATRIX
:
679 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
680 case STATE_TEXTURE_MATRIX
:
681 return _NEW_TEXTURE_MATRIX
;
682 case STATE_PROGRAM_MATRIX
:
683 return _NEW_TRACK_MATRIX
;
685 case STATE_NUM_SAMPLES
:
688 case STATE_DEPTH_RANGE
:
689 return _NEW_VIEWPORT
;
691 case STATE_FRAGMENT_PROGRAM
:
692 case STATE_VERTEX_PROGRAM
:
695 case STATE_NORMAL_SCALE
:
696 return _NEW_MODELVIEW
;
700 case STATE_CURRENT_ATTRIB
:
701 return _NEW_CURRENT_ATTRIB
;
702 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
703 return _NEW_CURRENT_ATTRIB
| _NEW_LIGHT
| _NEW_BUFFERS
;
705 case STATE_NORMAL_SCALE
:
706 return _NEW_MODELVIEW
;
708 case STATE_TEXRECT_SCALE
:
709 case STATE_ROT_MATRIX_0
:
710 case STATE_ROT_MATRIX_1
:
712 case STATE_FOG_PARAMS_OPTIMIZED
:
714 case STATE_POINT_SIZE_CLAMPED
:
715 return _NEW_POINT
| _NEW_MULTISAMPLE
;
716 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
717 case STATE_LIGHT_POSITION
:
718 case STATE_LIGHT_POSITION_NORMALIZED
:
719 case STATE_LIGHT_HALF_VECTOR
:
727 case STATE_FB_WPOS_Y_TRANSFORM
:
731 /* unknown state indexes are silently ignored and
732 * no flag set, since it is handled by the driver.
738 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
745 append(char *dst
, const char *src
)
756 * Convert token 'k' to a string, append it onto 'dst' string.
759 append_token(char *dst
, gl_state_index k
)
763 append(dst
, "material");
766 append(dst
, "light");
768 case STATE_LIGHTMODEL_AMBIENT
:
769 append(dst
, "lightmodel.ambient");
771 case STATE_LIGHTMODEL_SCENECOLOR
:
773 case STATE_LIGHTPROD
:
774 append(dst
, "lightprod");
777 append(dst
, "texgen");
779 case STATE_FOG_COLOR
:
780 append(dst
, "fog.color");
782 case STATE_FOG_PARAMS
:
783 append(dst
, "fog.params");
785 case STATE_CLIPPLANE
:
788 case STATE_POINT_SIZE
:
789 append(dst
, "point.size");
791 case STATE_POINT_ATTENUATION
:
792 append(dst
, "point.attenuation");
794 case STATE_MODELVIEW_MATRIX
:
795 append(dst
, "matrix.modelview");
797 case STATE_PROJECTION_MATRIX
:
798 append(dst
, "matrix.projection");
800 case STATE_MVP_MATRIX
:
801 append(dst
, "matrix.mvp");
803 case STATE_TEXTURE_MATRIX
:
804 append(dst
, "matrix.texture");
806 case STATE_PROGRAM_MATRIX
:
807 append(dst
, "matrix.program");
809 case STATE_MATRIX_INVERSE
:
810 append(dst
, ".inverse");
812 case STATE_MATRIX_TRANSPOSE
:
813 append(dst
, ".transpose");
815 case STATE_MATRIX_INVTRANS
:
816 append(dst
, ".invtrans");
819 append(dst
, ".ambient");
822 append(dst
, ".diffuse");
825 append(dst
, ".specular");
828 append(dst
, ".emission");
830 case STATE_SHININESS
:
831 append(dst
, "lshininess");
833 case STATE_HALF_VECTOR
:
834 append(dst
, ".half");
837 append(dst
, ".position");
839 case STATE_ATTENUATION
:
840 append(dst
, ".attenuation");
842 case STATE_SPOT_DIRECTION
:
843 append(dst
, ".spot.direction");
845 case STATE_SPOT_CUTOFF
:
846 append(dst
, ".spot.cutoff");
848 case STATE_TEXGEN_EYE_S
:
849 append(dst
, ".eye.s");
851 case STATE_TEXGEN_EYE_T
:
852 append(dst
, ".eye.t");
854 case STATE_TEXGEN_EYE_R
:
855 append(dst
, ".eye.r");
857 case STATE_TEXGEN_EYE_Q
:
858 append(dst
, ".eye.q");
860 case STATE_TEXGEN_OBJECT_S
:
861 append(dst
, ".object.s");
863 case STATE_TEXGEN_OBJECT_T
:
864 append(dst
, ".object.t");
866 case STATE_TEXGEN_OBJECT_R
:
867 append(dst
, ".object.r");
869 case STATE_TEXGEN_OBJECT_Q
:
870 append(dst
, ".object.q");
872 case STATE_TEXENV_COLOR
:
873 append(dst
, "texenv");
875 case STATE_NUM_SAMPLES
:
876 append(dst
, "numsamples");
878 case STATE_DEPTH_RANGE
:
879 append(dst
, "depth.range");
881 case STATE_VERTEX_PROGRAM
:
882 case STATE_FRAGMENT_PROGRAM
:
888 append(dst
, "local");
890 /* BEGIN internal state vars */
892 append(dst
, ".internal.");
894 case STATE_CURRENT_ATTRIB
:
895 append(dst
, "current");
897 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
898 append(dst
, "currentAttribMaybeVPClamped");
900 case STATE_NORMAL_SCALE
:
901 append(dst
, "normalScale");
903 case STATE_TEXRECT_SCALE
:
904 append(dst
, "texrectScale");
906 case STATE_FOG_PARAMS_OPTIMIZED
:
907 append(dst
, "fogParamsOptimized");
909 case STATE_POINT_SIZE_CLAMPED
:
910 append(dst
, "pointSizeClamped");
912 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
913 append(dst
, "lightSpotDirNormalized");
915 case STATE_LIGHT_POSITION
:
916 append(dst
, "lightPosition");
918 case STATE_LIGHT_POSITION_NORMALIZED
:
919 append(dst
, "light.position.normalized");
921 case STATE_LIGHT_HALF_VECTOR
:
922 append(dst
, "lightHalfVector");
925 append(dst
, "PTscale");
928 append(dst
, "PTbias");
931 append(dst
, "FbSize");
933 case STATE_FB_WPOS_Y_TRANSFORM
:
934 append(dst
, "FbWposYTransform");
936 case STATE_ROT_MATRIX_0
:
937 append(dst
, "rotMatrixRow0");
939 case STATE_ROT_MATRIX_1
:
940 append(dst
, "rotMatrixRow1");
943 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
944 append(dst
, "driverState");
949 append_face(char *dst
, GLint face
)
952 append(dst
, "front.");
954 append(dst
, "back.");
958 append_index(char *dst
, GLint index
)
961 sprintf(s
, "[%d]", index
);
966 * Make a string from the given state vector.
967 * For example, return "state.matrix.texture[2].inverse".
968 * Use free() to deallocate the string.
971 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
976 append(str
, "state.");
977 append_token(str
, state
[0]);
981 append_face(str
, state
[1]);
982 append_token(str
, state
[2]);
985 append_index(str
, state
[1]); /* light number [i]. */
986 append_token(str
, state
[2]); /* coefficients */
988 case STATE_LIGHTMODEL_AMBIENT
:
989 append(str
, "lightmodel.ambient");
991 case STATE_LIGHTMODEL_SCENECOLOR
:
993 append(str
, "lightmodel.front.scenecolor");
996 append(str
, "lightmodel.back.scenecolor");
999 case STATE_LIGHTPROD
:
1000 append_index(str
, state
[1]); /* light number [i]. */
1001 append_face(str
, state
[2]);
1002 append_token(str
, state
[3]);
1005 append_index(str
, state
[1]); /* tex unit [i] */
1006 append_token(str
, state
[2]); /* plane coef */
1008 case STATE_TEXENV_COLOR
:
1009 append_index(str
, state
[1]); /* tex unit [i] */
1010 append(str
, "color");
1012 case STATE_CLIPPLANE
:
1013 append_index(str
, state
[1]); /* plane [i] */
1014 append(str
, ".plane");
1016 case STATE_MODELVIEW_MATRIX
:
1017 case STATE_PROJECTION_MATRIX
:
1018 case STATE_MVP_MATRIX
:
1019 case STATE_TEXTURE_MATRIX
:
1020 case STATE_PROGRAM_MATRIX
:
1022 /* state[0] = modelview, projection, texture, etc. */
1023 /* state[1] = which texture matrix or program matrix */
1024 /* state[2] = first row to fetch */
1025 /* state[3] = last row to fetch */
1026 /* state[4] = transpose, inverse or invtrans */
1027 const gl_state_index mat
= state
[0];
1028 const GLuint index
= (GLuint
) state
[1];
1029 const GLuint firstRow
= (GLuint
) state
[2];
1030 const GLuint lastRow
= (GLuint
) state
[3];
1031 const gl_state_index modifier
= state
[4];
1033 mat
== STATE_TEXTURE_MATRIX
||
1034 mat
== STATE_PROGRAM_MATRIX
)
1035 append_index(str
, index
);
1037 append_token(str
, modifier
);
1038 if (firstRow
== lastRow
)
1039 sprintf(tmp
, ".row[%d]", firstRow
);
1041 sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
1045 case STATE_POINT_SIZE
:
1047 case STATE_POINT_ATTENUATION
:
1049 case STATE_FOG_PARAMS
:
1051 case STATE_FOG_COLOR
:
1053 case STATE_NUM_SAMPLES
:
1055 case STATE_DEPTH_RANGE
:
1057 case STATE_FRAGMENT_PROGRAM
:
1058 case STATE_VERTEX_PROGRAM
:
1059 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1060 /* state[2] = parameter index */
1061 append_token(str
, state
[1]);
1062 append_index(str
, state
[2]);
1064 case STATE_NORMAL_SCALE
:
1066 case STATE_INTERNAL
:
1067 append_token(str
, state
[1]);
1068 if (state
[1] == STATE_CURRENT_ATTRIB
)
1069 append_index(str
, state
[2]);
1072 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1076 return _mesa_strdup(str
);
1081 * Loop over all the parameters in a parameter list. If the parameter
1082 * is a GL state reference, look up the current value of that state
1083 * variable and put it into the parameter's Value[4] array.
1084 * Other parameter types never change or are explicitly set by the user
1085 * with glUniform() or glProgramParameter(), etc.
1086 * This would be called at glBegin time.
1089 _mesa_load_state_parameters(struct gl_context
*ctx
,
1090 struct gl_program_parameter_list
*paramList
)
1097 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1098 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1099 _mesa_fetch_state(ctx
,
1100 paramList
->Parameters
[i
].StateIndexes
,
1101 ¶mList
->ParameterValues
[i
][0].f
);