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.
33 #include "main/glheader.h"
34 #include "main/context.h"
35 #include "main/blend.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mtypes.h"
39 #include "main/fbobject.h"
40 #include "prog_statevars.h"
41 #include "prog_parameter.h"
42 #include "main/samplerobj.h"
43 #include "main/framebuffer.h"
46 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
50 * Use the list of tokens in the state[] array to find global GL state
51 * and return it in <value>. Usually, four values are returned in <value>
52 * but matrix queries may return as many as 16 values.
53 * This function is used for ARB vertex/fragment programs.
54 * The program parser will produce the state[] values.
57 _mesa_fetch_state(struct gl_context
*ctx
, const gl_state_index state
[],
58 gl_constant_value
*val
)
60 GLfloat
*value
= &val
->f
;
65 /* state[1] is either 0=front or 1=back side */
66 const GLuint face
= (GLuint
) state
[1];
67 const struct gl_material
*mat
= &ctx
->Light
.Material
;
68 assert(face
== 0 || face
== 1);
69 /* we rely on tokens numbered so that _BACK_ == _FRONT_+ 1 */
70 assert(MAT_ATTRIB_FRONT_AMBIENT
+ 1 == MAT_ATTRIB_BACK_AMBIENT
);
71 /* XXX we could get rid of this switch entirely with a little
72 * work in arbprogparse.c's parse_state_single_item().
74 /* state[2] is the material attribute */
77 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+ face
]);
80 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+ face
]);
83 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+ face
]);
86 COPY_4V(value
, mat
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
+ face
]);
89 value
[0] = mat
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ face
][0];
95 _mesa_problem(ctx
, "Invalid material state in fetch_state");
101 /* state[1] is the light number */
102 const GLuint ln
= (GLuint
) state
[1];
103 /* state[2] is the light attribute */
106 COPY_4V(value
, ctx
->Light
.Light
[ln
].Ambient
);
109 COPY_4V(value
, ctx
->Light
.Light
[ln
].Diffuse
);
112 COPY_4V(value
, ctx
->Light
.Light
[ln
].Specular
);
115 COPY_4V(value
, ctx
->Light
.Light
[ln
].EyePosition
);
117 case STATE_ATTENUATION
:
118 value
[0] = ctx
->Light
.Light
[ln
].ConstantAttenuation
;
119 value
[1] = ctx
->Light
.Light
[ln
].LinearAttenuation
;
120 value
[2] = ctx
->Light
.Light
[ln
].QuadraticAttenuation
;
121 value
[3] = ctx
->Light
.Light
[ln
].SpotExponent
;
123 case STATE_SPOT_DIRECTION
:
124 COPY_3V(value
, ctx
->Light
.Light
[ln
].SpotDirection
);
125 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
127 case STATE_SPOT_CUTOFF
:
128 value
[0] = ctx
->Light
.Light
[ln
].SpotCutoff
;
130 case STATE_HALF_VECTOR
:
132 static const GLfloat eye_z
[] = {0, 0, 1};
134 /* Compute infinite half angle vector:
135 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
136 * light.EyePosition.w should be 0 for infinite lights.
138 COPY_3V(p
, ctx
->Light
.Light
[ln
].EyePosition
);
140 ADD_3V(value
, p
, eye_z
);
141 NORMALIZE_3FV(value
);
146 _mesa_problem(ctx
, "Invalid light state in fetch_state");
150 case STATE_LIGHTMODEL_AMBIENT
:
151 COPY_4V(value
, ctx
->Light
.Model
.Ambient
);
153 case STATE_LIGHTMODEL_SCENECOLOR
:
157 for (i
= 0; i
< 3; i
++) {
158 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
159 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
][i
]
160 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_EMISSION
][i
];
162 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
167 for (i
= 0; i
< 3; i
++) {
168 value
[i
] = ctx
->Light
.Model
.Ambient
[i
]
169 * ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_AMBIENT
][i
]
170 + ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_EMISSION
][i
];
172 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
175 case STATE_LIGHTPROD
:
177 const GLuint ln
= (GLuint
) state
[1];
178 const GLuint face
= (GLuint
) state
[2];
180 assert(face
== 0 || face
== 1);
183 for (i
= 0; i
< 3; i
++) {
184 value
[i
] = ctx
->Light
.Light
[ln
].Ambient
[i
] *
185 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][i
];
187 /* [3] = material alpha */
188 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_AMBIENT
+face
][3];
191 for (i
= 0; i
< 3; i
++) {
192 value
[i
] = ctx
->Light
.Light
[ln
].Diffuse
[i
] *
193 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][i
];
195 /* [3] = material alpha */
196 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
+face
][3];
199 for (i
= 0; i
< 3; i
++) {
200 value
[i
] = ctx
->Light
.Light
[ln
].Specular
[i
] *
201 ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][i
];
203 /* [3] = material alpha */
204 value
[3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SPECULAR
+face
][3];
207 _mesa_problem(ctx
, "Invalid lightprod state in fetch_state");
213 /* state[1] is the texture unit */
214 const GLuint unit
= (GLuint
) state
[1];
215 /* state[2] is the texgen attribute */
217 case STATE_TEXGEN_EYE_S
:
218 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.EyePlane
);
220 case STATE_TEXGEN_EYE_T
:
221 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.EyePlane
);
223 case STATE_TEXGEN_EYE_R
:
224 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.EyePlane
);
226 case STATE_TEXGEN_EYE_Q
:
227 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.EyePlane
);
229 case STATE_TEXGEN_OBJECT_S
:
230 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenS
.ObjectPlane
);
232 case STATE_TEXGEN_OBJECT_T
:
233 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenT
.ObjectPlane
);
235 case STATE_TEXGEN_OBJECT_R
:
236 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenR
.ObjectPlane
);
238 case STATE_TEXGEN_OBJECT_Q
:
239 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].GenQ
.ObjectPlane
);
242 _mesa_problem(ctx
, "Invalid texgen state in fetch_state");
246 case STATE_TEXENV_COLOR
:
248 /* state[1] is the texture unit */
249 const GLuint unit
= (GLuint
) state
[1];
250 if (_mesa_get_clamp_fragment_color(ctx
, ctx
->DrawBuffer
))
251 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColor
);
253 COPY_4V(value
, ctx
->Texture
.Unit
[unit
].EnvColorUnclamped
);
256 case STATE_FOG_COLOR
:
257 if (_mesa_get_clamp_fragment_color(ctx
, ctx
->DrawBuffer
))
258 COPY_4V(value
, ctx
->Fog
.Color
);
260 COPY_4V(value
, ctx
->Fog
.ColorUnclamped
);
262 case STATE_FOG_PARAMS
:
263 value
[0] = ctx
->Fog
.Density
;
264 value
[1] = ctx
->Fog
.Start
;
265 value
[2] = ctx
->Fog
.End
;
266 value
[3] = 1.0f
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
268 case STATE_CLIPPLANE
:
270 const GLuint plane
= (GLuint
) state
[1];
271 COPY_4V(value
, ctx
->Transform
.EyeUserPlane
[plane
]);
274 case STATE_POINT_SIZE
:
275 value
[0] = ctx
->Point
.Size
;
276 value
[1] = ctx
->Point
.MinSize
;
277 value
[2] = ctx
->Point
.MaxSize
;
278 value
[3] = ctx
->Point
.Threshold
;
280 case STATE_POINT_ATTENUATION
:
281 value
[0] = ctx
->Point
.Params
[0];
282 value
[1] = ctx
->Point
.Params
[1];
283 value
[2] = ctx
->Point
.Params
[2];
286 case STATE_MODELVIEW_MATRIX
:
287 case STATE_PROJECTION_MATRIX
:
288 case STATE_MVP_MATRIX
:
289 case STATE_TEXTURE_MATRIX
:
290 case STATE_PROGRAM_MATRIX
:
292 /* state[0] = modelview, projection, texture, etc. */
293 /* state[1] = which texture matrix or program matrix */
294 /* state[2] = first row to fetch */
295 /* state[3] = last row to fetch */
296 /* state[4] = transpose, inverse or invtrans */
297 const GLmatrix
*matrix
;
298 const gl_state_index mat
= state
[0];
299 const GLuint index
= (GLuint
) state
[1];
300 const GLuint firstRow
= (GLuint
) state
[2];
301 const GLuint lastRow
= (GLuint
) state
[3];
302 const gl_state_index modifier
= state
[4];
305 assert(firstRow
< 4);
307 if (mat
== STATE_MODELVIEW_MATRIX
) {
308 matrix
= ctx
->ModelviewMatrixStack
.Top
;
310 else if (mat
== STATE_PROJECTION_MATRIX
) {
311 matrix
= ctx
->ProjectionMatrixStack
.Top
;
313 else if (mat
== STATE_MVP_MATRIX
) {
314 matrix
= &ctx
->_ModelProjectMatrix
;
316 else if (mat
== STATE_TEXTURE_MATRIX
) {
317 assert(index
< ARRAY_SIZE(ctx
->TextureMatrixStack
));
318 matrix
= ctx
->TextureMatrixStack
[index
].Top
;
320 else if (mat
== STATE_PROGRAM_MATRIX
) {
321 assert(index
< ARRAY_SIZE(ctx
->ProgramMatrixStack
));
322 matrix
= ctx
->ProgramMatrixStack
[index
].Top
;
325 _mesa_problem(ctx
, "Bad matrix name in _mesa_fetch_state()");
328 if (modifier
== STATE_MATRIX_INVERSE
||
329 modifier
== STATE_MATRIX_INVTRANS
) {
330 /* Be sure inverse is up to date:
332 _math_matrix_analyse( (GLmatrix
*) matrix
);
338 if (modifier
== STATE_MATRIX_TRANSPOSE
||
339 modifier
== STATE_MATRIX_INVTRANS
) {
340 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
341 value
[i
++] = m
[row
* 4 + 0];
342 value
[i
++] = m
[row
* 4 + 1];
343 value
[i
++] = m
[row
* 4 + 2];
344 value
[i
++] = m
[row
* 4 + 3];
348 for (i
= 0, row
= firstRow
; row
<= lastRow
; row
++) {
349 value
[i
++] = m
[row
+ 0];
350 value
[i
++] = m
[row
+ 4];
351 value
[i
++] = m
[row
+ 8];
352 value
[i
++] = m
[row
+ 12];
357 case STATE_NUM_SAMPLES
:
358 val
[0].i
= MAX2(1, _mesa_geometric_samples(ctx
->DrawBuffer
));
360 case STATE_DEPTH_RANGE
:
361 value
[0] = ctx
->ViewportArray
[0].Near
; /* near */
362 value
[1] = ctx
->ViewportArray
[0].Far
; /* far */
363 value
[2] = ctx
->ViewportArray
[0].Far
- ctx
->ViewportArray
[0].Near
; /* far - near */
366 case STATE_FRAGMENT_PROGRAM
:
368 /* state[1] = {STATE_ENV, STATE_LOCAL} */
369 /* state[2] = parameter index */
370 const int idx
= (int) state
[2];
373 COPY_4V(value
, ctx
->FragmentProgram
.Parameters
[idx
]);
376 if (!ctx
->FragmentProgram
.Current
->Base
.LocalParams
) {
377 ctx
->FragmentProgram
.Current
->Base
.LocalParams
=
378 calloc(MAX_PROGRAM_LOCAL_PARAMS
, sizeof(float[4]));
379 if (!ctx
->FragmentProgram
.Current
->Base
.LocalParams
)
383 COPY_4V(value
, ctx
->FragmentProgram
.Current
->Base
.LocalParams
[idx
]);
386 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
392 case STATE_VERTEX_PROGRAM
:
394 /* state[1] = {STATE_ENV, STATE_LOCAL} */
395 /* state[2] = parameter index */
396 const int idx
= (int) state
[2];
399 COPY_4V(value
, ctx
->VertexProgram
.Parameters
[idx
]);
402 if (!ctx
->VertexProgram
.Current
->LocalParams
) {
403 ctx
->VertexProgram
.Current
->LocalParams
=
404 calloc(MAX_PROGRAM_LOCAL_PARAMS
, sizeof(float[4]));
405 if (!ctx
->VertexProgram
.Current
->LocalParams
)
409 COPY_4V(value
, ctx
->VertexProgram
.Current
->LocalParams
[idx
]);
412 _mesa_problem(ctx
, "Bad state switch in _mesa_fetch_state()");
418 case STATE_NORMAL_SCALE
:
419 ASSIGN_4V(value
, ctx
->_ModelViewInvScale
, 0, 0, 1);
424 case STATE_CURRENT_ATTRIB
:
426 const GLuint idx
= (GLuint
) state
[2];
427 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
431 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
433 const GLuint idx
= (GLuint
) state
[2];
434 if(ctx
->Light
._ClampVertexColor
&&
435 (idx
== VERT_ATTRIB_COLOR0
||
436 idx
== VERT_ATTRIB_COLOR1
)) {
437 value
[0] = CLAMP(ctx
->Current
.Attrib
[idx
][0], 0.0f
, 1.0f
);
438 value
[1] = CLAMP(ctx
->Current
.Attrib
[idx
][1], 0.0f
, 1.0f
);
439 value
[2] = CLAMP(ctx
->Current
.Attrib
[idx
][2], 0.0f
, 1.0f
);
440 value
[3] = CLAMP(ctx
->Current
.Attrib
[idx
][3], 0.0f
, 1.0f
);
443 COPY_4V(value
, ctx
->Current
.Attrib
[idx
]);
447 case STATE_NORMAL_SCALE
:
449 ctx
->_ModelViewInvScale
,
450 ctx
->_ModelViewInvScale
,
451 ctx
->_ModelViewInvScale
,
455 case STATE_TEXRECT_SCALE
:
456 /* Value = { 1/texWidth, 1/texHeight, 0, 1 }.
457 * Used to convert unnormalized texcoords to normalized texcoords.
460 const int unit
= (int) state
[2];
461 const struct gl_texture_object
*texObj
462 = ctx
->Texture
.Unit
[unit
]._Current
;
464 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
466 (GLfloat
) (1.0 / texImage
->Width
),
467 (GLfloat
) (1.0 / texImage
->Height
),
473 case STATE_FOG_PARAMS_OPTIMIZED
:
474 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
475 * might be more expensive than EX2 on some hw, plus it needs
476 * another constant (e) anyway. Linear fog can now be done with a
478 * linear: fogcoord * -1/(end-start) + end/(end-start)
479 * exp: 2^-(density/ln(2) * fogcoord)
480 * exp2: 2^-((density/(sqrt(ln(2))) * fogcoord)^2)
482 value
[0] = (ctx
->Fog
.End
== ctx
->Fog
.Start
)
483 ? 1.0f
: (GLfloat
)(-1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
));
484 value
[1] = ctx
->Fog
.End
* -value
[0];
485 value
[2] = (GLfloat
)(ctx
->Fog
.Density
* M_LOG2E
); /* M_LOG2E == 1/ln(2) */
486 value
[3] = (GLfloat
)(ctx
->Fog
.Density
* ONE_DIV_SQRT_LN2
);
489 case STATE_POINT_SIZE_CLAMPED
:
491 /* this includes implementation dependent limits, to avoid
492 * another potentially necessary clamp.
493 * Note: for sprites, point smooth (point AA) is ignored
494 * and we'll clamp to MinPointSizeAA and MaxPointSize, because we
495 * expect drivers will want to say their minimum for AA size is 0.0
496 * but for non-AA it's 1.0 (because normal points with size below 1.0
497 * need to get rounded up to 1.0, hence never disappear). GL does
498 * not specify max clamp size for sprites, other than it needs to be
499 * at least as large as max AA size, hence use non-AA size there.
503 if (ctx
->Point
.PointSprite
) {
504 minImplSize
= ctx
->Const
.MinPointSizeAA
;
505 maxImplSize
= ctx
->Const
.MaxPointSize
;
507 else if (ctx
->Point
.SmoothFlag
|| _mesa_is_multisample_enabled(ctx
)) {
508 minImplSize
= ctx
->Const
.MinPointSizeAA
;
509 maxImplSize
= ctx
->Const
.MaxPointSizeAA
;
512 minImplSize
= ctx
->Const
.MinPointSize
;
513 maxImplSize
= ctx
->Const
.MaxPointSize
;
515 value
[0] = ctx
->Point
.Size
;
516 value
[1] = ctx
->Point
.MinSize
>= minImplSize
? ctx
->Point
.MinSize
: minImplSize
;
517 value
[2] = ctx
->Point
.MaxSize
<= maxImplSize
? ctx
->Point
.MaxSize
: maxImplSize
;
518 value
[3] = ctx
->Point
.Threshold
;
521 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
523 /* here, state[2] is the light number */
524 /* pre-normalize spot dir */
525 const GLuint ln
= (GLuint
) state
[2];
526 COPY_3V(value
, ctx
->Light
.Light
[ln
]._NormSpotDirection
);
527 value
[3] = ctx
->Light
.Light
[ln
]._CosCutoff
;
531 case STATE_LIGHT_POSITION
:
533 const GLuint ln
= (GLuint
) state
[2];
534 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
538 case STATE_LIGHT_POSITION_NORMALIZED
:
540 const GLuint ln
= (GLuint
) state
[2];
541 COPY_4V(value
, ctx
->Light
.Light
[ln
]._Position
);
542 NORMALIZE_3FV( value
);
546 case STATE_LIGHT_HALF_VECTOR
:
548 const GLuint ln
= (GLuint
) state
[2];
550 /* Compute infinite half angle vector:
551 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
552 * light.EyePosition.w should be 0 for infinite lights.
554 COPY_3V(p
, ctx
->Light
.Light
[ln
]._Position
);
556 ADD_3V(value
, p
, ctx
->_EyeZDir
);
557 NORMALIZE_3FV(value
);
563 value
[0] = ctx
->Pixel
.RedScale
;
564 value
[1] = ctx
->Pixel
.GreenScale
;
565 value
[2] = ctx
->Pixel
.BlueScale
;
566 value
[3] = ctx
->Pixel
.AlphaScale
;
570 value
[0] = ctx
->Pixel
.RedBias
;
571 value
[1] = ctx
->Pixel
.GreenBias
;
572 value
[2] = ctx
->Pixel
.BlueBias
;
573 value
[3] = ctx
->Pixel
.AlphaBias
;
577 value
[0] = (GLfloat
) (ctx
->DrawBuffer
->Width
- 1);
578 value
[1] = (GLfloat
) (ctx
->DrawBuffer
->Height
- 1);
583 case STATE_FB_WPOS_Y_TRANSFORM
:
584 /* A driver may negate this conditional by using ZW swizzle
585 * instead of XY (based on e.g. some other state). */
586 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
587 /* Identity (XY) followed by flipping Y upside down (ZW). */
591 value
[3] = (GLfloat
) ctx
->DrawBuffer
->Height
;
593 /* Flipping Y upside down (XY) followed by identity (ZW). */
595 value
[1] = (GLfloat
) ctx
->DrawBuffer
->Height
;
601 case STATE_TCS_PATCH_VERTICES_IN
:
602 val
[0].i
= ctx
->TessCtrlProgram
.patch_vertices
;
605 case STATE_TES_PATCH_VERTICES_IN
:
606 if (ctx
->TessCtrlProgram
._Current
)
607 val
[0].i
= ctx
->TessCtrlProgram
._Current
->info
.tcs
.vertices_out
;
609 val
[0].i
= ctx
->TessCtrlProgram
.patch_vertices
;
612 case STATE_ADVANCED_BLENDING_MODE
:
613 val
[0].i
= ctx
->Color
.BlendEnabled
? ctx
->Color
._AdvancedBlendMode
: 0;
616 /* XXX: make sure new tokens added here are also handled in the
617 * _mesa_program_state_flags() switch, below.
620 /* Unknown state indexes are silently ignored here.
621 * Drivers may do something special.
628 _mesa_problem(ctx
, "Invalid state in _mesa_fetch_state");
635 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
636 * indicate that the given context state may have changed.
637 * The bitmask is used during validation to determine if we need to update
638 * vertex/fragment program parameters (like "state.material.color") when
639 * some GL state has changed.
642 _mesa_program_state_flags(const gl_state_index state
[STATE_LENGTH
])
646 case STATE_LIGHTPROD
:
647 case STATE_LIGHTMODEL_SCENECOLOR
:
648 /* these can be effected by glColor when colormaterial mode is used */
649 return _NEW_LIGHT
| _NEW_CURRENT_ATTRIB
;
652 case STATE_LIGHTMODEL_AMBIENT
:
657 case STATE_TEXENV_COLOR
:
658 return _NEW_TEXTURE
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
660 case STATE_FOG_COLOR
:
661 return _NEW_FOG
| _NEW_BUFFERS
| _NEW_FRAG_CLAMP
;
662 case STATE_FOG_PARAMS
:
665 case STATE_CLIPPLANE
:
666 return _NEW_TRANSFORM
;
668 case STATE_POINT_SIZE
:
669 case STATE_POINT_ATTENUATION
:
672 case STATE_MODELVIEW_MATRIX
:
673 return _NEW_MODELVIEW
;
674 case STATE_PROJECTION_MATRIX
:
675 return _NEW_PROJECTION
;
676 case STATE_MVP_MATRIX
:
677 return _NEW_MODELVIEW
| _NEW_PROJECTION
;
678 case STATE_TEXTURE_MATRIX
:
679 return _NEW_TEXTURE_MATRIX
;
680 case STATE_PROGRAM_MATRIX
:
681 return _NEW_TRACK_MATRIX
;
683 case STATE_NUM_SAMPLES
:
686 case STATE_DEPTH_RANGE
:
687 return _NEW_VIEWPORT
;
689 case STATE_FRAGMENT_PROGRAM
:
690 case STATE_VERTEX_PROGRAM
:
693 case STATE_NORMAL_SCALE
:
694 return _NEW_MODELVIEW
;
698 case STATE_CURRENT_ATTRIB
:
699 return _NEW_CURRENT_ATTRIB
;
700 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
701 return _NEW_CURRENT_ATTRIB
| _NEW_LIGHT
| _NEW_BUFFERS
;
703 case STATE_NORMAL_SCALE
:
704 return _NEW_MODELVIEW
;
706 case STATE_TEXRECT_SCALE
:
708 case STATE_FOG_PARAMS_OPTIMIZED
:
710 case STATE_POINT_SIZE_CLAMPED
:
711 return _NEW_POINT
| _NEW_MULTISAMPLE
;
712 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
713 case STATE_LIGHT_POSITION
:
714 case STATE_LIGHT_POSITION_NORMALIZED
:
715 case STATE_LIGHT_HALF_VECTOR
:
723 case STATE_FB_WPOS_Y_TRANSFORM
:
726 case STATE_ADVANCED_BLENDING_MODE
:
730 /* unknown state indexes are silently ignored and
731 * no flag set, since it is handled by the driver.
737 _mesa_problem(NULL
, "unexpected state[0] in make_state_flags()");
744 append(char *dst
, const char *src
)
755 * Convert token 'k' to a string, append it onto 'dst' string.
758 append_token(char *dst
, gl_state_index k
)
762 append(dst
, "material");
765 append(dst
, "light");
767 case STATE_LIGHTMODEL_AMBIENT
:
768 append(dst
, "lightmodel.ambient");
770 case STATE_LIGHTMODEL_SCENECOLOR
:
772 case STATE_LIGHTPROD
:
773 append(dst
, "lightprod");
776 append(dst
, "texgen");
778 case STATE_FOG_COLOR
:
779 append(dst
, "fog.color");
781 case STATE_FOG_PARAMS
:
782 append(dst
, "fog.params");
784 case STATE_CLIPPLANE
:
787 case STATE_POINT_SIZE
:
788 append(dst
, "point.size");
790 case STATE_POINT_ATTENUATION
:
791 append(dst
, "point.attenuation");
793 case STATE_MODELVIEW_MATRIX
:
794 append(dst
, "matrix.modelview");
796 case STATE_PROJECTION_MATRIX
:
797 append(dst
, "matrix.projection");
799 case STATE_MVP_MATRIX
:
800 append(dst
, "matrix.mvp");
802 case STATE_TEXTURE_MATRIX
:
803 append(dst
, "matrix.texture");
805 case STATE_PROGRAM_MATRIX
:
806 append(dst
, "matrix.program");
808 case STATE_MATRIX_INVERSE
:
809 append(dst
, ".inverse");
811 case STATE_MATRIX_TRANSPOSE
:
812 append(dst
, ".transpose");
814 case STATE_MATRIX_INVTRANS
:
815 append(dst
, ".invtrans");
818 append(dst
, ".ambient");
821 append(dst
, ".diffuse");
824 append(dst
, ".specular");
827 append(dst
, ".emission");
829 case STATE_SHININESS
:
830 append(dst
, "lshininess");
832 case STATE_HALF_VECTOR
:
833 append(dst
, ".half");
836 append(dst
, ".position");
838 case STATE_ATTENUATION
:
839 append(dst
, ".attenuation");
841 case STATE_SPOT_DIRECTION
:
842 append(dst
, ".spot.direction");
844 case STATE_SPOT_CUTOFF
:
845 append(dst
, ".spot.cutoff");
847 case STATE_TEXGEN_EYE_S
:
848 append(dst
, ".eye.s");
850 case STATE_TEXGEN_EYE_T
:
851 append(dst
, ".eye.t");
853 case STATE_TEXGEN_EYE_R
:
854 append(dst
, ".eye.r");
856 case STATE_TEXGEN_EYE_Q
:
857 append(dst
, ".eye.q");
859 case STATE_TEXGEN_OBJECT_S
:
860 append(dst
, ".object.s");
862 case STATE_TEXGEN_OBJECT_T
:
863 append(dst
, ".object.t");
865 case STATE_TEXGEN_OBJECT_R
:
866 append(dst
, ".object.r");
868 case STATE_TEXGEN_OBJECT_Q
:
869 append(dst
, ".object.q");
871 case STATE_TEXENV_COLOR
:
872 append(dst
, "texenv");
874 case STATE_NUM_SAMPLES
:
875 append(dst
, "numsamples");
877 case STATE_DEPTH_RANGE
:
878 append(dst
, "depth.range");
880 case STATE_VERTEX_PROGRAM
:
881 case STATE_FRAGMENT_PROGRAM
:
887 append(dst
, "local");
889 /* BEGIN internal state vars */
891 append(dst
, ".internal.");
893 case STATE_CURRENT_ATTRIB
:
894 append(dst
, "current");
896 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED
:
897 append(dst
, "currentAttribMaybeVPClamped");
899 case STATE_NORMAL_SCALE
:
900 append(dst
, "normalScale");
902 case STATE_TEXRECT_SCALE
:
903 append(dst
, "texrectScale");
905 case STATE_FOG_PARAMS_OPTIMIZED
:
906 append(dst
, "fogParamsOptimized");
908 case STATE_POINT_SIZE_CLAMPED
:
909 append(dst
, "pointSizeClamped");
911 case STATE_LIGHT_SPOT_DIR_NORMALIZED
:
912 append(dst
, "lightSpotDirNormalized");
914 case STATE_LIGHT_POSITION
:
915 append(dst
, "lightPosition");
917 case STATE_LIGHT_POSITION_NORMALIZED
:
918 append(dst
, "light.position.normalized");
920 case STATE_LIGHT_HALF_VECTOR
:
921 append(dst
, "lightHalfVector");
924 append(dst
, "PTscale");
927 append(dst
, "PTbias");
930 append(dst
, "FbSize");
932 case STATE_FB_WPOS_Y_TRANSFORM
:
933 append(dst
, "FbWposYTransform");
935 case STATE_ADVANCED_BLENDING_MODE
:
936 append(dst
, "AdvancedBlendingMode");
939 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
940 append(dst
, "driverState");
945 append_face(char *dst
, GLint face
)
948 append(dst
, "front.");
950 append(dst
, "back.");
954 append_index(char *dst
, GLint index
)
957 sprintf(s
, "[%d]", index
);
962 * Make a string from the given state vector.
963 * For example, return "state.matrix.texture[2].inverse".
964 * Use free() to deallocate the string.
967 _mesa_program_state_string(const gl_state_index state
[STATE_LENGTH
])
972 append(str
, "state.");
973 append_token(str
, state
[0]);
977 append_face(str
, state
[1]);
978 append_token(str
, state
[2]);
981 append_index(str
, state
[1]); /* light number [i]. */
982 append_token(str
, state
[2]); /* coefficients */
984 case STATE_LIGHTMODEL_AMBIENT
:
985 append(str
, "lightmodel.ambient");
987 case STATE_LIGHTMODEL_SCENECOLOR
:
989 append(str
, "lightmodel.front.scenecolor");
992 append(str
, "lightmodel.back.scenecolor");
995 case STATE_LIGHTPROD
:
996 append_index(str
, state
[1]); /* light number [i]. */
997 append_face(str
, state
[2]);
998 append_token(str
, state
[3]);
1001 append_index(str
, state
[1]); /* tex unit [i] */
1002 append_token(str
, state
[2]); /* plane coef */
1004 case STATE_TEXENV_COLOR
:
1005 append_index(str
, state
[1]); /* tex unit [i] */
1006 append(str
, "color");
1008 case STATE_CLIPPLANE
:
1009 append_index(str
, state
[1]); /* plane [i] */
1010 append(str
, ".plane");
1012 case STATE_MODELVIEW_MATRIX
:
1013 case STATE_PROJECTION_MATRIX
:
1014 case STATE_MVP_MATRIX
:
1015 case STATE_TEXTURE_MATRIX
:
1016 case STATE_PROGRAM_MATRIX
:
1018 /* state[0] = modelview, projection, texture, etc. */
1019 /* state[1] = which texture matrix or program matrix */
1020 /* state[2] = first row to fetch */
1021 /* state[3] = last row to fetch */
1022 /* state[4] = transpose, inverse or invtrans */
1023 const gl_state_index mat
= state
[0];
1024 const GLuint index
= (GLuint
) state
[1];
1025 const GLuint firstRow
= (GLuint
) state
[2];
1026 const GLuint lastRow
= (GLuint
) state
[3];
1027 const gl_state_index modifier
= state
[4];
1029 mat
== STATE_TEXTURE_MATRIX
||
1030 mat
== STATE_PROGRAM_MATRIX
)
1031 append_index(str
, index
);
1033 append_token(str
, modifier
);
1034 if (firstRow
== lastRow
)
1035 sprintf(tmp
, ".row[%d]", firstRow
);
1037 sprintf(tmp
, ".row[%d..%d]", firstRow
, lastRow
);
1041 case STATE_POINT_SIZE
:
1043 case STATE_POINT_ATTENUATION
:
1045 case STATE_FOG_PARAMS
:
1047 case STATE_FOG_COLOR
:
1049 case STATE_NUM_SAMPLES
:
1051 case STATE_DEPTH_RANGE
:
1053 case STATE_FRAGMENT_PROGRAM
:
1054 case STATE_VERTEX_PROGRAM
:
1055 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1056 /* state[2] = parameter index */
1057 append_token(str
, state
[1]);
1058 append_index(str
, state
[2]);
1060 case STATE_NORMAL_SCALE
:
1062 case STATE_INTERNAL
:
1063 append_token(str
, state
[1]);
1064 if (state
[1] == STATE_CURRENT_ATTRIB
)
1065 append_index(str
, state
[2]);
1068 _mesa_problem(NULL
, "Invalid state in _mesa_program_state_string");
1077 * Loop over all the parameters in a parameter list. If the parameter
1078 * is a GL state reference, look up the current value of that state
1079 * variable and put it into the parameter's Value[4] array.
1080 * Other parameter types never change or are explicitly set by the user
1081 * with glUniform() or glProgramParameter(), etc.
1082 * This would be called at glBegin time.
1085 _mesa_load_state_parameters(struct gl_context
*ctx
,
1086 struct gl_program_parameter_list
*paramList
)
1093 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
1094 if (paramList
->Parameters
[i
].Type
== PROGRAM_STATE_VAR
) {
1095 _mesa_fetch_state(ctx
,
1096 paramList
->Parameters
[i
].StateIndexes
,
1097 ¶mList
->ParameterValues
[i
][0]);