texstate.c revision 584c9ff1
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * \file texstate.c
27 *
28 * Texture state handling.
29 */
30
31#include "glheader.h"
32#include "colormac.h"
33#if FEATURE_colortable
34#include "colortab.h"
35#endif
36#include "context.h"
37#include "enums.h"
38#include "macros.h"
39#include "texcompress.h"
40#include "texobj.h"
41#include "teximage.h"
42#include "texstate.h"
43#include "texenvprogram.h"
44#include "mtypes.h"
45#include "math/m_xform.h"
46
47
48
49/**
50 * Default texture combine environment state.  This is used to initialize
51 * a context's texture units and as the basis for converting "classic"
52 * texture environmnets to ARB_texture_env_combine style values.
53 */
54static const struct gl_tex_env_combine_state default_combine_state = {
55   GL_MODULATE, GL_MODULATE,
56   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
57   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
58   { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA },
59   { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
60   0, 0,
61   2, 2
62};
63
64
65
66/**
67 * Used by glXCopyContext to copy texture state from one context to another.
68 */
69void
70_mesa_copy_texture_state( const GLcontext *src, GLcontext *dst )
71{
72   GLuint i, tex;
73
74   ASSERT(src);
75   ASSERT(dst);
76
77   dst->Texture.CurrentUnit = src->Texture.CurrentUnit;
78   dst->Texture._GenFlags = src->Texture._GenFlags;
79   dst->Texture._TexGenEnabled = src->Texture._TexGenEnabled;
80   dst->Texture._TexMatEnabled = src->Texture._TexMatEnabled;
81   dst->Texture.SharedPalette = src->Texture.SharedPalette;
82
83   /* per-unit state */
84   for (i = 0; i < src->Const.MaxTextureImageUnits; i++) {
85      dst->Texture.Unit[i].Enabled = src->Texture.Unit[i].Enabled;
86      dst->Texture.Unit[i].EnvMode = src->Texture.Unit[i].EnvMode;
87      COPY_4V(dst->Texture.Unit[i].EnvColor, src->Texture.Unit[i].EnvColor);
88      dst->Texture.Unit[i].TexGenEnabled = src->Texture.Unit[i].TexGenEnabled;
89      dst->Texture.Unit[i].GenModeS = src->Texture.Unit[i].GenModeS;
90      dst->Texture.Unit[i].GenModeT = src->Texture.Unit[i].GenModeT;
91      dst->Texture.Unit[i].GenModeR = src->Texture.Unit[i].GenModeR;
92      dst->Texture.Unit[i].GenModeQ = src->Texture.Unit[i].GenModeQ;
93      dst->Texture.Unit[i]._GenBitS = src->Texture.Unit[i]._GenBitS;
94      dst->Texture.Unit[i]._GenBitT = src->Texture.Unit[i]._GenBitT;
95      dst->Texture.Unit[i]._GenBitR = src->Texture.Unit[i]._GenBitR;
96      dst->Texture.Unit[i]._GenBitQ = src->Texture.Unit[i]._GenBitQ;
97      dst->Texture.Unit[i]._GenFlags = src->Texture.Unit[i]._GenFlags;
98      COPY_4V(dst->Texture.Unit[i].ObjectPlaneS, src->Texture.Unit[i].ObjectPlaneS);
99      COPY_4V(dst->Texture.Unit[i].ObjectPlaneT, src->Texture.Unit[i].ObjectPlaneT);
100      COPY_4V(dst->Texture.Unit[i].ObjectPlaneR, src->Texture.Unit[i].ObjectPlaneR);
101      COPY_4V(dst->Texture.Unit[i].ObjectPlaneQ, src->Texture.Unit[i].ObjectPlaneQ);
102      COPY_4V(dst->Texture.Unit[i].EyePlaneS, src->Texture.Unit[i].EyePlaneS);
103      COPY_4V(dst->Texture.Unit[i].EyePlaneT, src->Texture.Unit[i].EyePlaneT);
104      COPY_4V(dst->Texture.Unit[i].EyePlaneR, src->Texture.Unit[i].EyePlaneR);
105      COPY_4V(dst->Texture.Unit[i].EyePlaneQ, src->Texture.Unit[i].EyePlaneQ);
106      dst->Texture.Unit[i].LodBias = src->Texture.Unit[i].LodBias;
107
108      /* GL_EXT_texture_env_combine */
109      dst->Texture.Unit[i].Combine.ModeRGB = src->Texture.Unit[i].Combine.ModeRGB;
110      dst->Texture.Unit[i].Combine.ModeA = src->Texture.Unit[i].Combine.ModeA;
111      COPY_3V(dst->Texture.Unit[i].Combine.SourceRGB, src->Texture.Unit[i].Combine.SourceRGB);
112      COPY_3V(dst->Texture.Unit[i].Combine.SourceA, src->Texture.Unit[i].Combine.SourceA);
113      COPY_3V(dst->Texture.Unit[i].Combine.OperandRGB, src->Texture.Unit[i].Combine.OperandRGB);
114      COPY_3V(dst->Texture.Unit[i].Combine.OperandA, src->Texture.Unit[i].Combine.OperandA);
115      dst->Texture.Unit[i].Combine.ScaleShiftRGB = src->Texture.Unit[i].Combine.ScaleShiftRGB;
116      dst->Texture.Unit[i].Combine.ScaleShiftA = src->Texture.Unit[i].Combine.ScaleShiftA;
117
118      /* copy texture object bindings, not contents of texture objects */
119      _mesa_lock_context_textures(dst);
120
121      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
122         _mesa_reference_texobj(&dst->Texture.Unit[i].CurrentTex[tex],
123                                src->Texture.Unit[i].CurrentTex[tex]);
124      }
125
126      _mesa_unlock_context_textures(dst);
127   }
128}
129
130
131/*
132 * For debugging
133 */
134void
135_mesa_print_texunit_state( GLcontext *ctx, GLuint unit )
136{
137   const struct gl_texture_unit *texUnit = ctx->Texture.Unit + unit;
138   _mesa_printf("Texture Unit %d\n", unit);
139   _mesa_printf("  GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit->EnvMode));
140   _mesa_printf("  GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeRGB));
141   _mesa_printf("  GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeA));
142   _mesa_printf("  GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[0]));
143   _mesa_printf("  GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[1]));
144   _mesa_printf("  GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[2]));
145   _mesa_printf("  GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[0]));
146   _mesa_printf("  GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[1]));
147   _mesa_printf("  GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[2]));
148   _mesa_printf("  GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[0]));
149   _mesa_printf("  GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[1]));
150   _mesa_printf("  GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[2]));
151   _mesa_printf("  GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[0]));
152   _mesa_printf("  GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[1]));
153   _mesa_printf("  GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[2]));
154   _mesa_printf("  GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
155   _mesa_printf("  GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
156   _mesa_printf("  GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
157}
158
159
160
161/**********************************************************************/
162/*                       Texture Environment                          */
163/**********************************************************************/
164
165/**
166 * Convert "classic" texture environment to ARB_texture_env_combine style
167 * environments.
168 *
169 * \param state  texture_env_combine state vector to be filled-in.
170 * \param mode   Classic texture environment mode (i.e., \c GL_REPLACE,
171 *               \c GL_BLEND, \c GL_DECAL, etc.).
172 * \param texBaseFormat  Base format of the texture associated with the
173 *               texture unit.
174 */
175static void
176calculate_derived_texenv( struct gl_tex_env_combine_state *state,
177			  GLenum mode, GLenum texBaseFormat )
178{
179   GLenum mode_rgb;
180   GLenum mode_a;
181
182   *state = default_combine_state;
183
184   switch (texBaseFormat) {
185   case GL_ALPHA:
186      state->SourceRGB[0] = GL_PREVIOUS;
187      break;
188
189   case GL_LUMINANCE_ALPHA:
190   case GL_INTENSITY:
191   case GL_RGBA:
192      break;
193
194   case GL_LUMINANCE:
195   case GL_RGB:
196   case GL_YCBCR_MESA:
197      state->SourceA[0] = GL_PREVIOUS;
198      break;
199
200   default:
201      _mesa_problem(NULL, "Invalid texBaseFormat in calculate_derived_texenv");
202      return;
203   }
204
205   if (mode == GL_REPLACE_EXT)
206      mode = GL_REPLACE;
207
208   switch (mode) {
209   case GL_REPLACE:
210   case GL_MODULATE:
211      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
212      mode_a   = mode;
213      break;
214
215   case GL_DECAL:
216      mode_rgb = GL_INTERPOLATE;
217      mode_a   = GL_REPLACE;
218
219      state->SourceA[0] = GL_PREVIOUS;
220
221      /* Having alpha / luminance / intensity textures replace using the
222       * incoming fragment color matches the definition in NV_texture_shader.
223       * The 1.5 spec simply marks these as "undefined".
224       */
225      switch (texBaseFormat) {
226      case GL_ALPHA:
227      case GL_LUMINANCE:
228      case GL_LUMINANCE_ALPHA:
229      case GL_INTENSITY:
230	 state->SourceRGB[0] = GL_PREVIOUS;
231	 break;
232      case GL_RGB:
233      case GL_YCBCR_MESA:
234	 mode_rgb = GL_REPLACE;
235	 break;
236      case GL_RGBA:
237	 state->SourceRGB[2] = GL_TEXTURE;
238	 break;
239      }
240      break;
241
242   case GL_BLEND:
243      mode_rgb = GL_INTERPOLATE;
244      mode_a   = GL_MODULATE;
245
246      switch (texBaseFormat) {
247      case GL_ALPHA:
248	 mode_rgb = GL_REPLACE;
249	 break;
250      case GL_INTENSITY:
251	 mode_a = GL_INTERPOLATE;
252	 state->SourceA[0] = GL_CONSTANT;
253	 state->OperandA[2] = GL_SRC_ALPHA;
254	 /* FALLTHROUGH */
255      case GL_LUMINANCE:
256      case GL_RGB:
257      case GL_LUMINANCE_ALPHA:
258      case GL_RGBA:
259      case GL_YCBCR_MESA:
260	 state->SourceRGB[2] = GL_TEXTURE;
261	 state->SourceA[2]   = GL_TEXTURE;
262	 state->SourceRGB[0] = GL_CONSTANT;
263	 state->OperandRGB[2] = GL_SRC_COLOR;
264	 break;
265      }
266      break;
267
268   case GL_ADD:
269      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
270      mode_a   = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
271      break;
272
273   default:
274      _mesa_problem(NULL,
275                    "Invalid texture env mode in calculate_derived_texenv");
276      return;
277   }
278
279   state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
280       ? mode_rgb : GL_REPLACE;
281   state->ModeA   = (state->SourceA[0]   != GL_PREVIOUS)
282       ? mode_a   : GL_REPLACE;
283}
284
285
286
287
288/* GL_ARB_multitexture */
289void GLAPIENTRY
290_mesa_ActiveTextureARB(GLenum texture)
291{
292   GET_CURRENT_CONTEXT(ctx);
293   const GLuint texUnit = texture - GL_TEXTURE0;
294   ASSERT_OUTSIDE_BEGIN_END(ctx);
295
296   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
297      _mesa_debug(ctx, "glActiveTexture %s\n",
298                  _mesa_lookup_enum_by_nr(texture));
299
300   if (texUnit >= ctx->Const.MaxTextureImageUnits) {
301      _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(texture)");
302      return;
303   }
304
305   if (ctx->Texture.CurrentUnit == texUnit)
306      return;
307
308   FLUSH_VERTICES(ctx, _NEW_TEXTURE);
309
310   ctx->Texture.CurrentUnit = texUnit;
311   if (ctx->Transform.MatrixMode == GL_TEXTURE) {
312      /* update current stack pointer */
313      ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
314   }
315
316   if (ctx->Driver.ActiveTexture) {
317      (*ctx->Driver.ActiveTexture)( ctx, (GLuint) texUnit );
318   }
319}
320
321
322/* GL_ARB_multitexture */
323void GLAPIENTRY
324_mesa_ClientActiveTextureARB(GLenum texture)
325{
326   GET_CURRENT_CONTEXT(ctx);
327   GLuint texUnit = texture - GL_TEXTURE0;
328   ASSERT_OUTSIDE_BEGIN_END(ctx);
329
330   if (texUnit >= ctx->Const.MaxTextureCoordUnits) {
331      _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(texture)");
332      return;
333   }
334
335   FLUSH_VERTICES(ctx, _NEW_ARRAY);
336   ctx->Array.ActiveTexture = texUnit;
337}
338
339
340
341/**********************************************************************/
342/*****                    State management                        *****/
343/**********************************************************************/
344
345
346/**
347 * \note This routine refers to derived texture attribute values to
348 * compute the ENABLE_TEXMAT flags, but is only called on
349 * _NEW_TEXTURE_MATRIX.  On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
350 * flags are updated by _mesa_update_textures(), below.
351 *
352 * \param ctx GL context.
353 */
354static void
355update_texture_matrices( GLcontext *ctx )
356{
357   GLuint i;
358
359   ctx->Texture._TexMatEnabled = 0;
360
361   for (i=0; i < ctx->Const.MaxTextureCoordUnits; i++) {
362      if (_math_matrix_is_dirty(ctx->TextureMatrixStack[i].Top)) {
363	 _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
364
365	 if (ctx->Texture.Unit[i]._ReallyEnabled &&
366	     ctx->TextureMatrixStack[i].Top->type != MATRIX_IDENTITY)
367	    ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(i);
368
369	 if (ctx->Driver.TextureMatrix)
370	    ctx->Driver.TextureMatrix( ctx, i, ctx->TextureMatrixStack[i].Top);
371      }
372   }
373}
374
375
376/**
377 * Update texture object's _Function field.  We need to do this
378 * whenever any of the texture object's shadow-related fields change
379 * or when we start/stop using a fragment program.
380 *
381 * This function could be expanded someday to update additional per-object
382 * fields that depend on assorted state changes.
383 */
384static void
385update_texture_compare_function(GLcontext *ctx,
386                                struct gl_texture_object *tObj)
387{
388   /* XXX temporarily disable this test since it breaks the GLSL
389    * shadow2D(), etc. functions.
390    */
391   if (0 /*ctx->FragmentProgram._Current*/) {
392      /* Texel/coordinate comparison is ignored for programs.
393       * See GL_ARB_fragment_program/shader spec for details.
394       */
395      tObj->_Function = GL_NONE;
396   }
397   else if (tObj->CompareFlag) {
398      /* GL_SGIX_shadow */
399      if (tObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
400         tObj->_Function = GL_LEQUAL;
401      }
402      else {
403         ASSERT(tObj->CompareOperator == GL_TEXTURE_GEQUAL_R_SGIX);
404         tObj->_Function = GL_GEQUAL;
405      }
406   }
407   else if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
408      /* GL_ARB_shadow */
409      tObj->_Function = tObj->CompareFunc;
410   }
411   else {
412      tObj->_Function = GL_NONE;  /* pass depth through as grayscale */
413   }
414}
415
416
417/**
418 * Helper function for determining which texture object (1D, 2D, cube, etc)
419 * should actually be used.
420 */
421static void
422texture_override(GLcontext *ctx,
423                 struct gl_texture_unit *texUnit, GLbitfield enableBits,
424                 struct gl_texture_object *texObj, GLuint textureBit)
425{
426   if (!texUnit->_ReallyEnabled && (enableBits & textureBit)) {
427      if (!texObj->_Complete) {
428         _mesa_test_texobj_completeness(ctx, texObj);
429      }
430      if (texObj->_Complete) {
431         texUnit->_ReallyEnabled = textureBit;
432         _mesa_reference_texobj(&texUnit->_Current, texObj);
433         update_texture_compare_function(ctx, texObj);
434      }
435   }
436}
437
438
439/**
440 * \note This routine refers to derived texture matrix values to
441 * compute the ENABLE_TEXMAT flags, but is only called on
442 * _NEW_TEXTURE.  On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
443 * flags are updated by _mesa_update_texture_matrices, above.
444 *
445 * \param ctx GL context.
446 */
447static void
448update_texture_state( GLcontext *ctx )
449{
450   GLuint unit;
451   struct gl_fragment_program *fprog = NULL;
452   struct gl_vertex_program *vprog = NULL;
453
454   if (ctx->Shader.CurrentProgram &&
455       ctx->Shader.CurrentProgram->LinkStatus) {
456      fprog = ctx->Shader.CurrentProgram->FragmentProgram;
457      vprog = ctx->Shader.CurrentProgram->VertexProgram;
458   }
459   else {
460      if (ctx->FragmentProgram._Enabled) {
461         fprog = ctx->FragmentProgram.Current;
462      }
463      if (ctx->VertexProgram._Enabled) {
464         /* XXX enable this if/when non-shader vertex programs get
465          * texture fetches:
466         vprog = ctx->VertexProgram.Current;
467         */
468      }
469   }
470
471   ctx->NewState |= _NEW_TEXTURE; /* TODO: only set this if there are
472				   * actual changes.
473				   */
474
475   ctx->Texture._EnabledUnits = 0;
476   ctx->Texture._GenFlags = 0;
477   ctx->Texture._TexMatEnabled = 0;
478   ctx->Texture._TexGenEnabled = 0;
479
480   /*
481    * Update texture unit state.
482    */
483   for (unit = 0; unit < ctx->Const.MaxTextureImageUnits; unit++) {
484      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
485      GLbitfield enableBits;
486      GLuint tex;
487
488      texUnit->_ReallyEnabled = 0;
489      texUnit->_GenFlags = 0;
490
491      /* Get the bitmask of texture target enables.
492       * enableBits will be a mask of the TEXTURE_*_BIT flags indicating
493       * which texture targets are enabled (fixed function) or referenced
494       * by a fragment shader/program.  When multiple flags are set, we'll
495       * settle on the one with highest priority (see texture_override below).
496       */
497      enableBits = 0x0;
498      if (vprog) {
499         enableBits |= vprog->Base.TexturesUsed[unit];
500      }
501      if (fprog) {
502         enableBits |= fprog->Base.TexturesUsed[unit];
503      }
504      else {
505         /* fixed-function fragment program */
506         enableBits |= texUnit->Enabled;
507      }
508
509      if (enableBits == 0x0)
510         continue;
511
512      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
513         ASSERT(texUnit->CurrentTex[tex]);
514      }
515
516      /* Look for the highest-priority texture target that's enabled and
517       * complete.  That's the one we'll use for texturing.  If we're using
518       * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
519       */
520      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
521         /* texture indexes from highest to lowest priority */
522         static const GLuint targets[NUM_TEXTURE_TARGETS] = {
523            TEXTURE_2D_ARRAY_INDEX,
524            TEXTURE_1D_ARRAY_INDEX,
525            TEXTURE_CUBE_INDEX,
526            TEXTURE_3D_INDEX,
527            TEXTURE_RECT_INDEX,
528            TEXTURE_2D_INDEX,
529            TEXTURE_1D_INDEX
530         };
531         GLuint texIndex = targets[tex];
532         texture_override(ctx, texUnit, enableBits,
533                          texUnit->CurrentTex[texIndex], 1 << texIndex);
534      }
535
536      if (!texUnit->_ReallyEnabled) {
537         continue;
538      }
539
540      if (texUnit->_ReallyEnabled)
541         ctx->Texture._EnabledUnits |= (1 << unit);
542
543      if (texUnit->EnvMode == GL_COMBINE) {
544	 texUnit->_CurrentCombine = & texUnit->Combine;
545      }
546      else {
547         const struct gl_texture_object *texObj = texUnit->_Current;
548         GLenum format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
549         if (format == GL_COLOR_INDEX) {
550            format = GL_RGBA;  /* a bit of a hack */
551         }
552         else if (format == GL_DEPTH_COMPONENT
553                  || format == GL_DEPTH_STENCIL_EXT) {
554            format = texObj->DepthMode;
555         }
556	 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
557	 texUnit->_CurrentCombine = & texUnit->_EnvMode;
558      }
559
560      switch (texUnit->_CurrentCombine->ModeRGB) {
561      case GL_REPLACE:
562	 texUnit->_CurrentCombine->_NumArgsRGB = 1;
563	 break;
564      case GL_MODULATE:
565      case GL_ADD:
566      case GL_ADD_SIGNED:
567      case GL_SUBTRACT:
568      case GL_DOT3_RGB:
569      case GL_DOT3_RGBA:
570      case GL_DOT3_RGB_EXT:
571      case GL_DOT3_RGBA_EXT:
572	 texUnit->_CurrentCombine->_NumArgsRGB = 2;
573	 break;
574      case GL_INTERPOLATE:
575      case GL_MODULATE_ADD_ATI:
576      case GL_MODULATE_SIGNED_ADD_ATI:
577      case GL_MODULATE_SUBTRACT_ATI:
578	 texUnit->_CurrentCombine->_NumArgsRGB = 3;
579	 break;
580      default:
581	 texUnit->_CurrentCombine->_NumArgsRGB = 0;
582         _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
583         return;
584      }
585
586      switch (texUnit->_CurrentCombine->ModeA) {
587      case GL_REPLACE:
588	 texUnit->_CurrentCombine->_NumArgsA = 1;
589	 break;
590      case GL_MODULATE:
591      case GL_ADD:
592      case GL_ADD_SIGNED:
593      case GL_SUBTRACT:
594	 texUnit->_CurrentCombine->_NumArgsA = 2;
595	 break;
596      case GL_INTERPOLATE:
597      case GL_MODULATE_ADD_ATI:
598      case GL_MODULATE_SIGNED_ADD_ATI:
599      case GL_MODULATE_SUBTRACT_ATI:
600	 texUnit->_CurrentCombine->_NumArgsA = 3;
601	 break;
602      default:
603	 texUnit->_CurrentCombine->_NumArgsA = 0;
604         _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
605	 break;
606      }
607   }
608
609   /* Determine which texture coordinate sets are actually needed */
610   if (fprog) {
611      const GLuint coordMask = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
612      ctx->Texture._EnabledCoordUnits
613         = (fprog->Base.InputsRead >> FRAG_ATTRIB_TEX0) & coordMask;
614   }
615   else {
616      ctx->Texture._EnabledCoordUnits = ctx->Texture._EnabledUnits;
617   }
618
619   /* Setup texgen for those texture coordinate sets that are in use */
620   for (unit = 0; unit < ctx->Const.MaxTextureCoordUnits; unit++) {
621      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
622
623      if (!(ctx->Texture._EnabledCoordUnits & (1 << unit)))
624	 continue;
625
626      if (texUnit->TexGenEnabled) {
627	 if (texUnit->TexGenEnabled & S_BIT) {
628	    texUnit->_GenFlags |= texUnit->_GenBitS;
629	 }
630	 if (texUnit->TexGenEnabled & T_BIT) {
631	    texUnit->_GenFlags |= texUnit->_GenBitT;
632	 }
633	 if (texUnit->TexGenEnabled & Q_BIT) {
634	    texUnit->_GenFlags |= texUnit->_GenBitQ;
635	 }
636	 if (texUnit->TexGenEnabled & R_BIT) {
637	    texUnit->_GenFlags |= texUnit->_GenBitR;
638	 }
639
640	 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
641	 ctx->Texture._GenFlags |= texUnit->_GenFlags;
642      }
643
644      if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
645	 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
646   }
647}
648
649
650/**
651 * Update texture-related derived state.
652 */
653void
654_mesa_update_texture( GLcontext *ctx, GLuint new_state )
655{
656   if (new_state & _NEW_TEXTURE_MATRIX)
657      update_texture_matrices( ctx );
658
659   if (new_state & (_NEW_TEXTURE | _NEW_PROGRAM))
660      update_texture_state( ctx );
661}
662
663
664/**********************************************************************/
665/*****                      Initialization                        *****/
666/**********************************************************************/
667
668/**
669 * Allocate the proxy textures for the given context.
670 *
671 * \param ctx the context to allocate proxies for.
672 *
673 * \return GL_TRUE on success, or GL_FALSE on failure
674 *
675 * If run out of memory part way through the allocations, clean up and return
676 * GL_FALSE.
677 */
678static GLboolean
679alloc_proxy_textures( GLcontext *ctx )
680{
681   static const GLenum targets[] = {
682      GL_TEXTURE_1D,
683      GL_TEXTURE_2D,
684      GL_TEXTURE_3D,
685      GL_TEXTURE_CUBE_MAP_ARB,
686      GL_TEXTURE_RECTANGLE_NV,
687      GL_TEXTURE_1D_ARRAY_EXT,
688      GL_TEXTURE_2D_ARRAY_EXT
689   };
690   GLint tgt;
691
692   ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
693
694   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
695      if (!(ctx->Texture.ProxyTex[tgt]
696            = ctx->Driver.NewTextureObject(ctx, 0, targets[tgt]))) {
697         /* out of memory, free what we did allocate */
698         while (--tgt >= 0) {
699            ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
700         }
701         return GL_FALSE;
702      }
703   }
704
705   assert(ctx->Texture.ProxyTex[0]->RefCount == 1); /* sanity check */
706   return GL_TRUE;
707}
708
709
710/**
711 * Initialize a texture unit.
712 *
713 * \param ctx GL context.
714 * \param unit texture unit number to be initialized.
715 */
716static void
717init_texture_unit( GLcontext *ctx, GLuint unit )
718{
719   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
720   GLuint tex;
721
722   texUnit->EnvMode = GL_MODULATE;
723   ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
724
725   texUnit->Combine = default_combine_state;
726   texUnit->_EnvMode = default_combine_state;
727   texUnit->_CurrentCombine = & texUnit->_EnvMode;
728
729   texUnit->TexGenEnabled = 0;
730   texUnit->GenModeS = GL_EYE_LINEAR;
731   texUnit->GenModeT = GL_EYE_LINEAR;
732   texUnit->GenModeR = GL_EYE_LINEAR;
733   texUnit->GenModeQ = GL_EYE_LINEAR;
734   texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
735   texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
736   texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
737   texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
738
739   /* Yes, these plane coefficients are correct! */
740   ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
741   ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
742   ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
743   ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
744   ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
745   ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
746   ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
747   ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
748
749   /* initialize current texture object ptrs to the shared default objects */
750   for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
751      _mesa_reference_texobj(&texUnit->CurrentTex[tex],
752                             ctx->Shared->DefaultTex[tex]);
753   }
754}
755
756
757/**
758 * Initialize texture state for the given context.
759 */
760GLboolean
761_mesa_init_texture(GLcontext *ctx)
762{
763   GLuint i;
764
765   assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
766   assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
767
768   /* Texture group */
769   ctx->Texture.CurrentUnit = 0;      /* multitexture */
770   ctx->Texture._EnabledUnits = 0;
771   ctx->Texture.SharedPalette = GL_FALSE;
772#if FEATURE_colortable
773   _mesa_init_colortable(&ctx->Texture.Palette);
774#endif
775
776   for (i = 0; i < MAX_TEXTURE_UNITS; i++)
777      init_texture_unit( ctx, i );
778
779   /* After we're done initializing the context's texture state the default
780    * texture objects' refcounts should be at least MAX_TEXTURE_UNITS + 1.
781    */
782   assert(ctx->Shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount
783          >= MAX_TEXTURE_UNITS + 1);
784
785   /* Allocate proxy textures */
786   if (!alloc_proxy_textures( ctx ))
787      return GL_FALSE;
788
789   return GL_TRUE;
790}
791
792
793/**
794 * Free dynamically-allocted texture data attached to the given context.
795 */
796void
797_mesa_free_texture_data(GLcontext *ctx)
798{
799   GLuint u, tgt;
800
801   /* unreference current textures */
802   for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
803      struct gl_texture_unit *unit = ctx->Texture.Unit + u;
804      /* The _Current texture could account for another reference */
805      _mesa_reference_texobj(&ctx->Texture.Unit[u]._Current, NULL);
806
807      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
808         _mesa_reference_texobj(&unit->CurrentTex[tgt], NULL);
809      }
810   }
811
812   /* Free proxy texture objects */
813   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++)
814      ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
815
816#if FEATURE_colortable
817   {
818      GLuint i;
819      for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
820         _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
821   }
822#endif
823}
824
825
826/**
827 * Update the default texture objects in the given context to reference those
828 * specified in the shared state and release those referencing the old
829 * shared state.
830 */
831void
832_mesa_update_default_objects_texture(GLcontext *ctx)
833{
834   GLuint i, tex;
835
836   for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
837      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
838      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
839         _mesa_reference_texobj(&texUnit->CurrentTex[tex],
840                                ctx->Shared->DefaultTex[tex]);
841      }
842   }
843}
844