• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 /**
26  * \file prog_statevars.c
27  * Program state variable management.
28  * \author Brian Paul
29  */
30 
31 
32 #include <stdio.h>
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"
44 
45 
46 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
47 
48 
49 /**
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.
55  */
56 static void
_mesa_fetch_state(struct gl_context * ctx,const gl_state_index state[],gl_constant_value * val)57 _mesa_fetch_state(struct gl_context *ctx, const gl_state_index state[],
58                   gl_constant_value *val)
59 {
60    GLfloat *value = &val->f;
61 
62    switch (state[0]) {
63    case STATE_MATERIAL:
64       {
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().
73           */
74          /* state[2] is the material attribute */
75          switch (state[2]) {
76          case STATE_AMBIENT:
77             COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_AMBIENT + face]);
78             return;
79          case STATE_DIFFUSE:
80             COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_DIFFUSE + face]);
81             return;
82          case STATE_SPECULAR:
83             COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_SPECULAR + face]);
84             return;
85          case STATE_EMISSION:
86             COPY_4V(value, mat->Attrib[MAT_ATTRIB_FRONT_EMISSION + face]);
87             return;
88          case STATE_SHININESS:
89             value[0] = mat->Attrib[MAT_ATTRIB_FRONT_SHININESS + face][0];
90             value[1] = 0.0F;
91             value[2] = 0.0F;
92             value[3] = 1.0F;
93             return;
94          default:
95             _mesa_problem(ctx, "Invalid material state in fetch_state");
96             return;
97          }
98       }
99    case STATE_LIGHT:
100       {
101          /* state[1] is the light number */
102          const GLuint ln = (GLuint) state[1];
103          /* state[2] is the light attribute */
104          switch (state[2]) {
105          case STATE_AMBIENT:
106             COPY_4V(value, ctx->Light.Light[ln].Ambient);
107             return;
108          case STATE_DIFFUSE:
109             COPY_4V(value, ctx->Light.Light[ln].Diffuse);
110             return;
111          case STATE_SPECULAR:
112             COPY_4V(value, ctx->Light.Light[ln].Specular);
113             return;
114          case STATE_POSITION:
115             COPY_4V(value, ctx->Light.Light[ln].EyePosition);
116             return;
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;
122             return;
123          case STATE_SPOT_DIRECTION:
124             COPY_3V(value, ctx->Light.Light[ln].SpotDirection);
125             value[3] = ctx->Light.Light[ln]._CosCutoff;
126             return;
127          case STATE_SPOT_CUTOFF:
128             value[0] = ctx->Light.Light[ln].SpotCutoff;
129             return;
130          case STATE_HALF_VECTOR:
131             {
132                static const GLfloat eye_z[] = {0, 0, 1};
133                GLfloat p[3];
134                /* Compute infinite half angle vector:
135                 *   halfVector = normalize(normalize(lightPos) + (0, 0, 1))
136 		* light.EyePosition.w should be 0 for infinite lights.
137                 */
138                COPY_3V(p, ctx->Light.Light[ln].EyePosition);
139                NORMALIZE_3FV(p);
140 	       ADD_3V(value, p, eye_z);
141 	       NORMALIZE_3FV(value);
142 	       value[3] = 1.0;
143             }
144             return;
145          default:
146             _mesa_problem(ctx, "Invalid light state in fetch_state");
147             return;
148          }
149       }
150    case STATE_LIGHTMODEL_AMBIENT:
151       COPY_4V(value, ctx->Light.Model.Ambient);
152       return;
153    case STATE_LIGHTMODEL_SCENECOLOR:
154       if (state[1] == 0) {
155          /* front */
156          GLint i;
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];
161          }
162 	 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
163       }
164       else {
165          /* back */
166          GLint i;
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];
171          }
172 	 value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
173       }
174       return;
175    case STATE_LIGHTPROD:
176       {
177          const GLuint ln = (GLuint) state[1];
178          const GLuint face = (GLuint) state[2];
179          GLint i;
180          assert(face == 0 || face == 1);
181          switch (state[3]) {
182             case STATE_AMBIENT:
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];
186                }
187                /* [3] = material alpha */
188                value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][3];
189                return;
190             case STATE_DIFFUSE:
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];
194                }
195                /* [3] = material alpha */
196                value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3];
197                return;
198             case STATE_SPECULAR:
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];
202                }
203                /* [3] = material alpha */
204                value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][3];
205                return;
206             default:
207                _mesa_problem(ctx, "Invalid lightprod state in fetch_state");
208                return;
209          }
210       }
211    case STATE_TEXGEN:
212       {
213          /* state[1] is the texture unit */
214          const GLuint unit = (GLuint) state[1];
215          /* state[2] is the texgen attribute */
216          switch (state[2]) {
217          case STATE_TEXGEN_EYE_S:
218             COPY_4V(value, ctx->Texture.Unit[unit].GenS.EyePlane);
219             return;
220          case STATE_TEXGEN_EYE_T:
221             COPY_4V(value, ctx->Texture.Unit[unit].GenT.EyePlane);
222             return;
223          case STATE_TEXGEN_EYE_R:
224             COPY_4V(value, ctx->Texture.Unit[unit].GenR.EyePlane);
225             return;
226          case STATE_TEXGEN_EYE_Q:
227             COPY_4V(value, ctx->Texture.Unit[unit].GenQ.EyePlane);
228             return;
229          case STATE_TEXGEN_OBJECT_S:
230             COPY_4V(value, ctx->Texture.Unit[unit].GenS.ObjectPlane);
231             return;
232          case STATE_TEXGEN_OBJECT_T:
233             COPY_4V(value, ctx->Texture.Unit[unit].GenT.ObjectPlane);
234             return;
235          case STATE_TEXGEN_OBJECT_R:
236             COPY_4V(value, ctx->Texture.Unit[unit].GenR.ObjectPlane);
237             return;
238          case STATE_TEXGEN_OBJECT_Q:
239             COPY_4V(value, ctx->Texture.Unit[unit].GenQ.ObjectPlane);
240             return;
241          default:
242             _mesa_problem(ctx, "Invalid texgen state in fetch_state");
243             return;
244          }
245       }
246    case STATE_TEXENV_COLOR:
247       {
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);
252          else
253             COPY_4V(value, ctx->Texture.Unit[unit].EnvColorUnclamped);
254       }
255       return;
256    case STATE_FOG_COLOR:
257       if (_mesa_get_clamp_fragment_color(ctx, ctx->DrawBuffer))
258          COPY_4V(value, ctx->Fog.Color);
259       else
260          COPY_4V(value, ctx->Fog.ColorUnclamped);
261       return;
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);
267       return;
268    case STATE_CLIPPLANE:
269       {
270          const GLuint plane = (GLuint) state[1];
271          COPY_4V(value, ctx->Transform.EyeUserPlane[plane]);
272       }
273       return;
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;
279       return;
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];
284       value[3] = 1.0F;
285       return;
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:
291       {
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];
303          const GLfloat *m;
304          GLuint row, i;
305          assert(firstRow < 4);
306          assert(lastRow < 4);
307          if (mat == STATE_MODELVIEW_MATRIX) {
308             matrix = ctx->ModelviewMatrixStack.Top;
309          }
310          else if (mat == STATE_PROJECTION_MATRIX) {
311             matrix = ctx->ProjectionMatrixStack.Top;
312          }
313          else if (mat == STATE_MVP_MATRIX) {
314             matrix = &ctx->_ModelProjectMatrix;
315          }
316          else if (mat == STATE_TEXTURE_MATRIX) {
317             assert(index < ARRAY_SIZE(ctx->TextureMatrixStack));
318             matrix = ctx->TextureMatrixStack[index].Top;
319          }
320          else if (mat == STATE_PROGRAM_MATRIX) {
321             assert(index < ARRAY_SIZE(ctx->ProgramMatrixStack));
322             matrix = ctx->ProgramMatrixStack[index].Top;
323          }
324          else {
325             _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()");
326             return;
327          }
328          if (modifier == STATE_MATRIX_INVERSE ||
329              modifier == STATE_MATRIX_INVTRANS) {
330             /* Be sure inverse is up to date:
331 	     */
332 	    _math_matrix_analyse( (GLmatrix*) matrix );
333             m = matrix->inv;
334          }
335          else {
336             m = matrix->m;
337          }
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];
345             }
346          }
347          else {
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];
353             }
354          }
355       }
356       return;
357    case STATE_NUM_SAMPLES:
358       val[0].i = MAX2(1, _mesa_geometric_samples(ctx->DrawBuffer));
359       return;
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 */
364       value[3] = 1.0;
365       return;
366    case STATE_FRAGMENT_PROGRAM:
367       {
368          /* state[1] = {STATE_ENV, STATE_LOCAL} */
369          /* state[2] = parameter index          */
370          const int idx = (int) state[2];
371          switch (state[1]) {
372             case STATE_ENV:
373                COPY_4V(value, ctx->FragmentProgram.Parameters[idx]);
374                return;
375             case STATE_LOCAL:
376                if (!ctx->FragmentProgram.Current->arb.LocalParams) {
377                   ctx->FragmentProgram.Current->arb.LocalParams =
378                      rzalloc_array_size(ctx->FragmentProgram.Current,
379                                         sizeof(float[4]),
380                                         MAX_PROGRAM_LOCAL_PARAMS);
381                   if (!ctx->FragmentProgram.Current->arb.LocalParams)
382                      return;
383                }
384 
385                COPY_4V(value,
386                        ctx->FragmentProgram.Current->arb.LocalParams[idx]);
387                return;
388             default:
389                _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
390                return;
391          }
392       }
393       return;
394 
395    case STATE_VERTEX_PROGRAM:
396       {
397          /* state[1] = {STATE_ENV, STATE_LOCAL} */
398          /* state[2] = parameter index          */
399          const int idx = (int) state[2];
400          switch (state[1]) {
401             case STATE_ENV:
402                COPY_4V(value, ctx->VertexProgram.Parameters[idx]);
403                return;
404             case STATE_LOCAL:
405                if (!ctx->VertexProgram.Current->arb.LocalParams) {
406                   ctx->VertexProgram.Current->arb.LocalParams =
407                      rzalloc_array_size(ctx->VertexProgram.Current,
408                                         sizeof(float[4]),
409                                         MAX_PROGRAM_LOCAL_PARAMS);
410                   if (!ctx->VertexProgram.Current->arb.LocalParams)
411                      return;
412                }
413 
414                COPY_4V(value,
415                        ctx->VertexProgram.Current->arb.LocalParams[idx]);
416                return;
417             default:
418                _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
419                return;
420          }
421       }
422       return;
423 
424    case STATE_NORMAL_SCALE:
425       ASSIGN_4V(value, ctx->_ModelViewInvScaleEyespace, 0, 0, 1);
426       return;
427 
428    case STATE_INTERNAL:
429       switch (state[1]) {
430       case STATE_CURRENT_ATTRIB:
431          {
432             const GLuint idx = (GLuint) state[2];
433             COPY_4V(value, ctx->Current.Attrib[idx]);
434          }
435          return;
436 
437       case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
438          {
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);
447             }
448             else
449                COPY_4V(value, ctx->Current.Attrib[idx]);
450          }
451          return;
452 
453       case STATE_NORMAL_SCALE:
454          ASSIGN_4V(value,
455                    ctx->_ModelViewInvScale,
456                    ctx->_ModelViewInvScale,
457                    ctx->_ModelViewInvScale,
458                    1);
459          return;
460 
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
465           * single MAD.
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)
469           */
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);
475          return;
476 
477       case STATE_POINT_SIZE_CLAMPED:
478          {
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.
488             */
489             GLfloat minImplSize;
490             GLfloat maxImplSize;
491             if (ctx->Point.PointSprite) {
492                minImplSize = ctx->Const.MinPointSizeAA;
493                maxImplSize = ctx->Const.MaxPointSize;
494             }
495             else if (ctx->Point.SmoothFlag || _mesa_is_multisample_enabled(ctx)) {
496                minImplSize = ctx->Const.MinPointSizeAA;
497                maxImplSize = ctx->Const.MaxPointSizeAA;
498             }
499             else {
500                minImplSize = ctx->Const.MinPointSize;
501                maxImplSize = ctx->Const.MaxPointSize;
502             }
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;
507          }
508          return;
509       case STATE_LIGHT_SPOT_DIR_NORMALIZED:
510          {
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;
516          }
517          return;
518 
519       case STATE_LIGHT_POSITION:
520          {
521             const GLuint ln = (GLuint) state[2];
522             COPY_4V(value, ctx->Light.Light[ln]._Position);
523          }
524          return;
525 
526       case STATE_LIGHT_POSITION_NORMALIZED:
527          {
528             const GLuint ln = (GLuint) state[2];
529             COPY_4V(value, ctx->Light.Light[ln]._Position);
530             NORMALIZE_3FV( value );
531          }
532          return;
533 
534       case STATE_LIGHT_HALF_VECTOR:
535          {
536             const GLuint ln = (GLuint) state[2];
537             GLfloat p[3];
538             /* Compute infinite half angle vector:
539              *   halfVector = normalize(normalize(lightPos) + (0, 0, 1))
540              * light.EyePosition.w should be 0 for infinite lights.
541              */
542             COPY_3V(p, ctx->Light.Light[ln]._Position);
543             NORMALIZE_3FV(p);
544             ADD_3V(value, p, ctx->_EyeZDir);
545             NORMALIZE_3FV(value);
546             value[3] = 1.0;
547          }
548          return;
549 
550       case STATE_PT_SCALE:
551          value[0] = ctx->Pixel.RedScale;
552          value[1] = ctx->Pixel.GreenScale;
553          value[2] = ctx->Pixel.BlueScale;
554          value[3] = ctx->Pixel.AlphaScale;
555          return;
556 
557       case STATE_PT_BIAS:
558          value[0] = ctx->Pixel.RedBias;
559          value[1] = ctx->Pixel.GreenBias;
560          value[2] = ctx->Pixel.BlueBias;
561          value[3] = ctx->Pixel.AlphaBias;
562          return;
563 
564       case STATE_FB_SIZE:
565          value[0] = (GLfloat) (ctx->DrawBuffer->Width - 1);
566          value[1] = (GLfloat) (ctx->DrawBuffer->Height - 1);
567          value[2] = 0.0F;
568          value[3] = 0.0F;
569          return;
570 
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). */
576             value[0] = 1.0F;
577             value[1] = 0.0F;
578             value[2] = -1.0F;
579             value[3] = (GLfloat) ctx->DrawBuffer->Height;
580          } else {
581             /* Flipping Y upside down (XY) followed by identity (ZW). */
582             value[0] = -1.0F;
583             value[1] = (GLfloat) ctx->DrawBuffer->Height;
584             value[2] = 1.0F;
585             value[3] = 0.0F;
586          }
587          return;
588 
589       case STATE_TCS_PATCH_VERTICES_IN:
590          val[0].i = ctx->TessCtrlProgram.patch_vertices;
591          return;
592 
593       case STATE_TES_PATCH_VERTICES_IN:
594          if (ctx->TessCtrlProgram._Current)
595             val[0].i = ctx->TessCtrlProgram._Current->info.tess.tcs_vertices_out;
596          else
597             val[0].i = ctx->TessCtrlProgram.patch_vertices;
598          return;
599 
600       case STATE_ADVANCED_BLENDING_MODE:
601          val[0].i = ctx->Color.BlendEnabled ? ctx->Color._AdvancedBlendMode : 0;
602          return;
603 
604       /* XXX: make sure new tokens added here are also handled in the
605        * _mesa_program_state_flags() switch, below.
606        */
607       default:
608          /* Unknown state indexes are silently ignored here.
609           * Drivers may do something special.
610           */
611          return;
612       }
613       return;
614 
615    default:
616       _mesa_problem(ctx, "Invalid state in _mesa_fetch_state");
617       return;
618    }
619 }
620 
621 
622 /**
623  * Return a bitmask of the Mesa state flags (_NEW_* values) which would
624  * indicate that the given context state may have changed.
625  * The bitmask is used during validation to determine if we need to update
626  * vertex/fragment program parameters (like "state.material.color") when
627  * some GL state has changed.
628  */
629 GLbitfield
_mesa_program_state_flags(const gl_state_index state[STATE_LENGTH])630 _mesa_program_state_flags(const gl_state_index state[STATE_LENGTH])
631 {
632    switch (state[0]) {
633    case STATE_MATERIAL:
634    case STATE_LIGHTPROD:
635    case STATE_LIGHTMODEL_SCENECOLOR:
636       /* these can be effected by glColor when colormaterial mode is used */
637       return _NEW_LIGHT | _NEW_CURRENT_ATTRIB;
638 
639    case STATE_LIGHT:
640    case STATE_LIGHTMODEL_AMBIENT:
641       return _NEW_LIGHT;
642 
643    case STATE_TEXGEN:
644       return _NEW_TEXTURE_STATE;
645    case STATE_TEXENV_COLOR:
646       return _NEW_TEXTURE_STATE | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
647 
648    case STATE_FOG_COLOR:
649       return _NEW_FOG | _NEW_BUFFERS | _NEW_FRAG_CLAMP;
650    case STATE_FOG_PARAMS:
651       return _NEW_FOG;
652 
653    case STATE_CLIPPLANE:
654       return _NEW_TRANSFORM;
655 
656    case STATE_POINT_SIZE:
657    case STATE_POINT_ATTENUATION:
658       return _NEW_POINT;
659 
660    case STATE_MODELVIEW_MATRIX:
661       return _NEW_MODELVIEW;
662    case STATE_PROJECTION_MATRIX:
663       return _NEW_PROJECTION;
664    case STATE_MVP_MATRIX:
665       return _NEW_MODELVIEW | _NEW_PROJECTION;
666    case STATE_TEXTURE_MATRIX:
667       return _NEW_TEXTURE_MATRIX;
668    case STATE_PROGRAM_MATRIX:
669       return _NEW_TRACK_MATRIX;
670 
671    case STATE_NUM_SAMPLES:
672       return _NEW_BUFFERS;
673 
674    case STATE_DEPTH_RANGE:
675       return _NEW_VIEWPORT;
676 
677    case STATE_FRAGMENT_PROGRAM:
678    case STATE_VERTEX_PROGRAM:
679       return _NEW_PROGRAM;
680 
681    case STATE_NORMAL_SCALE:
682       return _NEW_MODELVIEW;
683 
684    case STATE_INTERNAL:
685       switch (state[1]) {
686       case STATE_CURRENT_ATTRIB:
687          return _NEW_CURRENT_ATTRIB;
688       case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
689          return _NEW_CURRENT_ATTRIB | _NEW_LIGHT | _NEW_BUFFERS;
690 
691       case STATE_NORMAL_SCALE:
692          return _NEW_MODELVIEW;
693 
694       case STATE_FOG_PARAMS_OPTIMIZED:
695 	 return _NEW_FOG;
696       case STATE_POINT_SIZE_CLAMPED:
697          return _NEW_POINT | _NEW_MULTISAMPLE;
698       case STATE_LIGHT_SPOT_DIR_NORMALIZED:
699       case STATE_LIGHT_POSITION:
700       case STATE_LIGHT_POSITION_NORMALIZED:
701       case STATE_LIGHT_HALF_VECTOR:
702          return _NEW_LIGHT;
703 
704       case STATE_PT_SCALE:
705       case STATE_PT_BIAS:
706          return _NEW_PIXEL;
707 
708       case STATE_FB_SIZE:
709       case STATE_FB_WPOS_Y_TRANSFORM:
710          return _NEW_BUFFERS;
711 
712       case STATE_ADVANCED_BLENDING_MODE:
713          return _NEW_COLOR;
714 
715       default:
716          /* unknown state indexes are silently ignored and
717          *  no flag set, since it is handled by the driver.
718          */
719 	 return 0;
720       }
721 
722    default:
723       _mesa_problem(NULL, "unexpected state[0] in make_state_flags()");
724       return 0;
725    }
726 }
727 
728 
729 static void
append(char * dst,const char * src)730 append(char *dst, const char *src)
731 {
732    while (*dst)
733       dst++;
734    while (*src)
735      *dst++ = *src++;
736    *dst = 0;
737 }
738 
739 
740 /**
741  * Convert token 'k' to a string, append it onto 'dst' string.
742  */
743 static void
append_token(char * dst,gl_state_index k)744 append_token(char *dst, gl_state_index k)
745 {
746    switch (k) {
747    case STATE_MATERIAL:
748       append(dst, "material");
749       break;
750    case STATE_LIGHT:
751       append(dst, "light");
752       break;
753    case STATE_LIGHTMODEL_AMBIENT:
754       append(dst, "lightmodel.ambient");
755       break;
756    case STATE_LIGHTMODEL_SCENECOLOR:
757       break;
758    case STATE_LIGHTPROD:
759       append(dst, "lightprod");
760       break;
761    case STATE_TEXGEN:
762       append(dst, "texgen");
763       break;
764    case STATE_FOG_COLOR:
765       append(dst, "fog.color");
766       break;
767    case STATE_FOG_PARAMS:
768       append(dst, "fog.params");
769       break;
770    case STATE_CLIPPLANE:
771       append(dst, "clip");
772       break;
773    case STATE_POINT_SIZE:
774       append(dst, "point.size");
775       break;
776    case STATE_POINT_ATTENUATION:
777       append(dst, "point.attenuation");
778       break;
779    case STATE_MODELVIEW_MATRIX:
780       append(dst, "matrix.modelview");
781       break;
782    case STATE_PROJECTION_MATRIX:
783       append(dst, "matrix.projection");
784       break;
785    case STATE_MVP_MATRIX:
786       append(dst, "matrix.mvp");
787       break;
788    case STATE_TEXTURE_MATRIX:
789       append(dst, "matrix.texture");
790       break;
791    case STATE_PROGRAM_MATRIX:
792       append(dst, "matrix.program");
793       break;
794    case STATE_MATRIX_INVERSE:
795       append(dst, ".inverse");
796       break;
797    case STATE_MATRIX_TRANSPOSE:
798       append(dst, ".transpose");
799       break;
800    case STATE_MATRIX_INVTRANS:
801       append(dst, ".invtrans");
802       break;
803    case STATE_AMBIENT:
804       append(dst, ".ambient");
805       break;
806    case STATE_DIFFUSE:
807       append(dst, ".diffuse");
808       break;
809    case STATE_SPECULAR:
810       append(dst, ".specular");
811       break;
812    case STATE_EMISSION:
813       append(dst, ".emission");
814       break;
815    case STATE_SHININESS:
816       append(dst, "lshininess");
817       break;
818    case STATE_HALF_VECTOR:
819       append(dst, ".half");
820       break;
821    case STATE_POSITION:
822       append(dst, ".position");
823       break;
824    case STATE_ATTENUATION:
825       append(dst, ".attenuation");
826       break;
827    case STATE_SPOT_DIRECTION:
828       append(dst, ".spot.direction");
829       break;
830    case STATE_SPOT_CUTOFF:
831       append(dst, ".spot.cutoff");
832       break;
833    case STATE_TEXGEN_EYE_S:
834       append(dst, ".eye.s");
835       break;
836    case STATE_TEXGEN_EYE_T:
837       append(dst, ".eye.t");
838       break;
839    case STATE_TEXGEN_EYE_R:
840       append(dst, ".eye.r");
841       break;
842    case STATE_TEXGEN_EYE_Q:
843       append(dst, ".eye.q");
844       break;
845    case STATE_TEXGEN_OBJECT_S:
846       append(dst, ".object.s");
847       break;
848    case STATE_TEXGEN_OBJECT_T:
849       append(dst, ".object.t");
850       break;
851    case STATE_TEXGEN_OBJECT_R:
852       append(dst, ".object.r");
853       break;
854    case STATE_TEXGEN_OBJECT_Q:
855       append(dst, ".object.q");
856       break;
857    case STATE_TEXENV_COLOR:
858       append(dst, "texenv");
859       break;
860    case STATE_NUM_SAMPLES:
861       append(dst, "numsamples");
862       break;
863    case STATE_DEPTH_RANGE:
864       append(dst, "depth.range");
865       break;
866    case STATE_VERTEX_PROGRAM:
867    case STATE_FRAGMENT_PROGRAM:
868       break;
869    case STATE_ENV:
870       append(dst, "env");
871       break;
872    case STATE_LOCAL:
873       append(dst, "local");
874       break;
875    /* BEGIN internal state vars */
876    case STATE_INTERNAL:
877       append(dst, ".internal.");
878       break;
879    case STATE_CURRENT_ATTRIB:
880       append(dst, "current");
881       break;
882    case STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED:
883       append(dst, "currentAttribMaybeVPClamped");
884       break;
885    case STATE_NORMAL_SCALE:
886       append(dst, "normalScale");
887       break;
888    case STATE_FOG_PARAMS_OPTIMIZED:
889       append(dst, "fogParamsOptimized");
890       break;
891    case STATE_POINT_SIZE_CLAMPED:
892       append(dst, "pointSizeClamped");
893       break;
894    case STATE_LIGHT_SPOT_DIR_NORMALIZED:
895       append(dst, "lightSpotDirNormalized");
896       break;
897    case STATE_LIGHT_POSITION:
898       append(dst, "lightPosition");
899       break;
900    case STATE_LIGHT_POSITION_NORMALIZED:
901       append(dst, "light.position.normalized");
902       break;
903    case STATE_LIGHT_HALF_VECTOR:
904       append(dst, "lightHalfVector");
905       break;
906    case STATE_PT_SCALE:
907       append(dst, "PTscale");
908       break;
909    case STATE_PT_BIAS:
910       append(dst, "PTbias");
911       break;
912    case STATE_FB_SIZE:
913       append(dst, "FbSize");
914       break;
915    case STATE_FB_WPOS_Y_TRANSFORM:
916       append(dst, "FbWposYTransform");
917       break;
918    case STATE_ADVANCED_BLENDING_MODE:
919       append(dst, "AdvancedBlendingMode");
920       break;
921    default:
922       /* probably STATE_INTERNAL_DRIVER+i (driver private state) */
923       append(dst, "driverState");
924    }
925 }
926 
927 static void
append_face(char * dst,GLint face)928 append_face(char *dst, GLint face)
929 {
930    if (face == 0)
931       append(dst, "front.");
932    else
933       append(dst, "back.");
934 }
935 
936 static void
append_index(char * dst,GLint index)937 append_index(char *dst, GLint index)
938 {
939    char s[20];
940    sprintf(s, "[%d]", index);
941    append(dst, s);
942 }
943 
944 /**
945  * Make a string from the given state vector.
946  * For example, return "state.matrix.texture[2].inverse".
947  * Use free() to deallocate the string.
948  */
949 char *
_mesa_program_state_string(const gl_state_index state[STATE_LENGTH])950 _mesa_program_state_string(const gl_state_index state[STATE_LENGTH])
951 {
952    char str[1000] = "";
953    char tmp[30];
954 
955    append(str, "state.");
956    append_token(str, state[0]);
957 
958    switch (state[0]) {
959    case STATE_MATERIAL:
960       append_face(str, state[1]);
961       append_token(str, state[2]);
962       break;
963    case STATE_LIGHT:
964       append_index(str, state[1]); /* light number [i]. */
965       append_token(str, state[2]); /* coefficients */
966       break;
967    case STATE_LIGHTMODEL_AMBIENT:
968       append(str, "lightmodel.ambient");
969       break;
970    case STATE_LIGHTMODEL_SCENECOLOR:
971       if (state[1] == 0) {
972          append(str, "lightmodel.front.scenecolor");
973       }
974       else {
975          append(str, "lightmodel.back.scenecolor");
976       }
977       break;
978    case STATE_LIGHTPROD:
979       append_index(str, state[1]); /* light number [i]. */
980       append_face(str, state[2]);
981       append_token(str, state[3]);
982       break;
983    case STATE_TEXGEN:
984       append_index(str, state[1]); /* tex unit [i] */
985       append_token(str, state[2]); /* plane coef */
986       break;
987    case STATE_TEXENV_COLOR:
988       append_index(str, state[1]); /* tex unit [i] */
989       append(str, "color");
990       break;
991    case STATE_CLIPPLANE:
992       append_index(str, state[1]); /* plane [i] */
993       append(str, ".plane");
994       break;
995    case STATE_MODELVIEW_MATRIX:
996    case STATE_PROJECTION_MATRIX:
997    case STATE_MVP_MATRIX:
998    case STATE_TEXTURE_MATRIX:
999    case STATE_PROGRAM_MATRIX:
1000       {
1001          /* state[0] = modelview, projection, texture, etc. */
1002          /* state[1] = which texture matrix or program matrix */
1003          /* state[2] = first row to fetch */
1004          /* state[3] = last row to fetch */
1005          /* state[4] = transpose, inverse or invtrans */
1006          const gl_state_index mat = state[0];
1007          const GLuint index = (GLuint) state[1];
1008          const GLuint firstRow = (GLuint) state[2];
1009          const GLuint lastRow = (GLuint) state[3];
1010          const gl_state_index modifier = state[4];
1011          if (index ||
1012              mat == STATE_TEXTURE_MATRIX ||
1013              mat == STATE_PROGRAM_MATRIX)
1014             append_index(str, index);
1015          if (modifier)
1016             append_token(str, modifier);
1017          if (firstRow == lastRow)
1018             sprintf(tmp, ".row[%d]", firstRow);
1019          else
1020             sprintf(tmp, ".row[%d..%d]", firstRow, lastRow);
1021          append(str, tmp);
1022       }
1023       break;
1024    case STATE_POINT_SIZE:
1025       break;
1026    case STATE_POINT_ATTENUATION:
1027       break;
1028    case STATE_FOG_PARAMS:
1029       break;
1030    case STATE_FOG_COLOR:
1031       break;
1032    case STATE_NUM_SAMPLES:
1033       break;
1034    case STATE_DEPTH_RANGE:
1035       break;
1036    case STATE_FRAGMENT_PROGRAM:
1037    case STATE_VERTEX_PROGRAM:
1038       /* state[1] = {STATE_ENV, STATE_LOCAL} */
1039       /* state[2] = parameter index          */
1040       append_token(str, state[1]);
1041       append_index(str, state[2]);
1042       break;
1043    case STATE_NORMAL_SCALE:
1044       break;
1045    case STATE_INTERNAL:
1046       append_token(str, state[1]);
1047       if (state[1] == STATE_CURRENT_ATTRIB)
1048          append_index(str, state[2]);
1049        break;
1050    default:
1051       _mesa_problem(NULL, "Invalid state in _mesa_program_state_string");
1052       break;
1053    }
1054 
1055    return strdup(str);
1056 }
1057 
1058 
1059 /**
1060  * Loop over all the parameters in a parameter list.  If the parameter
1061  * is a GL state reference, look up the current value of that state
1062  * variable and put it into the parameter's Value[4] array.
1063  * Other parameter types never change or are explicitly set by the user
1064  * with glUniform() or glProgramParameter(), etc.
1065  * This would be called at glBegin time.
1066  */
1067 void
_mesa_load_state_parameters(struct gl_context * ctx,struct gl_program_parameter_list * paramList)1068 _mesa_load_state_parameters(struct gl_context *ctx,
1069                             struct gl_program_parameter_list *paramList)
1070 {
1071    GLuint i;
1072 
1073    if (!paramList)
1074       return;
1075 
1076    for (i = 0; i < paramList->NumParameters; i++) {
1077       if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1078          _mesa_fetch_state(ctx,
1079 			   paramList->Parameters[i].StateIndexes,
1080                            &paramList->ParameterValues[i][0]);
1081       }
1082    }
1083 }
1084