texstate.c revision c1f859d4
17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
3c1f859d4Smrg * Version:  7.1
47117f1b4Smrg *
57117f1b4Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
67117f1b4Smrg *
77117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
87117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
97117f1b4Smrg * to deal in the Software without restriction, including without limitation
107117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
127117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
137117f1b4Smrg *
147117f1b4Smrg * The above copyright notice and this permission notice shall be included
157117f1b4Smrg * in all copies or substantial portions of the Software.
167117f1b4Smrg *
177117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
187117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
207117f1b4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
217117f1b4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
227117f1b4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg */
247117f1b4Smrg
257117f1b4Smrg/**
267117f1b4Smrg * \file texstate.c
277117f1b4Smrg *
287117f1b4Smrg * Texture state handling.
297117f1b4Smrg */
307117f1b4Smrg
317117f1b4Smrg#include "glheader.h"
327117f1b4Smrg#include "colormac.h"
33c1f859d4Smrg#if FEATURE_colortable
347117f1b4Smrg#include "colortab.h"
35c1f859d4Smrg#endif
367117f1b4Smrg#include "context.h"
377117f1b4Smrg#include "enums.h"
387117f1b4Smrg#include "macros.h"
397117f1b4Smrg#include "texcompress.h"
407117f1b4Smrg#include "texobj.h"
417117f1b4Smrg#include "teximage.h"
427117f1b4Smrg#include "texstate.h"
437117f1b4Smrg#include "texenvprogram.h"
447117f1b4Smrg#include "mtypes.h"
457117f1b4Smrg#include "math/m_xform.h"
467117f1b4Smrg
477117f1b4Smrg
487117f1b4Smrg
497117f1b4Smrg/**
507117f1b4Smrg * Default texture combine environment state.  This is used to initialize
517117f1b4Smrg * a context's texture units and as the basis for converting "classic"
527117f1b4Smrg * texture environmnets to ARB_texture_env_combine style values.
537117f1b4Smrg */
547117f1b4Smrgstatic const struct gl_tex_env_combine_state default_combine_state = {
557117f1b4Smrg   GL_MODULATE, GL_MODULATE,
567117f1b4Smrg   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
577117f1b4Smrg   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
587117f1b4Smrg   { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA },
597117f1b4Smrg   { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
607117f1b4Smrg   0, 0,
617117f1b4Smrg   2, 2
627117f1b4Smrg};
637117f1b4Smrg
647117f1b4Smrg
657117f1b4Smrg
667117f1b4Smrg/**
677117f1b4Smrg * Used by glXCopyContext to copy texture state from one context to another.
687117f1b4Smrg */
697117f1b4Smrgvoid
707117f1b4Smrg_mesa_copy_texture_state( const GLcontext *src, GLcontext *dst )
717117f1b4Smrg{
72c1f859d4Smrg   GLuint i, tex;
737117f1b4Smrg
747117f1b4Smrg   ASSERT(src);
757117f1b4Smrg   ASSERT(dst);
767117f1b4Smrg
777117f1b4Smrg   dst->Texture.CurrentUnit = src->Texture.CurrentUnit;
787117f1b4Smrg   dst->Texture._GenFlags = src->Texture._GenFlags;
797117f1b4Smrg   dst->Texture._TexGenEnabled = src->Texture._TexGenEnabled;
807117f1b4Smrg   dst->Texture._TexMatEnabled = src->Texture._TexMatEnabled;
817117f1b4Smrg   dst->Texture.SharedPalette = src->Texture.SharedPalette;
827117f1b4Smrg
837117f1b4Smrg   /* per-unit state */
84c1f859d4Smrg   for (i = 0; i < src->Const.MaxTextureImageUnits; i++) {
857117f1b4Smrg      dst->Texture.Unit[i].Enabled = src->Texture.Unit[i].Enabled;
867117f1b4Smrg      dst->Texture.Unit[i].EnvMode = src->Texture.Unit[i].EnvMode;
877117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].EnvColor, src->Texture.Unit[i].EnvColor);
887117f1b4Smrg      dst->Texture.Unit[i].TexGenEnabled = src->Texture.Unit[i].TexGenEnabled;
897117f1b4Smrg      dst->Texture.Unit[i].GenModeS = src->Texture.Unit[i].GenModeS;
907117f1b4Smrg      dst->Texture.Unit[i].GenModeT = src->Texture.Unit[i].GenModeT;
917117f1b4Smrg      dst->Texture.Unit[i].GenModeR = src->Texture.Unit[i].GenModeR;
927117f1b4Smrg      dst->Texture.Unit[i].GenModeQ = src->Texture.Unit[i].GenModeQ;
937117f1b4Smrg      dst->Texture.Unit[i]._GenBitS = src->Texture.Unit[i]._GenBitS;
947117f1b4Smrg      dst->Texture.Unit[i]._GenBitT = src->Texture.Unit[i]._GenBitT;
957117f1b4Smrg      dst->Texture.Unit[i]._GenBitR = src->Texture.Unit[i]._GenBitR;
967117f1b4Smrg      dst->Texture.Unit[i]._GenBitQ = src->Texture.Unit[i]._GenBitQ;
977117f1b4Smrg      dst->Texture.Unit[i]._GenFlags = src->Texture.Unit[i]._GenFlags;
987117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].ObjectPlaneS, src->Texture.Unit[i].ObjectPlaneS);
997117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].ObjectPlaneT, src->Texture.Unit[i].ObjectPlaneT);
1007117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].ObjectPlaneR, src->Texture.Unit[i].ObjectPlaneR);
1017117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].ObjectPlaneQ, src->Texture.Unit[i].ObjectPlaneQ);
1027117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].EyePlaneS, src->Texture.Unit[i].EyePlaneS);
1037117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].EyePlaneT, src->Texture.Unit[i].EyePlaneT);
1047117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].EyePlaneR, src->Texture.Unit[i].EyePlaneR);
1057117f1b4Smrg      COPY_4V(dst->Texture.Unit[i].EyePlaneQ, src->Texture.Unit[i].EyePlaneQ);
1067117f1b4Smrg      dst->Texture.Unit[i].LodBias = src->Texture.Unit[i].LodBias;
1077117f1b4Smrg
1087117f1b4Smrg      /* GL_EXT_texture_env_combine */
1097117f1b4Smrg      dst->Texture.Unit[i].Combine.ModeRGB = src->Texture.Unit[i].Combine.ModeRGB;
1107117f1b4Smrg      dst->Texture.Unit[i].Combine.ModeA = src->Texture.Unit[i].Combine.ModeA;
1117117f1b4Smrg      COPY_3V(dst->Texture.Unit[i].Combine.SourceRGB, src->Texture.Unit[i].Combine.SourceRGB);
1127117f1b4Smrg      COPY_3V(dst->Texture.Unit[i].Combine.SourceA, src->Texture.Unit[i].Combine.SourceA);
1137117f1b4Smrg      COPY_3V(dst->Texture.Unit[i].Combine.OperandRGB, src->Texture.Unit[i].Combine.OperandRGB);
1147117f1b4Smrg      COPY_3V(dst->Texture.Unit[i].Combine.OperandA, src->Texture.Unit[i].Combine.OperandA);
1157117f1b4Smrg      dst->Texture.Unit[i].Combine.ScaleShiftRGB = src->Texture.Unit[i].Combine.ScaleShiftRGB;
1167117f1b4Smrg      dst->Texture.Unit[i].Combine.ScaleShiftA = src->Texture.Unit[i].Combine.ScaleShiftA;
1177117f1b4Smrg
1187117f1b4Smrg      /* copy texture object bindings, not contents of texture objects */
1197117f1b4Smrg      _mesa_lock_context_textures(dst);
1207117f1b4Smrg
121c1f859d4Smrg      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
122c1f859d4Smrg         _mesa_reference_texobj(&dst->Texture.Unit[i].CurrentTex[tex],
123c1f859d4Smrg                                src->Texture.Unit[i].CurrentTex[tex]);
124c1f859d4Smrg      }
1257117f1b4Smrg
1267117f1b4Smrg      _mesa_unlock_context_textures(dst);
1277117f1b4Smrg   }
1287117f1b4Smrg}
1297117f1b4Smrg
1307117f1b4Smrg
1317117f1b4Smrg/*
1327117f1b4Smrg * For debugging
1337117f1b4Smrg */
1347117f1b4Smrgvoid
1357117f1b4Smrg_mesa_print_texunit_state( GLcontext *ctx, GLuint unit )
1367117f1b4Smrg{
1377117f1b4Smrg   const struct gl_texture_unit *texUnit = ctx->Texture.Unit + unit;
1387117f1b4Smrg   _mesa_printf("Texture Unit %d\n", unit);
1397117f1b4Smrg   _mesa_printf("  GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit->EnvMode));
1407117f1b4Smrg   _mesa_printf("  GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeRGB));
1417117f1b4Smrg   _mesa_printf("  GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeA));
1427117f1b4Smrg   _mesa_printf("  GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[0]));
1437117f1b4Smrg   _mesa_printf("  GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[1]));
1447117f1b4Smrg   _mesa_printf("  GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[2]));
1457117f1b4Smrg   _mesa_printf("  GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[0]));
1467117f1b4Smrg   _mesa_printf("  GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[1]));
1477117f1b4Smrg   _mesa_printf("  GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[2]));
1487117f1b4Smrg   _mesa_printf("  GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[0]));
1497117f1b4Smrg   _mesa_printf("  GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[1]));
1507117f1b4Smrg   _mesa_printf("  GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[2]));
1517117f1b4Smrg   _mesa_printf("  GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[0]));
1527117f1b4Smrg   _mesa_printf("  GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[1]));
1537117f1b4Smrg   _mesa_printf("  GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[2]));
1547117f1b4Smrg   _mesa_printf("  GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
1557117f1b4Smrg   _mesa_printf("  GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
1567117f1b4Smrg   _mesa_printf("  GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
157c1f859d4Smrg}
1587117f1b4Smrg
1597117f1b4Smrg
1607117f1b4Smrg
161c1f859d4Smrg/**********************************************************************/
162c1f859d4Smrg/*                       Texture Environment                          */
163c1f859d4Smrg/**********************************************************************/
1647117f1b4Smrg
165c1f859d4Smrg/**
166c1f859d4Smrg * Convert "classic" texture environment to ARB_texture_env_combine style
167c1f859d4Smrg * environments.
168c1f859d4Smrg *
169c1f859d4Smrg * \param state  texture_env_combine state vector to be filled-in.
170c1f859d4Smrg * \param mode   Classic texture environment mode (i.e., \c GL_REPLACE,
171c1f859d4Smrg *               \c GL_BLEND, \c GL_DECAL, etc.).
172c1f859d4Smrg * \param texBaseFormat  Base format of the texture associated with the
173c1f859d4Smrg *               texture unit.
174c1f859d4Smrg */
175c1f859d4Smrgstatic void
176c1f859d4Smrgcalculate_derived_texenv( struct gl_tex_env_combine_state *state,
177c1f859d4Smrg			  GLenum mode, GLenum texBaseFormat )
178c1f859d4Smrg{
179c1f859d4Smrg   GLenum mode_rgb;
180c1f859d4Smrg   GLenum mode_a;
1817117f1b4Smrg
182c1f859d4Smrg   *state = default_combine_state;
1837117f1b4Smrg
184c1f859d4Smrg   switch (texBaseFormat) {
185c1f859d4Smrg   case GL_ALPHA:
186c1f859d4Smrg      state->SourceRGB[0] = GL_PREVIOUS;
187c1f859d4Smrg      break;
1887117f1b4Smrg
189c1f859d4Smrg   case GL_LUMINANCE_ALPHA:
190c1f859d4Smrg   case GL_INTENSITY:
191c1f859d4Smrg   case GL_RGBA:
192c1f859d4Smrg      break;
1937117f1b4Smrg
194c1f859d4Smrg   case GL_LUMINANCE:
195c1f859d4Smrg   case GL_RGB:
196c1f859d4Smrg   case GL_YCBCR_MESA:
197c1f859d4Smrg      state->SourceA[0] = GL_PREVIOUS;
198c1f859d4Smrg      break;
199c1f859d4Smrg
200c1f859d4Smrg   default:
201c1f859d4Smrg      _mesa_problem(NULL, "Invalid texBaseFormat in calculate_derived_texenv");
2027117f1b4Smrg      return;
2037117f1b4Smrg   }
2047117f1b4Smrg
205c1f859d4Smrg   if (mode == GL_REPLACE_EXT)
206c1f859d4Smrg      mode = GL_REPLACE;
2077117f1b4Smrg
208c1f859d4Smrg   switch (mode) {
209c1f859d4Smrg   case GL_REPLACE:
210c1f859d4Smrg   case GL_MODULATE:
211c1f859d4Smrg      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
212c1f859d4Smrg      mode_a   = mode;
213c1f859d4Smrg      break;
214c1f859d4Smrg
215c1f859d4Smrg   case GL_DECAL:
216c1f859d4Smrg      mode_rgb = GL_INTERPOLATE;
217c1f859d4Smrg      mode_a   = GL_REPLACE;
218c1f859d4Smrg
219c1f859d4Smrg      state->SourceA[0] = GL_PREVIOUS;
220c1f859d4Smrg
221c1f859d4Smrg      /* Having alpha / luminance / intensity textures replace using the
222c1f859d4Smrg       * incoming fragment color matches the definition in NV_texture_shader.
223c1f859d4Smrg       * The 1.5 spec simply marks these as "undefined".
224c1f859d4Smrg       */
225c1f859d4Smrg      switch (texBaseFormat) {
226c1f859d4Smrg      case GL_ALPHA:
227c1f859d4Smrg      case GL_LUMINANCE:
228c1f859d4Smrg      case GL_LUMINANCE_ALPHA:
229c1f859d4Smrg      case GL_INTENSITY:
230c1f859d4Smrg	 state->SourceRGB[0] = GL_PREVIOUS;
2317117f1b4Smrg	 break;
232c1f859d4Smrg      case GL_RGB:
233c1f859d4Smrg      case GL_YCBCR_MESA:
234c1f859d4Smrg	 mode_rgb = GL_REPLACE;
2357117f1b4Smrg	 break;
236c1f859d4Smrg      case GL_RGBA:
237c1f859d4Smrg	 state->SourceRGB[2] = GL_TEXTURE;
2387117f1b4Smrg	 break;
239c1f859d4Smrg      }
240c1f859d4Smrg      break;
2417117f1b4Smrg
242c1f859d4Smrg   case GL_BLEND:
243c1f859d4Smrg      mode_rgb = GL_INTERPOLATE;
244c1f859d4Smrg      mode_a   = GL_MODULATE;
2457117f1b4Smrg
246c1f859d4Smrg      switch (texBaseFormat) {
247c1f859d4Smrg      case GL_ALPHA:
248c1f859d4Smrg	 mode_rgb = GL_REPLACE;
249c1f859d4Smrg	 break;
250c1f859d4Smrg      case GL_INTENSITY:
251c1f859d4Smrg	 mode_a = GL_INTERPOLATE;
252c1f859d4Smrg	 state->SourceA[0] = GL_CONSTANT;
253c1f859d4Smrg	 state->OperandA[2] = GL_SRC_ALPHA;
254c1f859d4Smrg	 /* FALLTHROUGH */
255c1f859d4Smrg      case GL_LUMINANCE:
256c1f859d4Smrg      case GL_RGB:
257c1f859d4Smrg      case GL_LUMINANCE_ALPHA:
258c1f859d4Smrg      case GL_RGBA:
259c1f859d4Smrg      case GL_YCBCR_MESA:
260c1f859d4Smrg	 state->SourceRGB[2] = GL_TEXTURE;
261c1f859d4Smrg	 state->SourceA[2]   = GL_TEXTURE;
262c1f859d4Smrg	 state->SourceRGB[0] = GL_CONSTANT;
263c1f859d4Smrg	 state->OperandRGB[2] = GL_SRC_COLOR;
264c1f859d4Smrg	 break;
265c1f859d4Smrg      }
266c1f859d4Smrg      break;
2677117f1b4Smrg
268c1f859d4Smrg   case GL_ADD:
269c1f859d4Smrg      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
270c1f859d4Smrg      mode_a   = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
271c1f859d4Smrg      break;
2727117f1b4Smrg
273c1f859d4Smrg   default:
274c1f859d4Smrg      _mesa_problem(NULL,
275c1f859d4Smrg                    "Invalid texture env mode in calculate_derived_texenv");
2767117f1b4Smrg      return;
2777117f1b4Smrg   }
278c1f859d4Smrg
279c1f859d4Smrg   state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
280c1f859d4Smrg       ? mode_rgb : GL_REPLACE;
281c1f859d4Smrg   state->ModeA   = (state->SourceA[0]   != GL_PREVIOUS)
282c1f859d4Smrg       ? mode_a   : GL_REPLACE;
283c1f859d4Smrg}
2847117f1b4Smrg
2857117f1b4Smrg
2867117f1b4Smrg
2877117f1b4Smrg
2887117f1b4Smrg/* GL_ARB_multitexture */
2897117f1b4Smrgvoid GLAPIENTRY
2907117f1b4Smrg_mesa_ActiveTextureARB(GLenum texture)
2917117f1b4Smrg{
2927117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
2937117f1b4Smrg   const GLuint texUnit = texture - GL_TEXTURE0;
2947117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
2957117f1b4Smrg
2967117f1b4Smrg   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2977117f1b4Smrg      _mesa_debug(ctx, "glActiveTexture %s\n",
2987117f1b4Smrg                  _mesa_lookup_enum_by_nr(texture));
2997117f1b4Smrg
300c1f859d4Smrg   if (texUnit >= ctx->Const.MaxTextureImageUnits) {
3017117f1b4Smrg      _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(texture)");
3027117f1b4Smrg      return;
3037117f1b4Smrg   }
3047117f1b4Smrg
3057117f1b4Smrg   if (ctx->Texture.CurrentUnit == texUnit)
3067117f1b4Smrg      return;
3077117f1b4Smrg
3087117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_TEXTURE);
3097117f1b4Smrg
3107117f1b4Smrg   ctx->Texture.CurrentUnit = texUnit;
3117117f1b4Smrg   if (ctx->Transform.MatrixMode == GL_TEXTURE) {
3127117f1b4Smrg      /* update current stack pointer */
3137117f1b4Smrg      ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
3147117f1b4Smrg   }
3157117f1b4Smrg
3167117f1b4Smrg   if (ctx->Driver.ActiveTexture) {
3177117f1b4Smrg      (*ctx->Driver.ActiveTexture)( ctx, (GLuint) texUnit );
3187117f1b4Smrg   }
3197117f1b4Smrg}
3207117f1b4Smrg
3217117f1b4Smrg
3227117f1b4Smrg/* GL_ARB_multitexture */
3237117f1b4Smrgvoid GLAPIENTRY
3247117f1b4Smrg_mesa_ClientActiveTextureARB(GLenum texture)
3257117f1b4Smrg{
3267117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3277117f1b4Smrg   GLuint texUnit = texture - GL_TEXTURE0;
3287117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
3297117f1b4Smrg
3307117f1b4Smrg   if (texUnit >= ctx->Const.MaxTextureCoordUnits) {
3317117f1b4Smrg      _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(texture)");
3327117f1b4Smrg      return;
3337117f1b4Smrg   }
3347117f1b4Smrg
3357117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_ARRAY);
3367117f1b4Smrg   ctx->Array.ActiveTexture = texUnit;
3377117f1b4Smrg}
3387117f1b4Smrg
3397117f1b4Smrg
3407117f1b4Smrg
3417117f1b4Smrg/**********************************************************************/
3427117f1b4Smrg/*****                    State management                        *****/
3437117f1b4Smrg/**********************************************************************/
3447117f1b4Smrg
3457117f1b4Smrg
3467117f1b4Smrg/**
3477117f1b4Smrg * \note This routine refers to derived texture attribute values to
3487117f1b4Smrg * compute the ENABLE_TEXMAT flags, but is only called on
3497117f1b4Smrg * _NEW_TEXTURE_MATRIX.  On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
3507117f1b4Smrg * flags are updated by _mesa_update_textures(), below.
3517117f1b4Smrg *
3527117f1b4Smrg * \param ctx GL context.
3537117f1b4Smrg */
3547117f1b4Smrgstatic void
3557117f1b4Smrgupdate_texture_matrices( GLcontext *ctx )
3567117f1b4Smrg{
3577117f1b4Smrg   GLuint i;
3587117f1b4Smrg
3597117f1b4Smrg   ctx->Texture._TexMatEnabled = 0;
3607117f1b4Smrg
361c1f859d4Smrg   for (i=0; i < ctx->Const.MaxTextureCoordUnits; i++) {
3627117f1b4Smrg      if (_math_matrix_is_dirty(ctx->TextureMatrixStack[i].Top)) {
3637117f1b4Smrg	 _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
3647117f1b4Smrg
3657117f1b4Smrg	 if (ctx->Texture.Unit[i]._ReallyEnabled &&
3667117f1b4Smrg	     ctx->TextureMatrixStack[i].Top->type != MATRIX_IDENTITY)
3677117f1b4Smrg	    ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(i);
3687117f1b4Smrg
3697117f1b4Smrg	 if (ctx->Driver.TextureMatrix)
3707117f1b4Smrg	    ctx->Driver.TextureMatrix( ctx, i, ctx->TextureMatrixStack[i].Top);
3717117f1b4Smrg      }
3727117f1b4Smrg   }
3737117f1b4Smrg}
3747117f1b4Smrg
3757117f1b4Smrg
376c1f859d4Smrg/**
377c1f859d4Smrg * Update texture object's _Function field.  We need to do this
378c1f859d4Smrg * whenever any of the texture object's shadow-related fields change
379c1f859d4Smrg * or when we start/stop using a fragment program.
380c1f859d4Smrg *
381c1f859d4Smrg * This function could be expanded someday to update additional per-object
382c1f859d4Smrg * fields that depend on assorted state changes.
383c1f859d4Smrg */
384c1f859d4Smrgstatic void
385c1f859d4Smrgupdate_texture_compare_function(GLcontext *ctx,
386c1f859d4Smrg                                struct gl_texture_object *tObj)
387c1f859d4Smrg{
388c1f859d4Smrg   /* XXX temporarily disable this test since it breaks the GLSL
389c1f859d4Smrg    * shadow2D(), etc. functions.
390c1f859d4Smrg    */
391c1f859d4Smrg   if (0 /*ctx->FragmentProgram._Current*/) {
392c1f859d4Smrg      /* Texel/coordinate comparison is ignored for programs.
393c1f859d4Smrg       * See GL_ARB_fragment_program/shader spec for details.
394c1f859d4Smrg       */
395c1f859d4Smrg      tObj->_Function = GL_NONE;
396c1f859d4Smrg   }
397c1f859d4Smrg   else if (tObj->CompareFlag) {
398c1f859d4Smrg      /* GL_SGIX_shadow */
399c1f859d4Smrg      if (tObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
400c1f859d4Smrg         tObj->_Function = GL_LEQUAL;
401c1f859d4Smrg      }
402c1f859d4Smrg      else {
403c1f859d4Smrg         ASSERT(tObj->CompareOperator == GL_TEXTURE_GEQUAL_R_SGIX);
404c1f859d4Smrg         tObj->_Function = GL_GEQUAL;
405c1f859d4Smrg      }
406c1f859d4Smrg   }
407c1f859d4Smrg   else if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
408c1f859d4Smrg      /* GL_ARB_shadow */
409c1f859d4Smrg      tObj->_Function = tObj->CompareFunc;
410c1f859d4Smrg   }
411c1f859d4Smrg   else {
412c1f859d4Smrg      tObj->_Function = GL_NONE;  /* pass depth through as grayscale */
413c1f859d4Smrg   }
414c1f859d4Smrg}
415c1f859d4Smrg
416c1f859d4Smrg
4177117f1b4Smrg/**
4187117f1b4Smrg * Helper function for determining which texture object (1D, 2D, cube, etc)
4197117f1b4Smrg * should actually be used.
4207117f1b4Smrg */
4217117f1b4Smrgstatic void
4227117f1b4Smrgtexture_override(GLcontext *ctx,
4237117f1b4Smrg                 struct gl_texture_unit *texUnit, GLbitfield enableBits,
4247117f1b4Smrg                 struct gl_texture_object *texObj, GLuint textureBit)
4257117f1b4Smrg{
4267117f1b4Smrg   if (!texUnit->_ReallyEnabled && (enableBits & textureBit)) {
427c1f859d4Smrg      if (!texObj->_Complete) {
4287117f1b4Smrg         _mesa_test_texobj_completeness(ctx, texObj);
4297117f1b4Smrg      }
430c1f859d4Smrg      if (texObj->_Complete) {
4317117f1b4Smrg         texUnit->_ReallyEnabled = textureBit;
4327117f1b4Smrg         texUnit->_Current = texObj;
433c1f859d4Smrg         update_texture_compare_function(ctx, texObj);
4347117f1b4Smrg      }
4357117f1b4Smrg   }
4367117f1b4Smrg}
4377117f1b4Smrg
4387117f1b4Smrg
4397117f1b4Smrg/**
4407117f1b4Smrg * \note This routine refers to derived texture matrix values to
4417117f1b4Smrg * compute the ENABLE_TEXMAT flags, but is only called on
4427117f1b4Smrg * _NEW_TEXTURE.  On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
4437117f1b4Smrg * flags are updated by _mesa_update_texture_matrices, above.
4447117f1b4Smrg *
4457117f1b4Smrg * \param ctx GL context.
4467117f1b4Smrg */
4477117f1b4Smrgstatic void
4487117f1b4Smrgupdate_texture_state( GLcontext *ctx )
4497117f1b4Smrg{
4507117f1b4Smrg   GLuint unit;
4517117f1b4Smrg   struct gl_fragment_program *fprog = NULL;
4527117f1b4Smrg   struct gl_vertex_program *vprog = NULL;
4537117f1b4Smrg
4547117f1b4Smrg   if (ctx->Shader.CurrentProgram &&
4557117f1b4Smrg       ctx->Shader.CurrentProgram->LinkStatus) {
4567117f1b4Smrg      fprog = ctx->Shader.CurrentProgram->FragmentProgram;
4577117f1b4Smrg      vprog = ctx->Shader.CurrentProgram->VertexProgram;
4587117f1b4Smrg   }
4597117f1b4Smrg   else {
4607117f1b4Smrg      if (ctx->FragmentProgram._Enabled) {
4617117f1b4Smrg         fprog = ctx->FragmentProgram.Current;
4627117f1b4Smrg      }
4637117f1b4Smrg      if (ctx->VertexProgram._Enabled) {
4647117f1b4Smrg         /* XXX enable this if/when non-shader vertex programs get
4657117f1b4Smrg          * texture fetches:
4667117f1b4Smrg         vprog = ctx->VertexProgram.Current;
4677117f1b4Smrg         */
4687117f1b4Smrg      }
4697117f1b4Smrg   }
4707117f1b4Smrg
4717117f1b4Smrg   ctx->NewState |= _NEW_TEXTURE; /* TODO: only set this if there are
4727117f1b4Smrg				   * actual changes.
4737117f1b4Smrg				   */
4747117f1b4Smrg
4757117f1b4Smrg   ctx->Texture._EnabledUnits = 0;
4767117f1b4Smrg   ctx->Texture._GenFlags = 0;
4777117f1b4Smrg   ctx->Texture._TexMatEnabled = 0;
4787117f1b4Smrg   ctx->Texture._TexGenEnabled = 0;
4797117f1b4Smrg
4807117f1b4Smrg   /*
4817117f1b4Smrg    * Update texture unit state.
4827117f1b4Smrg    */
483c1f859d4Smrg   for (unit = 0; unit < ctx->Const.MaxTextureImageUnits; unit++) {
4847117f1b4Smrg      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
4857117f1b4Smrg      GLbitfield enableBits;
486c1f859d4Smrg      GLuint tex;
4877117f1b4Smrg
4887117f1b4Smrg      texUnit->_Current = NULL;
4897117f1b4Smrg      texUnit->_ReallyEnabled = 0;
4907117f1b4Smrg      texUnit->_GenFlags = 0;
4917117f1b4Smrg
492c1f859d4Smrg      /* Get the bitmask of texture target enables.
4937117f1b4Smrg       * enableBits will be a mask of the TEXTURE_*_BIT flags indicating
4947117f1b4Smrg       * which texture targets are enabled (fixed function) or referenced
4957117f1b4Smrg       * by a fragment shader/program.  When multiple flags are set, we'll
4967117f1b4Smrg       * settle on the one with highest priority (see texture_override below).
4977117f1b4Smrg       */
498c1f859d4Smrg      enableBits = 0x0;
499c1f859d4Smrg      if (vprog) {
500c1f859d4Smrg         enableBits |= vprog->Base.TexturesUsed[unit];
501c1f859d4Smrg      }
502c1f859d4Smrg      if (fprog) {
503c1f859d4Smrg         enableBits |= fprog->Base.TexturesUsed[unit];
5047117f1b4Smrg      }
5057117f1b4Smrg      else {
506c1f859d4Smrg         /* fixed-function fragment program */
507c1f859d4Smrg         enableBits |= texUnit->Enabled;
508c1f859d4Smrg      }
509c1f859d4Smrg
510c1f859d4Smrg      if (enableBits == 0x0)
511c1f859d4Smrg         continue;
512c1f859d4Smrg
513c1f859d4Smrg      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
514c1f859d4Smrg         ASSERT(texUnit->CurrentTex[tex]);
5157117f1b4Smrg      }
5167117f1b4Smrg
5177117f1b4Smrg      /* Look for the highest-priority texture target that's enabled and
5187117f1b4Smrg       * complete.  That's the one we'll use for texturing.  If we're using
5197117f1b4Smrg       * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
5207117f1b4Smrg       */
521c1f859d4Smrg      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
522c1f859d4Smrg         /* texture indexes from highest to lowest priority */
523c1f859d4Smrg         static const GLuint targets[NUM_TEXTURE_TARGETS] = {
524c1f859d4Smrg            TEXTURE_2D_ARRAY_INDEX,
525c1f859d4Smrg            TEXTURE_1D_ARRAY_INDEX,
526c1f859d4Smrg            TEXTURE_CUBE_INDEX,
527c1f859d4Smrg            TEXTURE_3D_INDEX,
528c1f859d4Smrg            TEXTURE_RECT_INDEX,
529c1f859d4Smrg            TEXTURE_2D_INDEX,
530c1f859d4Smrg            TEXTURE_1D_INDEX
531c1f859d4Smrg         };
532c1f859d4Smrg         GLuint texIndex = targets[tex];
533c1f859d4Smrg         texture_override(ctx, texUnit, enableBits,
534c1f859d4Smrg                          texUnit->CurrentTex[texIndex], 1 << texIndex);
535c1f859d4Smrg      }
5367117f1b4Smrg
5377117f1b4Smrg      if (!texUnit->_ReallyEnabled) {
5387117f1b4Smrg         continue;
5397117f1b4Smrg      }
5407117f1b4Smrg
5417117f1b4Smrg      if (texUnit->_ReallyEnabled)
5427117f1b4Smrg         ctx->Texture._EnabledUnits |= (1 << unit);
5437117f1b4Smrg
5447117f1b4Smrg      if (texUnit->EnvMode == GL_COMBINE) {
5457117f1b4Smrg	 texUnit->_CurrentCombine = & texUnit->Combine;
5467117f1b4Smrg      }
5477117f1b4Smrg      else {
5487117f1b4Smrg         const struct gl_texture_object *texObj = texUnit->_Current;
5497117f1b4Smrg         GLenum format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
5507117f1b4Smrg         if (format == GL_COLOR_INDEX) {
5517117f1b4Smrg            format = GL_RGBA;  /* a bit of a hack */
5527117f1b4Smrg         }
5537117f1b4Smrg         else if (format == GL_DEPTH_COMPONENT
5547117f1b4Smrg                  || format == GL_DEPTH_STENCIL_EXT) {
5557117f1b4Smrg            format = texObj->DepthMode;
5567117f1b4Smrg         }
5577117f1b4Smrg	 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
5587117f1b4Smrg	 texUnit->_CurrentCombine = & texUnit->_EnvMode;
5597117f1b4Smrg      }
5607117f1b4Smrg
5617117f1b4Smrg      switch (texUnit->_CurrentCombine->ModeRGB) {
5627117f1b4Smrg      case GL_REPLACE:
5637117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsRGB = 1;
5647117f1b4Smrg	 break;
5657117f1b4Smrg      case GL_MODULATE:
5667117f1b4Smrg      case GL_ADD:
5677117f1b4Smrg      case GL_ADD_SIGNED:
5687117f1b4Smrg      case GL_SUBTRACT:
5697117f1b4Smrg      case GL_DOT3_RGB:
5707117f1b4Smrg      case GL_DOT3_RGBA:
5717117f1b4Smrg      case GL_DOT3_RGB_EXT:
5727117f1b4Smrg      case GL_DOT3_RGBA_EXT:
5737117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsRGB = 2;
5747117f1b4Smrg	 break;
5757117f1b4Smrg      case GL_INTERPOLATE:
5767117f1b4Smrg      case GL_MODULATE_ADD_ATI:
5777117f1b4Smrg      case GL_MODULATE_SIGNED_ADD_ATI:
5787117f1b4Smrg      case GL_MODULATE_SUBTRACT_ATI:
5797117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsRGB = 3;
5807117f1b4Smrg	 break;
5817117f1b4Smrg      default:
5827117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsRGB = 0;
5837117f1b4Smrg         _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
5847117f1b4Smrg         return;
5857117f1b4Smrg      }
5867117f1b4Smrg
5877117f1b4Smrg      switch (texUnit->_CurrentCombine->ModeA) {
5887117f1b4Smrg      case GL_REPLACE:
5897117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsA = 1;
5907117f1b4Smrg	 break;
5917117f1b4Smrg      case GL_MODULATE:
5927117f1b4Smrg      case GL_ADD:
5937117f1b4Smrg      case GL_ADD_SIGNED:
5947117f1b4Smrg      case GL_SUBTRACT:
5957117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsA = 2;
5967117f1b4Smrg	 break;
5977117f1b4Smrg      case GL_INTERPOLATE:
5987117f1b4Smrg      case GL_MODULATE_ADD_ATI:
5997117f1b4Smrg      case GL_MODULATE_SIGNED_ADD_ATI:
6007117f1b4Smrg      case GL_MODULATE_SUBTRACT_ATI:
6017117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsA = 3;
6027117f1b4Smrg	 break;
6037117f1b4Smrg      default:
6047117f1b4Smrg	 texUnit->_CurrentCombine->_NumArgsA = 0;
6057117f1b4Smrg         _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
6067117f1b4Smrg	 break;
6077117f1b4Smrg      }
608c1f859d4Smrg   }
609c1f859d4Smrg
610c1f859d4Smrg   /* Determine which texture coordinate sets are actually needed */
611c1f859d4Smrg   if (fprog) {
612c1f859d4Smrg      const GLuint coordMask = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
613c1f859d4Smrg      ctx->Texture._EnabledCoordUnits
614c1f859d4Smrg         = (fprog->Base.InputsRead >> FRAG_ATTRIB_TEX0) & coordMask;
615c1f859d4Smrg   }
616c1f859d4Smrg   else {
617c1f859d4Smrg      ctx->Texture._EnabledCoordUnits = ctx->Texture._EnabledUnits;
618c1f859d4Smrg   }
619c1f859d4Smrg
620c1f859d4Smrg   /* Setup texgen for those texture coordinate sets that are in use */
621c1f859d4Smrg   for (unit = 0; unit < ctx->Const.MaxTextureCoordUnits; unit++) {
622c1f859d4Smrg      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
623c1f859d4Smrg
624c1f859d4Smrg      if (!(ctx->Texture._EnabledCoordUnits & (1 << unit)))
625c1f859d4Smrg	 continue;
6267117f1b4Smrg
6277117f1b4Smrg      if (texUnit->TexGenEnabled) {
6287117f1b4Smrg	 if (texUnit->TexGenEnabled & S_BIT) {
6297117f1b4Smrg	    texUnit->_GenFlags |= texUnit->_GenBitS;
6307117f1b4Smrg	 }
6317117f1b4Smrg	 if (texUnit->TexGenEnabled & T_BIT) {
6327117f1b4Smrg	    texUnit->_GenFlags |= texUnit->_GenBitT;
6337117f1b4Smrg	 }
6347117f1b4Smrg	 if (texUnit->TexGenEnabled & Q_BIT) {
6357117f1b4Smrg	    texUnit->_GenFlags |= texUnit->_GenBitQ;
6367117f1b4Smrg	 }
6377117f1b4Smrg	 if (texUnit->TexGenEnabled & R_BIT) {
6387117f1b4Smrg	    texUnit->_GenFlags |= texUnit->_GenBitR;
6397117f1b4Smrg	 }
6407117f1b4Smrg
6417117f1b4Smrg	 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
6427117f1b4Smrg	 ctx->Texture._GenFlags |= texUnit->_GenFlags;
6437117f1b4Smrg      }
6447117f1b4Smrg
6457117f1b4Smrg      if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
6467117f1b4Smrg	 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
6477117f1b4Smrg   }
6487117f1b4Smrg}
6497117f1b4Smrg
6507117f1b4Smrg
6517117f1b4Smrg/**
6527117f1b4Smrg * Update texture-related derived state.
6537117f1b4Smrg */
6547117f1b4Smrgvoid
6557117f1b4Smrg_mesa_update_texture( GLcontext *ctx, GLuint new_state )
6567117f1b4Smrg{
6577117f1b4Smrg   if (new_state & _NEW_TEXTURE_MATRIX)
6587117f1b4Smrg      update_texture_matrices( ctx );
6597117f1b4Smrg
6607117f1b4Smrg   if (new_state & (_NEW_TEXTURE | _NEW_PROGRAM))
6617117f1b4Smrg      update_texture_state( ctx );
6627117f1b4Smrg}
6637117f1b4Smrg
6647117f1b4Smrg
6657117f1b4Smrg/**********************************************************************/
6667117f1b4Smrg/*****                      Initialization                        *****/
6677117f1b4Smrg/**********************************************************************/
6687117f1b4Smrg
6697117f1b4Smrg/**
6707117f1b4Smrg * Allocate the proxy textures for the given context.
6717117f1b4Smrg *
6727117f1b4Smrg * \param ctx the context to allocate proxies for.
6737117f1b4Smrg *
6747117f1b4Smrg * \return GL_TRUE on success, or GL_FALSE on failure
6757117f1b4Smrg *
6767117f1b4Smrg * If run out of memory part way through the allocations, clean up and return
6777117f1b4Smrg * GL_FALSE.
6787117f1b4Smrg */
6797117f1b4Smrgstatic GLboolean
6807117f1b4Smrgalloc_proxy_textures( GLcontext *ctx )
6817117f1b4Smrg{
682c1f859d4Smrg   static const GLenum targets[] = {
683c1f859d4Smrg      GL_TEXTURE_1D,
684c1f859d4Smrg      GL_TEXTURE_2D,
685c1f859d4Smrg      GL_TEXTURE_3D,
686c1f859d4Smrg      GL_TEXTURE_CUBE_MAP_ARB,
687c1f859d4Smrg      GL_TEXTURE_RECTANGLE_NV,
688c1f859d4Smrg      GL_TEXTURE_1D_ARRAY_EXT,
689c1f859d4Smrg      GL_TEXTURE_2D_ARRAY_EXT
690c1f859d4Smrg   };
691c1f859d4Smrg   GLint tgt;
6927117f1b4Smrg
693c1f859d4Smrg   ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
6947117f1b4Smrg
695c1f859d4Smrg   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
696c1f859d4Smrg      if (!(ctx->Texture.ProxyTex[tgt]
697c1f859d4Smrg            = ctx->Driver.NewTextureObject(ctx, 0, targets[tgt]))) {
698c1f859d4Smrg         /* out of memory, free what we did allocate */
699c1f859d4Smrg         while (--tgt >= 0) {
700c1f859d4Smrg            ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
701c1f859d4Smrg         }
702c1f859d4Smrg         return GL_FALSE;
703c1f859d4Smrg      }
704c1f859d4Smrg   }
7057117f1b4Smrg
706c1f859d4Smrg   assert(ctx->Texture.ProxyTex[0]->RefCount == 1); /* sanity check */
7077117f1b4Smrg   return GL_TRUE;
7087117f1b4Smrg}
7097117f1b4Smrg
7107117f1b4Smrg
7117117f1b4Smrg/**
7127117f1b4Smrg * Initialize a texture unit.
7137117f1b4Smrg *
7147117f1b4Smrg * \param ctx GL context.
7157117f1b4Smrg * \param unit texture unit number to be initialized.
7167117f1b4Smrg */
7177117f1b4Smrgstatic void
7187117f1b4Smrginit_texture_unit( GLcontext *ctx, GLuint unit )
7197117f1b4Smrg{
7207117f1b4Smrg   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
721c1f859d4Smrg   GLuint tex;
7227117f1b4Smrg
7237117f1b4Smrg   texUnit->EnvMode = GL_MODULATE;
7247117f1b4Smrg   ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
7257117f1b4Smrg
7267117f1b4Smrg   texUnit->Combine = default_combine_state;
7277117f1b4Smrg   texUnit->_EnvMode = default_combine_state;
7287117f1b4Smrg   texUnit->_CurrentCombine = & texUnit->_EnvMode;
7297117f1b4Smrg
7307117f1b4Smrg   texUnit->TexGenEnabled = 0;
7317117f1b4Smrg   texUnit->GenModeS = GL_EYE_LINEAR;
7327117f1b4Smrg   texUnit->GenModeT = GL_EYE_LINEAR;
7337117f1b4Smrg   texUnit->GenModeR = GL_EYE_LINEAR;
7347117f1b4Smrg   texUnit->GenModeQ = GL_EYE_LINEAR;
7357117f1b4Smrg   texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
7367117f1b4Smrg   texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
7377117f1b4Smrg   texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
7387117f1b4Smrg   texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
7397117f1b4Smrg
7407117f1b4Smrg   /* Yes, these plane coefficients are correct! */
7417117f1b4Smrg   ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
7427117f1b4Smrg   ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
7437117f1b4Smrg   ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
7447117f1b4Smrg   ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
7457117f1b4Smrg   ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
7467117f1b4Smrg   ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
7477117f1b4Smrg   ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
7487117f1b4Smrg   ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
7497117f1b4Smrg
7507117f1b4Smrg   /* initialize current texture object ptrs to the shared default objects */
751c1f859d4Smrg   for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
752c1f859d4Smrg      _mesa_reference_texobj(&texUnit->CurrentTex[tex],
753c1f859d4Smrg                             ctx->Shared->DefaultTex[tex]);
754c1f859d4Smrg   }
7557117f1b4Smrg}
7567117f1b4Smrg
7577117f1b4Smrg
7587117f1b4Smrg/**
7597117f1b4Smrg * Initialize texture state for the given context.
7607117f1b4Smrg */
7617117f1b4SmrgGLboolean
7627117f1b4Smrg_mesa_init_texture(GLcontext *ctx)
7637117f1b4Smrg{
7647117f1b4Smrg   GLuint i;
7657117f1b4Smrg
7667117f1b4Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
7677117f1b4Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
7687117f1b4Smrg
7697117f1b4Smrg   /* Texture group */
7707117f1b4Smrg   ctx->Texture.CurrentUnit = 0;      /* multitexture */
7717117f1b4Smrg   ctx->Texture._EnabledUnits = 0;
7727117f1b4Smrg   ctx->Texture.SharedPalette = GL_FALSE;
773c1f859d4Smrg#if FEATURE_colortable
7747117f1b4Smrg   _mesa_init_colortable(&ctx->Texture.Palette);
775c1f859d4Smrg#endif
7767117f1b4Smrg
7777117f1b4Smrg   for (i = 0; i < MAX_TEXTURE_UNITS; i++)
7787117f1b4Smrg      init_texture_unit( ctx, i );
7797117f1b4Smrg
7807117f1b4Smrg   /* After we're done initializing the context's texture state the default
7817117f1b4Smrg    * texture objects' refcounts should be at least MAX_TEXTURE_UNITS + 1.
7827117f1b4Smrg    */
783c1f859d4Smrg   assert(ctx->Shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount
784c1f859d4Smrg          >= MAX_TEXTURE_UNITS + 1);
7857117f1b4Smrg
7867117f1b4Smrg   /* Allocate proxy textures */
7877117f1b4Smrg   if (!alloc_proxy_textures( ctx ))
7887117f1b4Smrg      return GL_FALSE;
7897117f1b4Smrg
7907117f1b4Smrg   return GL_TRUE;
7917117f1b4Smrg}
7927117f1b4Smrg
7937117f1b4Smrg
7947117f1b4Smrg/**
795c1f859d4Smrg * Free dynamically-allocted texture data attached to the given context.
7967117f1b4Smrg */
7977117f1b4Smrgvoid
7987117f1b4Smrg_mesa_free_texture_data(GLcontext *ctx)
7997117f1b4Smrg{
800c1f859d4Smrg   GLuint u, tgt;
8017117f1b4Smrg
8027117f1b4Smrg   /* unreference current textures */
8037117f1b4Smrg   for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
8047117f1b4Smrg      struct gl_texture_unit *unit = ctx->Texture.Unit + u;
805c1f859d4Smrg      /* The _Current texture could account for another reference */
806c1f859d4Smrg      _mesa_reference_texobj(&ctx->Texture.Unit[u]._Current, NULL);
807c1f859d4Smrg
808c1f859d4Smrg      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
809c1f859d4Smrg         _mesa_reference_texobj(&unit->CurrentTex[tgt], NULL);
810c1f859d4Smrg      }
8117117f1b4Smrg   }
8127117f1b4Smrg
8137117f1b4Smrg   /* Free proxy texture objects */
814c1f859d4Smrg   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++)
815c1f859d4Smrg      ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
816c1f859d4Smrg
817c1f859d4Smrg#if FEATURE_colortable
818c1f859d4Smrg   {
819c1f859d4Smrg      GLuint i;
820c1f859d4Smrg      for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
821c1f859d4Smrg         _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
822c1f859d4Smrg   }
823c1f859d4Smrg#endif
824c1f859d4Smrg}
825c1f859d4Smrg
8267117f1b4Smrg
827c1f859d4Smrg/**
828c1f859d4Smrg * Update the default texture objects in the given context to reference those
829c1f859d4Smrg * specified in the shared state and release those referencing the old
830c1f859d4Smrg * shared state.
831c1f859d4Smrg */
832c1f859d4Smrgvoid
833c1f859d4Smrg_mesa_update_default_objects_texture(GLcontext *ctx)
834c1f859d4Smrg{
835c1f859d4Smrg   GLuint i, tex;
8367117f1b4Smrg
837c1f859d4Smrg   for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
838c1f859d4Smrg      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
839c1f859d4Smrg      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
840c1f859d4Smrg         _mesa_reference_texobj(&texUnit->CurrentTex[tex],
841c1f859d4Smrg                                ctx->Shared->DefaultTex[tex]);
842c1f859d4Smrg      }
843c1f859d4Smrg   }
8447117f1b4Smrg}
845