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_index16 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.FixedFuncUnit[unit].GenS.EyePlane);
220 case STATE_TEXGEN_EYE_T:
221 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenT.EyePlane);
223 case STATE_TEXGEN_EYE_R:
224 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenR.EyePlane);
226 case STATE_TEXGEN_EYE_Q:
227 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenQ.EyePlane);
229 case STATE_TEXGEN_OBJECT_S:
230 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenS.ObjectPlane);
232 case STATE_TEXGEN_OBJECT_T:
233 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenT.ObjectPlane);
235 case STATE_TEXGEN_OBJECT_R:
236 COPY_4V(value, ctx->Texture.FixedFuncUnit[unit].GenR.ObjectPlane);
238 case STATE_TEXGEN_OBJECT_Q:
239 COPY_4V(value, ctx->Texture.FixedFuncUnit[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.FixedFuncUnit[unit].EnvColor);
253 COPY_4V(value, ctx->Texture.FixedFuncUnit[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->arb.LocalParams) {
377 ctx->FragmentProgram.Current->arb.LocalParams =
378 rzalloc_array_size(ctx->FragmentProgram.Current,
380 MAX_PROGRAM_LOCAL_PARAMS);
381 if (!ctx->FragmentProgram.Current->arb.LocalParams)
386 ctx->FragmentProgram.Current->arb.LocalParams[idx]);
389 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
395 case STATE_VERTEX_PROGRAM:
397 /* state[1] = {STATE_ENV, STATE_LOCAL} */
398 /* state[2] = parameter index */
399 const int idx = (int) state[2];
402 COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
405 if (!ctx->VertexProgram.Current->arb.LocalParams) {
406 ctx->VertexProgram.Current->arb.LocalParams =
407 rzalloc_array_size(ctx->VertexProgram.Current,
409 MAX_PROGRAM_LOCAL_PARAMS);
410 if (!ctx->VertexProgram.Current->arb.LocalParams)
415 ctx->VertexProgram.Current->arb.LocalParams[idx]);
418 _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
424 case STATE_NORMAL_SCALE:
425 ASSIGN_4V(value, ctx->_ModelViewInvScaleEyespace, 0, 0, 1);
430 case STATE_CURRENT_ATTRIB:
432 const GLuint idx = (GLuint) state[2];
433 COPY_4V(value, ctx->Current.Attrib[idx]);
437 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
439 const GLuint idx = (GLuint) state[2];
440 if(ctx->Light._ClampVertexColor &&
441 (idx == VERT_ATTRIB_COLOR0 ||
442 idx == VERT_ATTRIB_COLOR1)) {
443 value[0] = CLAMP(ctx->Current.Attrib[idx][0], 0.0f, 1.0f);
444 value[1] = CLAMP(ctx->Current.Attrib[idx][1], 0.0f, 1.0f);
445 value[2] = CLAMP(ctx->Current.Attrib[idx][2], 0.0f, 1.0f);
446 value[3] = CLAMP(ctx->Current.Attrib[idx][3], 0.0f, 1.0f);
449 COPY_4V(value, ctx->Current.Attrib[idx]);
453 case STATE_NORMAL_SCALE:
455 ctx->_ModelViewInvScale,
456 ctx->_ModelViewInvScale,
457 ctx->_ModelViewInvScale,
461 case STATE_FOG_PARAMS_OPTIMIZED:
462 /* for simpler per-vertex/pixel fog calcs. POW (for EXP/EXP2 fog)
463 * might be more expensive than EX2 on some hw, plus it needs
464 * another constant (e) anyway. Linear fog can now be done with a
466 * linear: fogcoord * -1/(end-start) + end/(end-start)
467 * exp: 2^-(density/ln(2) * fogcoord)
468 * exp2: 2^-((density/(sqrt(ln(2))) * fogcoord)^2)
470 value[0] = (ctx->Fog.End == ctx->Fog.Start)
471 ? 1.0f : (GLfloat)(-1.0F / (ctx->Fog.End - ctx->Fog.Start));
472 value[1] = ctx->Fog.End * -value[0];
473 value[2] = (GLfloat)(ctx->Fog.Density * M_LOG2E); /* M_LOG2E == 1/ln(2) */
474 value[3] = (GLfloat)(ctx->Fog.Density * ONE_DIV_SQRT_LN2);
477 case STATE_POINT_SIZE_CLAMPED:
479 /* this includes implementation dependent limits, to avoid
480 * another potentially necessary clamp.
481 * Note: for sprites, point smooth (point AA) is ignored
482 * and we'll clamp to MinPointSizeAA and MaxPointSize, because we
483 * expect drivers will want to say their minimum for AA size is 0.0
484 * but for non-AA it's 1.0 (because normal points with size below 1.0
485 * need to get rounded up to 1.0, hence never disappear). GL does
486 * not specify max clamp size for sprites, other than it needs to be
487 * at least as large as max AA size, hence use non-AA size there.
491 if (ctx->Point.PointSprite) {
492 minImplSize = ctx->Const.MinPointSizeAA;
493 maxImplSize = ctx->Const.MaxPointSize;
495 else if (ctx->Point.SmoothFlag || _mesa_is_multisample_enabled(ctx)) {
496 minImplSize = ctx->Const.MinPointSizeAA;
497 maxImplSize = ctx->Const.MaxPointSizeAA;
500 minImplSize = ctx->Const.MinPointSize;
501 maxImplSize = ctx->Const.MaxPointSize;
503 value[0] = ctx->Point.Size;
504 value[1] = ctx->Point.MinSize >= minImplSize ? ctx->Point.MinSize : minImplSize;
505 value[2] = ctx->Point.MaxSize <= maxImplSize ? ctx->Point.MaxSize : maxImplSize;
506 value[3] = ctx->Point.Threshold;
509 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
511 /* here, state[2] is the light number */
512 /* pre-normalize spot dir */
513 const GLuint ln = (GLuint) state[2];
514 COPY_3V(value, ctx->Light.Light[ln]._NormSpotDirection);
515 value[3] = ctx->Light.Light[ln]._CosCutoff;
519 case STATE_LIGHT_POSITION:
521 const GLuint ln = (GLuint) state[2];
522 COPY_4V(value, ctx->Light.Light[ln]._Position);
526 case STATE_LIGHT_POSITION_NORMALIZED:
528 const GLuint ln = (GLuint) state[2];
529 COPY_4V(value, ctx->Light.Light[ln]._Position);
530 NORMALIZE_3FV( value );
534 case STATE_LIGHT_HALF_VECTOR:
536 const GLuint ln = (GLuint) state[2];
538 /* Compute infinite half angle vector:
539 * halfVector = normalize(normalize(lightPos) + (0, 0, 1))
540 * light.EyePosition.w should be 0 for infinite lights.
542 COPY_3V(p, ctx->Light.Light[ln]._Position);
544 ADD_3V(value, p, ctx->_EyeZDir);
545 NORMALIZE_3FV(value);
551 value[0] = ctx->Pixel.RedScale;
552 value[1] = ctx->Pixel.GreenScale;
553 value[2] = ctx->Pixel.BlueScale;
554 value[3] = ctx->Pixel.AlphaScale;
558 value[0] = ctx->Pixel.RedBias;
559 value[1] = ctx->Pixel.GreenBias;
560 value[2] = ctx->Pixel.BlueBias;
561 value[3] = ctx->Pixel.AlphaBias;
565 value[0] = (GLfloat) (ctx->DrawBuffer->Width - 1);
566 value[1] = (GLfloat) (ctx->DrawBuffer->Height - 1);
571 case STATE_FB_WPOS_Y_TRANSFORM:
572 /* A driver may negate this conditional by using ZW swizzle
573 * instead of XY (based on e.g. some other state). */
574 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
575 /* Identity (XY) followed by flipping Y upside down (ZW). */
579 value[3] = (GLfloat) ctx->DrawBuffer->Height;
581 /* Flipping Y upside down (XY) followed by identity (ZW). */
583 value[1] = (GLfloat) ctx->DrawBuffer->Height;
589 case STATE_TCS_PATCH_VERTICES_IN:
590 val[0].i = ctx->TessCtrlProgram.patch_vertices;
593 case STATE_TES_PATCH_VERTICES_IN:
594 if (ctx->TessCtrlProgram._Current)
595 val[0].i = ctx->TessCtrlProgram._Current->info.tess.tcs_vertices_out;
597 val[0].i = ctx->TessCtrlProgram.patch_vertices;
600 case STATE_ADVANCED_BLENDING_MODE:
601 val[0].i = _mesa_get_advanced_blend_sh_constant(
602 ctx->Color.BlendEnabled, ctx->Color._AdvancedBlendMode);
605 /* XXX: make sure new tokens added here are also handled in the
606 * _mesa_program_state_flags() switch, below.
609 /* Unknown state indexes are silently ignored here.
610 * Drivers may do something special.
617 _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
624 * Return a bitmask of the Mesa state flags (_NEW_* values) which would
625 * indicate that the given context state may have changed.
626 * The bitmask is used during validation to determine if we need to update
627 * vertex/fragment program parameters (like "state.material.color") when
628 * some GL state has changed.
631 _mesa_program_state_flags(const gl_state_index16 state[STATE_LENGTH])
635 case STATE_LIGHTPROD:
636 case STATE_LIGHTMODEL_SCENECOLOR:
637 /* these can be effected by glColor when colormaterial mode is used */
638 return _NEW_LIGHT | _NEW_CURRENT_ATTRIB;
641 case STATE_LIGHTMODEL_AMBIENT:
645 return _NEW_TEXTURE_STATE;
646 case STATE_TEXENV_COLOR:
647 return _NEW_TEXTURE_STATE | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
649 case STATE_FOG_COLOR:
650 return _NEW_FOG | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
651 case STATE_FOG_PARAMS:
654 case STATE_CLIPPLANE:
655 return _NEW_TRANSFORM;
657 case STATE_POINT_SIZE:
658 case STATE_POINT_ATTENUATION:
661 case STATE_MODELVIEW_MATRIX:
662 return _NEW_MODELVIEW;
663 case STATE_PROJECTION_MATRIX:
664 return _NEW_PROJECTION;
665 case STATE_MVP_MATRIX:
666 return _NEW_MODELVIEW | _NEW_PROJECTION;
667 case STATE_TEXTURE_MATRIX:
668 return _NEW_TEXTURE_MATRIX;
669 case STATE_PROGRAM_MATRIX:
670 return _NEW_TRACK_MATRIX;
672 case STATE_NUM_SAMPLES:
675 case STATE_DEPTH_RANGE:
676 return _NEW_VIEWPORT;
678 case STATE_FRAGMENT_PROGRAM:
679 case STATE_VERTEX_PROGRAM:
682 case STATE_NORMAL_SCALE:
683 return _NEW_MODELVIEW;
687 case STATE_CURRENT_ATTRIB:
688 return _NEW_CURRENT_ATTRIB;
689 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
690 return _NEW_CURRENT_ATTRIB | _NEW_LIGHT | _NEW_BUFFERS;
692 case STATE_NORMAL_SCALE:
693 return _NEW_MODELVIEW;
695 case STATE_FOG_PARAMS_OPTIMIZED:
697 case STATE_POINT_SIZE_CLAMPED:
698 return _NEW_POINT | _NEW_MULTISAMPLE;
699 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
700 case STATE_LIGHT_POSITION:
701 case STATE_LIGHT_POSITION_NORMALIZED:
702 case STATE_LIGHT_HALF_VECTOR:
710 case STATE_FB_WPOS_Y_TRANSFORM:
713 case STATE_ADVANCED_BLENDING_MODE:
717 /* unknown state indexes are silently ignored and
718 * no flag set, since it is handled by the driver.
724 _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
731 append(char *dst, const char *src)
742 * Convert token 'k' to a string, append it onto 'dst' string.
745 append_token(char *dst, gl_state_index k)
749 append(dst, "material");
752 append(dst, "light");
754 case STATE_LIGHTMODEL_AMBIENT:
755 append(dst, "lightmodel.ambient");
757 case STATE_LIGHTMODEL_SCENECOLOR:
759 case STATE_LIGHTPROD:
760 append(dst, "lightprod");
763 append(dst, "texgen");
765 case STATE_FOG_COLOR:
766 append(dst, "fog.color");
768 case STATE_FOG_PARAMS:
769 append(dst, "fog.params");
771 case STATE_CLIPPLANE:
774 case STATE_POINT_SIZE:
775 append(dst, "point.size");
777 case STATE_POINT_ATTENUATION:
778 append(dst, "point.attenuation");
780 case STATE_MODELVIEW_MATRIX:
781 append(dst, "matrix.modelview");
783 case STATE_PROJECTION_MATRIX:
784 append(dst, "matrix.projection");
786 case STATE_MVP_MATRIX:
787 append(dst, "matrix.mvp");
789 case STATE_TEXTURE_MATRIX:
790 append(dst, "matrix.texture");
792 case STATE_PROGRAM_MATRIX:
793 append(dst, "matrix.program");
795 case STATE_MATRIX_INVERSE:
796 append(dst, ".inverse");
798 case STATE_MATRIX_TRANSPOSE:
799 append(dst, ".transpose");
801 case STATE_MATRIX_INVTRANS:
802 append(dst, ".invtrans");
805 append(dst, ".ambient");
808 append(dst, ".diffuse");
811 append(dst, ".specular");
814 append(dst, ".emission");
816 case STATE_SHININESS:
817 append(dst, "lshininess");
819 case STATE_HALF_VECTOR:
820 append(dst, ".half");
823 append(dst, ".position");
825 case STATE_ATTENUATION:
826 append(dst, ".attenuation");
828 case STATE_SPOT_DIRECTION:
829 append(dst, ".spot.direction");
831 case STATE_SPOT_CUTOFF:
832 append(dst, ".spot.cutoff");
834 case STATE_TEXGEN_EYE_S:
835 append(dst, ".eye.s");
837 case STATE_TEXGEN_EYE_T:
838 append(dst, ".eye.t");
840 case STATE_TEXGEN_EYE_R:
841 append(dst, ".eye.r");
843 case STATE_TEXGEN_EYE_Q:
844 append(dst, ".eye.q");
846 case STATE_TEXGEN_OBJECT_S:
847 append(dst, ".object.s");
849 case STATE_TEXGEN_OBJECT_T:
850 append(dst, ".object.t");
852 case STATE_TEXGEN_OBJECT_R:
853 append(dst, ".object.r");
855 case STATE_TEXGEN_OBJECT_Q:
856 append(dst, ".object.q");
858 case STATE_TEXENV_COLOR:
859 append(dst, "texenv");
861 case STATE_NUM_SAMPLES:
862 append(dst, "numsamples");
864 case STATE_DEPTH_RANGE:
865 append(dst, "depth.range");
867 case STATE_VERTEX_PROGRAM:
868 case STATE_FRAGMENT_PROGRAM:
874 append(dst, "local");
876 /* BEGIN internal state vars */
878 append(dst, ".internal.");
880 case STATE_CURRENT_ATTRIB:
881 append(dst, "current");
883 case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
884 append(dst, "currentAttribMaybeVPClamped");
886 case STATE_NORMAL_SCALE:
887 append(dst, "normalScale");
889 case STATE_FOG_PARAMS_OPTIMIZED:
890 append(dst, "fogParamsOptimized");
892 case STATE_POINT_SIZE_CLAMPED:
893 append(dst, "pointSizeClamped");
895 case STATE_LIGHT_SPOT_DIR_NORMALIZED:
896 append(dst, "lightSpotDirNormalized");
898 case STATE_LIGHT_POSITION:
899 append(dst, "lightPosition");
901 case STATE_LIGHT_POSITION_NORMALIZED:
902 append(dst, "light.position.normalized");
904 case STATE_LIGHT_HALF_VECTOR:
905 append(dst, "lightHalfVector");
908 append(dst, "PTscale");
911 append(dst, "PTbias");
914 append(dst, "FbSize");
916 case STATE_FB_WPOS_Y_TRANSFORM:
917 append(dst, "FbWposYTransform");
919 case STATE_ADVANCED_BLENDING_MODE:
920 append(dst, "AdvancedBlendingMode");
923 /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
924 append(dst, "driverState");
929 append_face(char *dst, GLint face)
932 append(dst, "front.");
934 append(dst, "back.");
938 append_index(char *dst, GLint index)
941 sprintf(s, "[%d]", index);
946 * Make a string from the given state vector.
947 * For example, return "state.matrix.texture[2].inverse".
948 * Use free() to deallocate the string.
951 _mesa_program_state_string(const gl_state_index16 state[STATE_LENGTH])
956 append(str, "state.");
957 append_token(str, state[0]);
961 append_face(str, state[1]);
962 append_token(str, state[2]);
965 append_index(str, state[1]); /* light number [i]. */
966 append_token(str, state[2]); /* coefficients */
968 case STATE_LIGHTMODEL_AMBIENT:
969 append(str, "lightmodel.ambient");
971 case STATE_LIGHTMODEL_SCENECOLOR:
973 append(str, "lightmodel.front.scenecolor");
976 append(str, "lightmodel.back.scenecolor");
979 case STATE_LIGHTPROD:
980 append_index(str, state[1]); /* light number [i]. */
981 append_face(str, state[2]);
982 append_token(str, state[3]);
985 append_index(str, state[1]); /* tex unit [i] */
986 append_token(str, state[2]); /* plane coef */
988 case STATE_TEXENV_COLOR:
989 append_index(str, state[1]); /* tex unit [i] */
990 append(str, "color");
992 case STATE_CLIPPLANE:
993 append_index(str, state[1]); /* plane [i] */
994 append(str, ".plane");
996 case STATE_MODELVIEW_MATRIX:
997 case STATE_PROJECTION_MATRIX:
998 case STATE_MVP_MATRIX:
999 case STATE_TEXTURE_MATRIX:
1000 case STATE_PROGRAM_MATRIX:
1002 /* state[0] = modelview, projection, texture, etc. */
1003 /* state[1] = which texture matrix or program matrix */
1004 /* state[2] = first row to fetch */
1005 /* state[3] = last row to fetch */
1006 /* state[4] = transpose, inverse or invtrans */
1007 const gl_state_index mat = state[0];
1008 const GLuint index = (GLuint) state[1];
1009 const GLuint firstRow = (GLuint) state[2];
1010 const GLuint lastRow = (GLuint) state[3];
1011 const gl_state_index modifier = state[4];
1013 mat == STATE_TEXTURE_MATRIX ||
1014 mat == STATE_PROGRAM_MATRIX)
1015 append_index(str, index);
1017 append_token(str, modifier);
1018 if (firstRow == lastRow)
1019 sprintf(tmp, ".row[%d]", firstRow);
1021 sprintf(tmp, ".row[%d..%d]", firstRow, lastRow);
1025 case STATE_POINT_SIZE:
1027 case STATE_POINT_ATTENUATION:
1029 case STATE_FOG_PARAMS:
1031 case STATE_FOG_COLOR:
1033 case STATE_NUM_SAMPLES:
1035 case STATE_DEPTH_RANGE:
1037 case STATE_FRAGMENT_PROGRAM:
1038 case STATE_VERTEX_PROGRAM:
1039 /* state[1] = {STATE_ENV, STATE_LOCAL} */
1040 /* state[2] = parameter index */
1041 append_token(str, state[1]);
1042 append_index(str, state[2]);
1044 case STATE_NORMAL_SCALE:
1046 case STATE_INTERNAL:
1047 append_token(str, state[1]);
1048 if (state[1] == STATE_CURRENT_ATTRIB)
1049 append_index(str, state[2]);
1052 _mesa_problem(NULL, "Invalid state in _mesa_program_state_string");
1061 * Loop over all the parameters in a parameter list. If the parameter
1062 * is a GL state reference, look up the current value of that state
1063 * variable and put it into the parameter's Value[4] array.
1064 * Other parameter types never change or are explicitly set by the user
1065 * with glUniform() or glProgramParameter(), etc.
1066 * This would be called at glBegin time.
1069 _mesa_load_state_parameters(struct gl_context *ctx,
1070 struct gl_program_parameter_list *paramList)
1077 for (i = 0; i < paramList->NumParameters; i++) {
1078 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1079 _mesa_fetch_state(ctx,
1080 paramList->Parameters[i].StateIndexes,
1081 ¶mList->ParameterValues[i][0]);