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