1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci/**
26bf215546Sopenharmony_ci * \file texstate.c
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci * Texture state handling.
29bf215546Sopenharmony_ci */
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <stdio.h>
32bf215546Sopenharmony_ci#include "glheader.h"
33bf215546Sopenharmony_ci#include "bufferobj.h"
34bf215546Sopenharmony_ci#include "context.h"
35bf215546Sopenharmony_ci#include "enums.h"
36bf215546Sopenharmony_ci#include "macros.h"
37bf215546Sopenharmony_ci#include "texobj.h"
38bf215546Sopenharmony_ci#include "teximage.h"
39bf215546Sopenharmony_ci#include "texstate.h"
40bf215546Sopenharmony_ci#include "mtypes.h"
41bf215546Sopenharmony_ci#include "state.h"
42bf215546Sopenharmony_ci#include "util/bitscan.h"
43bf215546Sopenharmony_ci#include "util/bitset.h"
44bf215546Sopenharmony_ci#include "api_exec_decl.h"
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci#include "state_tracker/st_cb_texture.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci/**
49bf215546Sopenharmony_ci * Default texture combine environment state.  This is used to initialize
50bf215546Sopenharmony_ci * a context's texture units and as the basis for converting "classic"
51bf215546Sopenharmony_ci * texture environmnets to ARB_texture_env_combine style values.
52bf215546Sopenharmony_ci */
53bf215546Sopenharmony_cistatic const struct gl_tex_env_combine_state default_combine_state = {
54bf215546Sopenharmony_ci   GL_MODULATE, GL_MODULATE,
55bf215546Sopenharmony_ci   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
56bf215546Sopenharmony_ci   { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
57bf215546Sopenharmony_ci   { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA, GL_SRC_ALPHA },
58bf215546Sopenharmony_ci   { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
59bf215546Sopenharmony_ci   0, 0,
60bf215546Sopenharmony_ci   2, 2
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci/**
66bf215546Sopenharmony_ci * Used by glXCopyContext to copy texture state from one context to another.
67bf215546Sopenharmony_ci */
68bf215546Sopenharmony_civoid
69bf215546Sopenharmony_ci_mesa_copy_texture_state( const struct gl_context *src, struct gl_context *dst )
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   GLuint u, tex;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   assert(src);
74bf215546Sopenharmony_ci   assert(dst);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   dst->Texture.CurrentUnit = src->Texture.CurrentUnit;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   /* per-unit state */
79bf215546Sopenharmony_ci   for (u = 0; u < src->Const.MaxCombinedTextureImageUnits; u++) {
80bf215546Sopenharmony_ci      dst->Texture.Unit[u].LodBias = src->Texture.Unit[u].LodBias;
81bf215546Sopenharmony_ci      dst->Texture.Unit[u].LodBiasQuantized = src->Texture.Unit[u].LodBiasQuantized;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      /*
84bf215546Sopenharmony_ci       * XXX strictly speaking, we should compare texture names/ids and
85bf215546Sopenharmony_ci       * bind textures in the dest context according to id.  For now, only
86bf215546Sopenharmony_ci       * copy bindings if the contexts share the same pool of textures to
87bf215546Sopenharmony_ci       * avoid refcounting bugs.
88bf215546Sopenharmony_ci       */
89bf215546Sopenharmony_ci      if (dst->Shared == src->Shared) {
90bf215546Sopenharmony_ci         /* copy texture object bindings, not contents of texture objects */
91bf215546Sopenharmony_ci         _mesa_lock_context_textures(dst);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci         for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
94bf215546Sopenharmony_ci            _mesa_reference_texobj(&dst->Texture.Unit[u].CurrentTex[tex],
95bf215546Sopenharmony_ci                                   src->Texture.Unit[u].CurrentTex[tex]);
96bf215546Sopenharmony_ci            if (src->Texture.Unit[u].CurrentTex[tex]) {
97bf215546Sopenharmony_ci               dst->Texture.NumCurrentTexUsed =
98bf215546Sopenharmony_ci                  MAX2(dst->Texture.NumCurrentTexUsed, u + 1);
99bf215546Sopenharmony_ci            }
100bf215546Sopenharmony_ci         }
101bf215546Sopenharmony_ci         dst->Texture.Unit[u]._BoundTextures = src->Texture.Unit[u]._BoundTextures;
102bf215546Sopenharmony_ci         _mesa_unlock_context_textures(dst);
103bf215546Sopenharmony_ci      }
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   for (u = 0; u < src->Const.MaxTextureCoordUnits; u++) {
107bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].Enabled = src->Texture.FixedFuncUnit[u].Enabled;
108bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].EnvMode = src->Texture.FixedFuncUnit[u].EnvMode;
109bf215546Sopenharmony_ci      COPY_4V(dst->Texture.FixedFuncUnit[u].EnvColor, src->Texture.FixedFuncUnit[u].EnvColor);
110bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].TexGenEnabled = src->Texture.FixedFuncUnit[u].TexGenEnabled;
111bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].GenS = src->Texture.FixedFuncUnit[u].GenS;
112bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].GenT = src->Texture.FixedFuncUnit[u].GenT;
113bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].GenR = src->Texture.FixedFuncUnit[u].GenR;
114bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].GenQ = src->Texture.FixedFuncUnit[u].GenQ;
115bf215546Sopenharmony_ci      memcpy(dst->Texture.FixedFuncUnit[u].ObjectPlane,
116bf215546Sopenharmony_ci             src->Texture.FixedFuncUnit[u].ObjectPlane,
117bf215546Sopenharmony_ci             sizeof(src->Texture.FixedFuncUnit[u].ObjectPlane));
118bf215546Sopenharmony_ci      memcpy(dst->Texture.FixedFuncUnit[u].EyePlane,
119bf215546Sopenharmony_ci             src->Texture.FixedFuncUnit[u].EyePlane,
120bf215546Sopenharmony_ci             sizeof(src->Texture.FixedFuncUnit[u].EyePlane));
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      /* GL_EXT_texture_env_combine */
123bf215546Sopenharmony_ci      dst->Texture.FixedFuncUnit[u].Combine = src->Texture.FixedFuncUnit[u].Combine;
124bf215546Sopenharmony_ci   }
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci/*
129bf215546Sopenharmony_ci * For debugging
130bf215546Sopenharmony_ci */
131bf215546Sopenharmony_civoid
132bf215546Sopenharmony_ci_mesa_print_texunit_state( struct gl_context *ctx, GLuint unit )
133bf215546Sopenharmony_ci{
134bf215546Sopenharmony_ci   const struct gl_fixedfunc_texture_unit *texUnit = ctx->Texture.FixedFuncUnit + unit;
135bf215546Sopenharmony_ci   printf("Texture Unit %d\n", unit);
136bf215546Sopenharmony_ci   printf("  GL_TEXTURE_ENV_MODE = %s\n", _mesa_enum_to_string(texUnit->EnvMode));
137bf215546Sopenharmony_ci   printf("  GL_COMBINE_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.ModeRGB));
138bf215546Sopenharmony_ci   printf("  GL_COMBINE_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.ModeA));
139bf215546Sopenharmony_ci   printf("  GL_SOURCE0_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceRGB[0]));
140bf215546Sopenharmony_ci   printf("  GL_SOURCE1_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceRGB[1]));
141bf215546Sopenharmony_ci   printf("  GL_SOURCE2_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceRGB[2]));
142bf215546Sopenharmony_ci   printf("  GL_SOURCE0_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceA[0]));
143bf215546Sopenharmony_ci   printf("  GL_SOURCE1_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceA[1]));
144bf215546Sopenharmony_ci   printf("  GL_SOURCE2_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.SourceA[2]));
145bf215546Sopenharmony_ci   printf("  GL_OPERAND0_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandRGB[0]));
146bf215546Sopenharmony_ci   printf("  GL_OPERAND1_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandRGB[1]));
147bf215546Sopenharmony_ci   printf("  GL_OPERAND2_RGB = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandRGB[2]));
148bf215546Sopenharmony_ci   printf("  GL_OPERAND0_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandA[0]));
149bf215546Sopenharmony_ci   printf("  GL_OPERAND1_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandA[1]));
150bf215546Sopenharmony_ci   printf("  GL_OPERAND2_ALPHA = %s\n", _mesa_enum_to_string(texUnit->Combine.OperandA[2]));
151bf215546Sopenharmony_ci   printf("  GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
152bf215546Sopenharmony_ci   printf("  GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
153bf215546Sopenharmony_ci   printf("  GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci/**********************************************************************/
159bf215546Sopenharmony_ci/*                       Texture Environment                          */
160bf215546Sopenharmony_ci/**********************************************************************/
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci/**
163bf215546Sopenharmony_ci * Convert "classic" texture environment to ARB_texture_env_combine style
164bf215546Sopenharmony_ci * environments.
165bf215546Sopenharmony_ci *
166bf215546Sopenharmony_ci * \param state  texture_env_combine state vector to be filled-in.
167bf215546Sopenharmony_ci * \param mode   Classic texture environment mode (i.e., \c GL_REPLACE,
168bf215546Sopenharmony_ci *               \c GL_BLEND, \c GL_DECAL, etc.).
169bf215546Sopenharmony_ci * \param texBaseFormat  Base format of the texture associated with the
170bf215546Sopenharmony_ci *               texture unit.
171bf215546Sopenharmony_ci */
172bf215546Sopenharmony_cistatic void
173bf215546Sopenharmony_cicalculate_derived_texenv( struct gl_tex_env_combine_state *state,
174bf215546Sopenharmony_ci			  GLenum mode, GLenum texBaseFormat )
175bf215546Sopenharmony_ci{
176bf215546Sopenharmony_ci   GLenum mode_rgb;
177bf215546Sopenharmony_ci   GLenum mode_a;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   *state = default_combine_state;
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   switch (texBaseFormat) {
182bf215546Sopenharmony_ci   case GL_ALPHA:
183bf215546Sopenharmony_ci      state->SourceRGB[0] = GL_PREVIOUS;
184bf215546Sopenharmony_ci      break;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   case GL_LUMINANCE_ALPHA:
187bf215546Sopenharmony_ci   case GL_INTENSITY:
188bf215546Sopenharmony_ci   case GL_RGBA:
189bf215546Sopenharmony_ci      break;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   case GL_LUMINANCE:
192bf215546Sopenharmony_ci   case GL_RED:
193bf215546Sopenharmony_ci   case GL_RG:
194bf215546Sopenharmony_ci   case GL_RGB:
195bf215546Sopenharmony_ci   case GL_YCBCR_MESA:
196bf215546Sopenharmony_ci      state->SourceA[0] = GL_PREVIOUS;
197bf215546Sopenharmony_ci      break;
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   default:
200bf215546Sopenharmony_ci      _mesa_problem(NULL,
201bf215546Sopenharmony_ci                    "Invalid texBaseFormat 0x%x in calculate_derived_texenv",
202bf215546Sopenharmony_ci                    texBaseFormat);
203bf215546Sopenharmony_ci      return;
204bf215546Sopenharmony_ci   }
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   if (mode == GL_REPLACE_EXT)
207bf215546Sopenharmony_ci      mode = GL_REPLACE;
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   switch (mode) {
210bf215546Sopenharmony_ci   case GL_REPLACE:
211bf215546Sopenharmony_ci   case GL_MODULATE:
212bf215546Sopenharmony_ci      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
213bf215546Sopenharmony_ci      mode_a   = mode;
214bf215546Sopenharmony_ci      break;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   case GL_DECAL:
217bf215546Sopenharmony_ci      mode_rgb = GL_INTERPOLATE;
218bf215546Sopenharmony_ci      mode_a   = GL_REPLACE;
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci      state->SourceA[0] = GL_PREVIOUS;
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci      /* Having alpha / luminance / intensity textures replace using the
223bf215546Sopenharmony_ci       * incoming fragment color matches the definition in NV_texture_shader.
224bf215546Sopenharmony_ci       * The 1.5 spec simply marks these as "undefined".
225bf215546Sopenharmony_ci       */
226bf215546Sopenharmony_ci      switch (texBaseFormat) {
227bf215546Sopenharmony_ci      case GL_ALPHA:
228bf215546Sopenharmony_ci      case GL_LUMINANCE:
229bf215546Sopenharmony_ci      case GL_LUMINANCE_ALPHA:
230bf215546Sopenharmony_ci      case GL_INTENSITY:
231bf215546Sopenharmony_ci	 state->SourceRGB[0] = GL_PREVIOUS;
232bf215546Sopenharmony_ci	 break;
233bf215546Sopenharmony_ci      case GL_RED:
234bf215546Sopenharmony_ci      case GL_RG:
235bf215546Sopenharmony_ci      case GL_RGB:
236bf215546Sopenharmony_ci      case GL_YCBCR_MESA:
237bf215546Sopenharmony_ci	 mode_rgb = GL_REPLACE;
238bf215546Sopenharmony_ci	 break;
239bf215546Sopenharmony_ci      case GL_RGBA:
240bf215546Sopenharmony_ci	 state->SourceRGB[2] = GL_TEXTURE;
241bf215546Sopenharmony_ci	 break;
242bf215546Sopenharmony_ci      }
243bf215546Sopenharmony_ci      break;
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci   case GL_BLEND:
246bf215546Sopenharmony_ci      mode_rgb = GL_INTERPOLATE;
247bf215546Sopenharmony_ci      mode_a   = GL_MODULATE;
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci      switch (texBaseFormat) {
250bf215546Sopenharmony_ci      case GL_ALPHA:
251bf215546Sopenharmony_ci	 mode_rgb = GL_REPLACE;
252bf215546Sopenharmony_ci	 break;
253bf215546Sopenharmony_ci      case GL_INTENSITY:
254bf215546Sopenharmony_ci	 mode_a = GL_INTERPOLATE;
255bf215546Sopenharmony_ci	 state->SourceA[0] = GL_CONSTANT;
256bf215546Sopenharmony_ci	 state->OperandA[2] = GL_SRC_ALPHA;
257bf215546Sopenharmony_ci	 FALLTHROUGH;
258bf215546Sopenharmony_ci      case GL_LUMINANCE:
259bf215546Sopenharmony_ci      case GL_RED:
260bf215546Sopenharmony_ci      case GL_RG:
261bf215546Sopenharmony_ci      case GL_RGB:
262bf215546Sopenharmony_ci      case GL_LUMINANCE_ALPHA:
263bf215546Sopenharmony_ci      case GL_RGBA:
264bf215546Sopenharmony_ci      case GL_YCBCR_MESA:
265bf215546Sopenharmony_ci	 state->SourceRGB[2] = GL_TEXTURE;
266bf215546Sopenharmony_ci	 state->SourceA[2]   = GL_TEXTURE;
267bf215546Sopenharmony_ci	 state->SourceRGB[0] = GL_CONSTANT;
268bf215546Sopenharmony_ci	 state->OperandRGB[2] = GL_SRC_COLOR;
269bf215546Sopenharmony_ci	 break;
270bf215546Sopenharmony_ci      }
271bf215546Sopenharmony_ci      break;
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci   case GL_ADD:
274bf215546Sopenharmony_ci      mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
275bf215546Sopenharmony_ci      mode_a   = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
276bf215546Sopenharmony_ci      break;
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   default:
279bf215546Sopenharmony_ci      _mesa_problem(NULL,
280bf215546Sopenharmony_ci                    "Invalid texture env mode 0x%x in calculate_derived_texenv",
281bf215546Sopenharmony_ci                    mode);
282bf215546Sopenharmony_ci      return;
283bf215546Sopenharmony_ci   }
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
286bf215546Sopenharmony_ci       ? mode_rgb : GL_REPLACE;
287bf215546Sopenharmony_ci   state->ModeA   = (state->SourceA[0]   != GL_PREVIOUS)
288bf215546Sopenharmony_ci       ? mode_a   : GL_REPLACE;
289bf215546Sopenharmony_ci}
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci/* GL_ARB_multitexture */
293bf215546Sopenharmony_cistatic ALWAYS_INLINE void
294bf215546Sopenharmony_ciactive_texture(GLenum texture, bool no_error)
295bf215546Sopenharmony_ci{
296bf215546Sopenharmony_ci   const GLuint texUnit = texture - GL_TEXTURE0;
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
301bf215546Sopenharmony_ci      _mesa_debug(ctx, "glActiveTexture %s\n",
302bf215546Sopenharmony_ci                  _mesa_enum_to_string(texture));
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   if (ctx->Texture.CurrentUnit == texUnit)
305bf215546Sopenharmony_ci      return;
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   if (!no_error) {
308bf215546Sopenharmony_ci      GLuint k = _mesa_max_tex_unit(ctx);
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci      assert(k <= ARRAY_SIZE(ctx->Texture.Unit));
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci      if (texUnit >= k) {
313bf215546Sopenharmony_ci         _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(texture=%s)",
314bf215546Sopenharmony_ci                     _mesa_enum_to_string(texture));
315bf215546Sopenharmony_ci         return;
316bf215546Sopenharmony_ci      }
317bf215546Sopenharmony_ci   }
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   /* The below flush call seems useless because
321bf215546Sopenharmony_ci    * gl_context::Texture::CurrentUnit is not used by
322bf215546Sopenharmony_ci    * _mesa_update_texture_state() and friends.
323bf215546Sopenharmony_ci    *
324bf215546Sopenharmony_ci    * However removing the flush
325bf215546Sopenharmony_ci    * introduced some blinking textures in UT2004. More investigation is
326bf215546Sopenharmony_ci    * needed to find the root cause.
327bf215546Sopenharmony_ci    *
328bf215546Sopenharmony_ci    * https://bugs.freedesktop.org/show_bug.cgi?id=105436
329bf215546Sopenharmony_ci    */
330bf215546Sopenharmony_ci   FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   ctx->Texture.CurrentUnit = texUnit;
333bf215546Sopenharmony_ci   if (ctx->Transform.MatrixMode == GL_TEXTURE) {
334bf215546Sopenharmony_ci      /* update current stack pointer */
335bf215546Sopenharmony_ci      ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
336bf215546Sopenharmony_ci   }
337bf215546Sopenharmony_ci}
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_civoid GLAPIENTRY
341bf215546Sopenharmony_ci_mesa_ActiveTexture_no_error(GLenum texture)
342bf215546Sopenharmony_ci{
343bf215546Sopenharmony_ci   active_texture(texture, true);
344bf215546Sopenharmony_ci}
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_civoid GLAPIENTRY
348bf215546Sopenharmony_ci_mesa_ActiveTexture(GLenum texture)
349bf215546Sopenharmony_ci{
350bf215546Sopenharmony_ci   active_texture(texture, false);
351bf215546Sopenharmony_ci}
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci/* GL_ARB_multitexture */
355bf215546Sopenharmony_civoid GLAPIENTRY
356bf215546Sopenharmony_ci_mesa_ClientActiveTexture(GLenum texture)
357bf215546Sopenharmony_ci{
358bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
359bf215546Sopenharmony_ci   GLuint texUnit = texture - GL_TEXTURE0;
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci   if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE))
362bf215546Sopenharmony_ci      _mesa_debug(ctx, "glClientActiveTexture %s\n",
363bf215546Sopenharmony_ci                  _mesa_enum_to_string(texture));
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci   if (ctx->Array.ActiveTexture == texUnit)
366bf215546Sopenharmony_ci      return;
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci   if (texUnit >= ctx->Const.MaxTextureCoordUnits) {
369bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(texture=%s)",
370bf215546Sopenharmony_ci                  _mesa_enum_to_string(texture));
371bf215546Sopenharmony_ci      return;
372bf215546Sopenharmony_ci   }
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci   /* Don't flush vertices. This is a "latched" state. */
375bf215546Sopenharmony_ci   ctx->Array.ActiveTexture = texUnit;
376bf215546Sopenharmony_ci}
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci/**********************************************************************/
381bf215546Sopenharmony_ci/*****                    State management                        *****/
382bf215546Sopenharmony_ci/**********************************************************************/
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci/**
386bf215546Sopenharmony_ci * \note This routine refers to derived texture attribute values to
387bf215546Sopenharmony_ci * compute the ENABLE_TEXMAT flags, but is only called on
388bf215546Sopenharmony_ci * _NEW_TEXTURE_MATRIX.  On changes to _NEW_TEXTURE_OBJECT/STATE,
389bf215546Sopenharmony_ci * the ENABLE_TEXMAT flags are updated by _mesa_update_textures(), below.
390bf215546Sopenharmony_ci *
391bf215546Sopenharmony_ci * \param ctx GL context.
392bf215546Sopenharmony_ci */
393bf215546Sopenharmony_ciGLbitfield
394bf215546Sopenharmony_ci_mesa_update_texture_matrices(struct gl_context *ctx)
395bf215546Sopenharmony_ci{
396bf215546Sopenharmony_ci   GLuint u;
397bf215546Sopenharmony_ci   GLbitfield old_texmat_enabled = ctx->Texture._TexMatEnabled;
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci   ctx->Texture._TexMatEnabled = 0x0;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci   for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
402bf215546Sopenharmony_ci      assert(u < ARRAY_SIZE(ctx->TextureMatrixStack));
403bf215546Sopenharmony_ci      if (_math_matrix_is_dirty(ctx->TextureMatrixStack[u].Top)) {
404bf215546Sopenharmony_ci	 _math_matrix_analyse( ctx->TextureMatrixStack[u].Top );
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci	 if (ctx->Texture.Unit[u]._Current &&
407bf215546Sopenharmony_ci	     ctx->TextureMatrixStack[u].Top->type != MATRIX_IDENTITY)
408bf215546Sopenharmony_ci	    ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(u);
409bf215546Sopenharmony_ci      }
410bf215546Sopenharmony_ci   }
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   if (old_texmat_enabled != ctx->Texture._TexMatEnabled)
413bf215546Sopenharmony_ci      return _NEW_FF_VERT_PROGRAM | _NEW_FF_FRAG_PROGRAM;
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   return 0;
416bf215546Sopenharmony_ci}
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci/**
420bf215546Sopenharmony_ci * Translate GL combiner state into a MODE_x value
421bf215546Sopenharmony_ci */
422bf215546Sopenharmony_cistatic uint32_t
423bf215546Sopenharmony_citex_combine_translate_mode(GLenum envMode, GLenum mode)
424bf215546Sopenharmony_ci{
425bf215546Sopenharmony_ci   switch (mode) {
426bf215546Sopenharmony_ci   case GL_REPLACE: return TEXENV_MODE_REPLACE;
427bf215546Sopenharmony_ci   case GL_MODULATE: return TEXENV_MODE_MODULATE;
428bf215546Sopenharmony_ci   case GL_ADD:
429bf215546Sopenharmony_ci      if (envMode == GL_COMBINE4_NV)
430bf215546Sopenharmony_ci	 return TEXENV_MODE_ADD_PRODUCTS_NV;
431bf215546Sopenharmony_ci      else
432bf215546Sopenharmony_ci	 return TEXENV_MODE_ADD;
433bf215546Sopenharmony_ci   case GL_ADD_SIGNED:
434bf215546Sopenharmony_ci      if (envMode == GL_COMBINE4_NV)
435bf215546Sopenharmony_ci	 return TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV;
436bf215546Sopenharmony_ci      else
437bf215546Sopenharmony_ci	 return TEXENV_MODE_ADD_SIGNED;
438bf215546Sopenharmony_ci   case GL_INTERPOLATE: return TEXENV_MODE_INTERPOLATE;
439bf215546Sopenharmony_ci   case GL_SUBTRACT: return TEXENV_MODE_SUBTRACT;
440bf215546Sopenharmony_ci   case GL_DOT3_RGB: return TEXENV_MODE_DOT3_RGB;
441bf215546Sopenharmony_ci   case GL_DOT3_RGB_EXT: return TEXENV_MODE_DOT3_RGB_EXT;
442bf215546Sopenharmony_ci   case GL_DOT3_RGBA: return TEXENV_MODE_DOT3_RGBA;
443bf215546Sopenharmony_ci   case GL_DOT3_RGBA_EXT: return TEXENV_MODE_DOT3_RGBA_EXT;
444bf215546Sopenharmony_ci   case GL_MODULATE_ADD_ATI: return TEXENV_MODE_MODULATE_ADD_ATI;
445bf215546Sopenharmony_ci   case GL_MODULATE_SIGNED_ADD_ATI: return TEXENV_MODE_MODULATE_SIGNED_ADD_ATI;
446bf215546Sopenharmony_ci   case GL_MODULATE_SUBTRACT_ATI: return TEXENV_MODE_MODULATE_SUBTRACT_ATI;
447bf215546Sopenharmony_ci   default:
448bf215546Sopenharmony_ci      unreachable("Invalid TexEnv Combine mode");
449bf215546Sopenharmony_ci   }
450bf215546Sopenharmony_ci}
451bf215546Sopenharmony_ci
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_cistatic uint8_t
454bf215546Sopenharmony_citex_combine_translate_source(GLenum src)
455bf215546Sopenharmony_ci{
456bf215546Sopenharmony_ci   switch (src) {
457bf215546Sopenharmony_ci   case GL_TEXTURE0:
458bf215546Sopenharmony_ci   case GL_TEXTURE1:
459bf215546Sopenharmony_ci   case GL_TEXTURE2:
460bf215546Sopenharmony_ci   case GL_TEXTURE3:
461bf215546Sopenharmony_ci   case GL_TEXTURE4:
462bf215546Sopenharmony_ci   case GL_TEXTURE5:
463bf215546Sopenharmony_ci   case GL_TEXTURE6:
464bf215546Sopenharmony_ci   case GL_TEXTURE7: return TEXENV_SRC_TEXTURE0 + (src - GL_TEXTURE0);
465bf215546Sopenharmony_ci   case GL_TEXTURE: return TEXENV_SRC_TEXTURE;
466bf215546Sopenharmony_ci   case GL_PREVIOUS: return TEXENV_SRC_PREVIOUS;
467bf215546Sopenharmony_ci   case GL_PRIMARY_COLOR: return TEXENV_SRC_PRIMARY_COLOR;
468bf215546Sopenharmony_ci   case GL_CONSTANT: return TEXENV_SRC_CONSTANT;
469bf215546Sopenharmony_ci   case GL_ZERO: return TEXENV_SRC_ZERO;
470bf215546Sopenharmony_ci   case GL_ONE: return TEXENV_SRC_ONE;
471bf215546Sopenharmony_ci   default:
472bf215546Sopenharmony_ci      unreachable("Invalid TexEnv Combine argument source");
473bf215546Sopenharmony_ci   }
474bf215546Sopenharmony_ci}
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_cistatic uint8_t
478bf215546Sopenharmony_citex_combine_translate_operand(GLenum operand)
479bf215546Sopenharmony_ci{
480bf215546Sopenharmony_ci   switch (operand) {
481bf215546Sopenharmony_ci   case GL_SRC_COLOR: return TEXENV_OPR_COLOR;
482bf215546Sopenharmony_ci   case GL_ONE_MINUS_SRC_COLOR: return TEXENV_OPR_ONE_MINUS_COLOR;
483bf215546Sopenharmony_ci   case GL_SRC_ALPHA: return TEXENV_OPR_ALPHA;
484bf215546Sopenharmony_ci   case GL_ONE_MINUS_SRC_ALPHA: return TEXENV_OPR_ONE_MINUS_ALPHA;
485bf215546Sopenharmony_ci   default:
486bf215546Sopenharmony_ci      unreachable("Invalid TexEnv Combine argument source");
487bf215546Sopenharmony_ci   }
488bf215546Sopenharmony_ci}
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_cistatic void
492bf215546Sopenharmony_cipack_tex_combine(struct gl_fixedfunc_texture_unit *texUnit)
493bf215546Sopenharmony_ci{
494bf215546Sopenharmony_ci   struct gl_tex_env_combine_state *state = texUnit->_CurrentCombine;
495bf215546Sopenharmony_ci   struct gl_tex_env_combine_packed *packed = &texUnit->_CurrentCombinePacked;
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci   memset(packed, 0, sizeof *packed);
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci   packed->ModeRGB = tex_combine_translate_mode(texUnit->EnvMode, state->ModeRGB);
500bf215546Sopenharmony_ci   packed->ModeA = tex_combine_translate_mode(texUnit->EnvMode, state->ModeA);
501bf215546Sopenharmony_ci   packed->ScaleShiftRGB = state->ScaleShiftRGB;
502bf215546Sopenharmony_ci   packed->ScaleShiftA = state->ScaleShiftA;
503bf215546Sopenharmony_ci   packed->NumArgsRGB = state->_NumArgsRGB;
504bf215546Sopenharmony_ci   packed->NumArgsA = state->_NumArgsA;
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   for (int i = 0; i < state->_NumArgsRGB; ++i)
507bf215546Sopenharmony_ci   {
508bf215546Sopenharmony_ci      packed->ArgsRGB[i].Source = tex_combine_translate_source(state->SourceRGB[i]);
509bf215546Sopenharmony_ci      packed->ArgsRGB[i].Operand = tex_combine_translate_operand(state->OperandRGB[i]);
510bf215546Sopenharmony_ci   }
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   for (int i = 0; i < state->_NumArgsA; ++i)
513bf215546Sopenharmony_ci   {
514bf215546Sopenharmony_ci      packed->ArgsA[i].Source = tex_combine_translate_source(state->SourceA[i]);
515bf215546Sopenharmony_ci      packed->ArgsA[i].Operand = tex_combine_translate_operand(state->OperandA[i]);
516bf215546Sopenharmony_ci   }
517bf215546Sopenharmony_ci}
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci
520bf215546Sopenharmony_ci/**
521bf215546Sopenharmony_ci * Examine texture unit's combine/env state to update derived state.
522bf215546Sopenharmony_ci */
523bf215546Sopenharmony_cistatic void
524bf215546Sopenharmony_ciupdate_tex_combine(struct gl_context *ctx,
525bf215546Sopenharmony_ci                   struct gl_texture_unit *texUnit,
526bf215546Sopenharmony_ci                   struct gl_fixedfunc_texture_unit *fftexUnit)
527bf215546Sopenharmony_ci{
528bf215546Sopenharmony_ci   struct gl_tex_env_combine_state *combine;
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_ci   /* No combiners will apply to this. */
531bf215546Sopenharmony_ci   if (texUnit->_Current->Target == GL_TEXTURE_BUFFER)
532bf215546Sopenharmony_ci      return;
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_ci   /* Set the texUnit->_CurrentCombine field to point to the user's combiner
535bf215546Sopenharmony_ci    * state, or the combiner state which is derived from traditional texenv
536bf215546Sopenharmony_ci    * mode.
537bf215546Sopenharmony_ci    */
538bf215546Sopenharmony_ci   if (fftexUnit->EnvMode == GL_COMBINE ||
539bf215546Sopenharmony_ci       fftexUnit->EnvMode == GL_COMBINE4_NV) {
540bf215546Sopenharmony_ci      fftexUnit->_CurrentCombine = & fftexUnit->Combine;
541bf215546Sopenharmony_ci   }
542bf215546Sopenharmony_ci   else {
543bf215546Sopenharmony_ci      const struct gl_texture_object *texObj = texUnit->_Current;
544bf215546Sopenharmony_ci      GLenum format = texObj->Image[0][texObj->Attrib.BaseLevel]->_BaseFormat;
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci      if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL_EXT) {
547bf215546Sopenharmony_ci         format = texObj->Attrib.DepthMode;
548bf215546Sopenharmony_ci      }
549bf215546Sopenharmony_ci      calculate_derived_texenv(&fftexUnit->_EnvMode, fftexUnit->EnvMode, format);
550bf215546Sopenharmony_ci      fftexUnit->_CurrentCombine = & fftexUnit->_EnvMode;
551bf215546Sopenharmony_ci   }
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci   combine = fftexUnit->_CurrentCombine;
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci   /* Determine number of source RGB terms in the combiner function */
556bf215546Sopenharmony_ci   switch (combine->ModeRGB) {
557bf215546Sopenharmony_ci   case GL_REPLACE:
558bf215546Sopenharmony_ci      combine->_NumArgsRGB = 1;
559bf215546Sopenharmony_ci      break;
560bf215546Sopenharmony_ci   case GL_ADD:
561bf215546Sopenharmony_ci   case GL_ADD_SIGNED:
562bf215546Sopenharmony_ci      if (fftexUnit->EnvMode == GL_COMBINE4_NV)
563bf215546Sopenharmony_ci         combine->_NumArgsRGB = 4;
564bf215546Sopenharmony_ci      else
565bf215546Sopenharmony_ci         combine->_NumArgsRGB = 2;
566bf215546Sopenharmony_ci      break;
567bf215546Sopenharmony_ci   case GL_MODULATE:
568bf215546Sopenharmony_ci   case GL_SUBTRACT:
569bf215546Sopenharmony_ci   case GL_DOT3_RGB:
570bf215546Sopenharmony_ci   case GL_DOT3_RGBA:
571bf215546Sopenharmony_ci   case GL_DOT3_RGB_EXT:
572bf215546Sopenharmony_ci   case GL_DOT3_RGBA_EXT:
573bf215546Sopenharmony_ci      combine->_NumArgsRGB = 2;
574bf215546Sopenharmony_ci      break;
575bf215546Sopenharmony_ci   case GL_INTERPOLATE:
576bf215546Sopenharmony_ci   case GL_MODULATE_ADD_ATI:
577bf215546Sopenharmony_ci   case GL_MODULATE_SIGNED_ADD_ATI:
578bf215546Sopenharmony_ci   case GL_MODULATE_SUBTRACT_ATI:
579bf215546Sopenharmony_ci      combine->_NumArgsRGB = 3;
580bf215546Sopenharmony_ci      break;
581bf215546Sopenharmony_ci   default:
582bf215546Sopenharmony_ci      combine->_NumArgsRGB = 0;
583bf215546Sopenharmony_ci      _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
584bf215546Sopenharmony_ci      return;
585bf215546Sopenharmony_ci   }
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci   /* Determine number of source Alpha terms in the combiner function */
588bf215546Sopenharmony_ci   switch (combine->ModeA) {
589bf215546Sopenharmony_ci   case GL_REPLACE:
590bf215546Sopenharmony_ci      combine->_NumArgsA = 1;
591bf215546Sopenharmony_ci      break;
592bf215546Sopenharmony_ci   case GL_ADD:
593bf215546Sopenharmony_ci   case GL_ADD_SIGNED:
594bf215546Sopenharmony_ci      if (fftexUnit->EnvMode == GL_COMBINE4_NV)
595bf215546Sopenharmony_ci         combine->_NumArgsA = 4;
596bf215546Sopenharmony_ci      else
597bf215546Sopenharmony_ci         combine->_NumArgsA = 2;
598bf215546Sopenharmony_ci      break;
599bf215546Sopenharmony_ci   case GL_MODULATE:
600bf215546Sopenharmony_ci   case GL_SUBTRACT:
601bf215546Sopenharmony_ci      combine->_NumArgsA = 2;
602bf215546Sopenharmony_ci      break;
603bf215546Sopenharmony_ci   case GL_INTERPOLATE:
604bf215546Sopenharmony_ci   case GL_MODULATE_ADD_ATI:
605bf215546Sopenharmony_ci   case GL_MODULATE_SIGNED_ADD_ATI:
606bf215546Sopenharmony_ci   case GL_MODULATE_SUBTRACT_ATI:
607bf215546Sopenharmony_ci      combine->_NumArgsA = 3;
608bf215546Sopenharmony_ci      break;
609bf215546Sopenharmony_ci   default:
610bf215546Sopenharmony_ci      combine->_NumArgsA = 0;
611bf215546Sopenharmony_ci      _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
612bf215546Sopenharmony_ci      break;
613bf215546Sopenharmony_ci   }
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci   pack_tex_combine(fftexUnit);
616bf215546Sopenharmony_ci}
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_cistatic void
619bf215546Sopenharmony_ciupdate_texgen(struct gl_context *ctx)
620bf215546Sopenharmony_ci{
621bf215546Sopenharmony_ci   GLuint unit;
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci   /* Setup texgen for those texture coordinate sets that are in use */
624bf215546Sopenharmony_ci   for (unit = 0; unit < ctx->Const.MaxTextureCoordUnits; unit++) {
625bf215546Sopenharmony_ci      struct gl_fixedfunc_texture_unit *texUnit =
626bf215546Sopenharmony_ci         &ctx->Texture.FixedFuncUnit[unit];
627bf215546Sopenharmony_ci
628bf215546Sopenharmony_ci      texUnit->_GenFlags = 0x0;
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci      if (!(ctx->Texture._EnabledCoordUnits & (1 << unit)))
631bf215546Sopenharmony_ci	 continue;
632bf215546Sopenharmony_ci
633bf215546Sopenharmony_ci      if (texUnit->TexGenEnabled) {
634bf215546Sopenharmony_ci	 if (texUnit->TexGenEnabled & S_BIT) {
635bf215546Sopenharmony_ci	    texUnit->_GenFlags |= texUnit->GenS._ModeBit;
636bf215546Sopenharmony_ci	 }
637bf215546Sopenharmony_ci	 if (texUnit->TexGenEnabled & T_BIT) {
638bf215546Sopenharmony_ci	    texUnit->_GenFlags |= texUnit->GenT._ModeBit;
639bf215546Sopenharmony_ci	 }
640bf215546Sopenharmony_ci	 if (texUnit->TexGenEnabled & R_BIT) {
641bf215546Sopenharmony_ci	    texUnit->_GenFlags |= texUnit->GenR._ModeBit;
642bf215546Sopenharmony_ci	 }
643bf215546Sopenharmony_ci	 if (texUnit->TexGenEnabled & Q_BIT) {
644bf215546Sopenharmony_ci	    texUnit->_GenFlags |= texUnit->GenQ._ModeBit;
645bf215546Sopenharmony_ci	 }
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci	 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
648bf215546Sopenharmony_ci	 ctx->Texture._GenFlags |= texUnit->_GenFlags;
649bf215546Sopenharmony_ci      }
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_ci      assert(unit < ARRAY_SIZE(ctx->TextureMatrixStack));
652bf215546Sopenharmony_ci      if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
653bf215546Sopenharmony_ci	 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
654bf215546Sopenharmony_ci   }
655bf215546Sopenharmony_ci}
656bf215546Sopenharmony_ci
657bf215546Sopenharmony_cistatic struct gl_texture_object *
658bf215546Sopenharmony_ciupdate_single_program_texture(struct gl_context *ctx, struct gl_program *prog,
659bf215546Sopenharmony_ci                              int unit)
660bf215546Sopenharmony_ci{
661bf215546Sopenharmony_ci   gl_texture_index target_index;
662bf215546Sopenharmony_ci   struct gl_texture_unit *texUnit;
663bf215546Sopenharmony_ci   struct gl_texture_object *texObj;
664bf215546Sopenharmony_ci   struct gl_sampler_object *sampler;
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci   texUnit = &ctx->Texture.Unit[unit];
667bf215546Sopenharmony_ci
668bf215546Sopenharmony_ci   /* Note: If more than one bit was set in TexturesUsed[unit], then we should
669bf215546Sopenharmony_ci    * have had the draw call rejected already.  From the GL 4.4 specification,
670bf215546Sopenharmony_ci    * section 7.10 ("Samplers"):
671bf215546Sopenharmony_ci    *
672bf215546Sopenharmony_ci    *     "It is not allowed to have variables of different sampler types
673bf215546Sopenharmony_ci    *      pointing to the same texture image unit within a program
674bf215546Sopenharmony_ci    *      object. This situation can only be detected at the next rendering
675bf215546Sopenharmony_ci    *      command issued which triggers shader invocations, and an
676bf215546Sopenharmony_ci    *      INVALID_OPERATION error will then be generated."
677bf215546Sopenharmony_ci    */
678bf215546Sopenharmony_ci   target_index = ffs(prog->TexturesUsed[unit]) - 1;
679bf215546Sopenharmony_ci   texObj = texUnit->CurrentTex[target_index];
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci   sampler = texUnit->Sampler ?
682bf215546Sopenharmony_ci      texUnit->Sampler : &texObj->Sampler;
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci   if (likely(texObj)) {
685bf215546Sopenharmony_ci      if (_mesa_is_texture_complete(texObj, sampler,
686bf215546Sopenharmony_ci                                    ctx->Const.ForceIntegerTexNearest))
687bf215546Sopenharmony_ci         return texObj;
688bf215546Sopenharmony_ci
689bf215546Sopenharmony_ci      _mesa_test_texobj_completeness(ctx, texObj);
690bf215546Sopenharmony_ci      if (_mesa_is_texture_complete(texObj, sampler,
691bf215546Sopenharmony_ci                                    ctx->Const.ForceIntegerTexNearest))
692bf215546Sopenharmony_ci         return texObj;
693bf215546Sopenharmony_ci   }
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci   /* If we've reached this point, we didn't find a complete texture of the
696bf215546Sopenharmony_ci    * shader's target.  From the GL 4.4 core specification, section 11.1.3.5
697bf215546Sopenharmony_ci    * ("Texture Access"):
698bf215546Sopenharmony_ci    *
699bf215546Sopenharmony_ci    *     "If a sampler is used in a shader and the sampler’s associated
700bf215546Sopenharmony_ci    *      texture is not complete, as defined in section 8.17, (0, 0, 0, 1)
701bf215546Sopenharmony_ci    *      will be returned for a non-shadow sampler and 0 for a shadow
702bf215546Sopenharmony_ci    *      sampler."
703bf215546Sopenharmony_ci    *
704bf215546Sopenharmony_ci    * Mesa implements this by creating a hidden texture object with a pixel of
705bf215546Sopenharmony_ci    * that value.
706bf215546Sopenharmony_ci    */
707bf215546Sopenharmony_ci   texObj = _mesa_get_fallback_texture(ctx, target_index);
708bf215546Sopenharmony_ci   assert(texObj);
709bf215546Sopenharmony_ci
710bf215546Sopenharmony_ci   return texObj;
711bf215546Sopenharmony_ci}
712bf215546Sopenharmony_ci
713bf215546Sopenharmony_cistatic inline void
714bf215546Sopenharmony_ciupdate_single_program_texture_state(struct gl_context *ctx,
715bf215546Sopenharmony_ci                                    struct gl_program *prog,
716bf215546Sopenharmony_ci                                    int unit,
717bf215546Sopenharmony_ci                                    BITSET_WORD *enabled_texture_units)
718bf215546Sopenharmony_ci{
719bf215546Sopenharmony_ci   struct gl_texture_object *texObj;
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci   texObj = update_single_program_texture(ctx, prog, unit);
722bf215546Sopenharmony_ci
723bf215546Sopenharmony_ci   _mesa_reference_texobj(&ctx->Texture.Unit[unit]._Current, texObj);
724bf215546Sopenharmony_ci   BITSET_SET(enabled_texture_units, unit);
725bf215546Sopenharmony_ci   ctx->Texture._MaxEnabledTexImageUnit =
726bf215546Sopenharmony_ci      MAX2(ctx->Texture._MaxEnabledTexImageUnit, (int)unit);
727bf215546Sopenharmony_ci}
728bf215546Sopenharmony_ci
729bf215546Sopenharmony_cistatic void
730bf215546Sopenharmony_ciupdate_program_texture_state(struct gl_context *ctx, struct gl_program **prog,
731bf215546Sopenharmony_ci                             BITSET_WORD *enabled_texture_units)
732bf215546Sopenharmony_ci{
733bf215546Sopenharmony_ci   int i;
734bf215546Sopenharmony_ci
735bf215546Sopenharmony_ci   for (i = 0; i < MESA_SHADER_STAGES; i++) {
736bf215546Sopenharmony_ci      GLbitfield mask;
737bf215546Sopenharmony_ci      GLuint s;
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci      if (!prog[i])
740bf215546Sopenharmony_ci         continue;
741bf215546Sopenharmony_ci
742bf215546Sopenharmony_ci      mask = prog[i]->SamplersUsed;
743bf215546Sopenharmony_ci
744bf215546Sopenharmony_ci      while (mask) {
745bf215546Sopenharmony_ci         s = u_bit_scan(&mask);
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_ci         update_single_program_texture_state(ctx, prog[i],
748bf215546Sopenharmony_ci                                             prog[i]->SamplerUnits[s],
749bf215546Sopenharmony_ci                                             enabled_texture_units);
750bf215546Sopenharmony_ci      }
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_ci      if (unlikely(prog[i]->sh.HasBoundBindlessSampler)) {
753bf215546Sopenharmony_ci         /* Loop over bindless samplers bound to texture units.
754bf215546Sopenharmony_ci          */
755bf215546Sopenharmony_ci         for (s = 0; s < prog[i]->sh.NumBindlessSamplers; s++) {
756bf215546Sopenharmony_ci            struct gl_bindless_sampler *sampler =
757bf215546Sopenharmony_ci               &prog[i]->sh.BindlessSamplers[s];
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci            if (!sampler->bound)
760bf215546Sopenharmony_ci               continue;
761bf215546Sopenharmony_ci
762bf215546Sopenharmony_ci            update_single_program_texture_state(ctx, prog[i], sampler->unit,
763bf215546Sopenharmony_ci                                                enabled_texture_units);
764bf215546Sopenharmony_ci         }
765bf215546Sopenharmony_ci      }
766bf215546Sopenharmony_ci   }
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   if (prog[MESA_SHADER_FRAGMENT]) {
769bf215546Sopenharmony_ci      const GLuint coordMask = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
770bf215546Sopenharmony_ci      ctx->Texture._EnabledCoordUnits |=
771bf215546Sopenharmony_ci         (prog[MESA_SHADER_FRAGMENT]->info.inputs_read >> VARYING_SLOT_TEX0) &
772bf215546Sopenharmony_ci         coordMask;
773bf215546Sopenharmony_ci   }
774bf215546Sopenharmony_ci}
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_cistatic void
777bf215546Sopenharmony_ciupdate_ff_texture_state(struct gl_context *ctx,
778bf215546Sopenharmony_ci                        BITSET_WORD *enabled_texture_units)
779bf215546Sopenharmony_ci{
780bf215546Sopenharmony_ci   int unit;
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_ci   for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
783bf215546Sopenharmony_ci      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
784bf215546Sopenharmony_ci      struct gl_fixedfunc_texture_unit *fftexUnit =
785bf215546Sopenharmony_ci         &ctx->Texture.FixedFuncUnit[unit];
786bf215546Sopenharmony_ci      GLbitfield mask;
787bf215546Sopenharmony_ci      bool complete;
788bf215546Sopenharmony_ci
789bf215546Sopenharmony_ci      if (fftexUnit->Enabled == 0x0)
790bf215546Sopenharmony_ci         continue;
791bf215546Sopenharmony_ci
792bf215546Sopenharmony_ci      /* If a shader already dictated what texture target was used for this
793bf215546Sopenharmony_ci       * unit, just go along with it.
794bf215546Sopenharmony_ci       */
795bf215546Sopenharmony_ci      if (BITSET_TEST(enabled_texture_units, unit))
796bf215546Sopenharmony_ci         continue;
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci      /* From the GL 4.4 compat specification, section 16.2 ("Texture Application"):
799bf215546Sopenharmony_ci       *
800bf215546Sopenharmony_ci       *     "Texturing is enabled or disabled using the generic Enable and
801bf215546Sopenharmony_ci       *      Disable commands, respectively, with the symbolic constants
802bf215546Sopenharmony_ci       *      TEXTURE_1D, TEXTURE_2D, TEXTURE_RECTANGLE, TEXTURE_3D, or
803bf215546Sopenharmony_ci       *      TEXTURE_CUBE_MAP to enable the one-, two-, rectangular,
804bf215546Sopenharmony_ci       *      three-dimensional, or cube map texture, respectively. If more
805bf215546Sopenharmony_ci       *      than one of these textures is enabled, the first one enabled
806bf215546Sopenharmony_ci       *      from the following list is used:
807bf215546Sopenharmony_ci       *
808bf215546Sopenharmony_ci       *      • cube map texture
809bf215546Sopenharmony_ci       *      • three-dimensional texture
810bf215546Sopenharmony_ci       *      • rectangular texture
811bf215546Sopenharmony_ci       *      • two-dimensional texture
812bf215546Sopenharmony_ci       *      • one-dimensional texture"
813bf215546Sopenharmony_ci       *
814bf215546Sopenharmony_ci       * Note that the TEXTURE_x_INDEX values are in high to low priority.
815bf215546Sopenharmony_ci       * Also:
816bf215546Sopenharmony_ci       *
817bf215546Sopenharmony_ci       *     "If a texture unit is disabled or has an invalid or incomplete
818bf215546Sopenharmony_ci       *      texture (as defined in section 8.17) bound to it, then blending
819bf215546Sopenharmony_ci       *      is disabled for that texture unit. If the texture environment
820bf215546Sopenharmony_ci       *      for a given enabled texture unit references a disabled texture
821bf215546Sopenharmony_ci       *      unit, or an invalid or incomplete texture that is bound to
822bf215546Sopenharmony_ci       *      another unit, then the results of texture blending are
823bf215546Sopenharmony_ci       *      undefined."
824bf215546Sopenharmony_ci       */
825bf215546Sopenharmony_ci      complete = false;
826bf215546Sopenharmony_ci      mask = fftexUnit->Enabled;
827bf215546Sopenharmony_ci      while (mask) {
828bf215546Sopenharmony_ci         const int texIndex = u_bit_scan(&mask);
829bf215546Sopenharmony_ci         struct gl_texture_object *texObj = texUnit->CurrentTex[texIndex];
830bf215546Sopenharmony_ci         struct gl_sampler_object *sampler = texUnit->Sampler ?
831bf215546Sopenharmony_ci            texUnit->Sampler : &texObj->Sampler;
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci         if (!_mesa_is_texture_complete(texObj, sampler,
834bf215546Sopenharmony_ci                                        ctx->Const.ForceIntegerTexNearest)) {
835bf215546Sopenharmony_ci            _mesa_test_texobj_completeness(ctx, texObj);
836bf215546Sopenharmony_ci         }
837bf215546Sopenharmony_ci         if (_mesa_is_texture_complete(texObj, sampler,
838bf215546Sopenharmony_ci                                       ctx->Const.ForceIntegerTexNearest)) {
839bf215546Sopenharmony_ci            _mesa_reference_texobj(&texUnit->_Current, texObj);
840bf215546Sopenharmony_ci            complete = true;
841bf215546Sopenharmony_ci            break;
842bf215546Sopenharmony_ci         }
843bf215546Sopenharmony_ci      }
844bf215546Sopenharmony_ci
845bf215546Sopenharmony_ci      if (!complete)
846bf215546Sopenharmony_ci         continue;
847bf215546Sopenharmony_ci
848bf215546Sopenharmony_ci      /* if we get here, we know this texture unit is enabled */
849bf215546Sopenharmony_ci      BITSET_SET(enabled_texture_units, unit);
850bf215546Sopenharmony_ci      ctx->Texture._MaxEnabledTexImageUnit =
851bf215546Sopenharmony_ci         MAX2(ctx->Texture._MaxEnabledTexImageUnit, (int)unit);
852bf215546Sopenharmony_ci
853bf215546Sopenharmony_ci      ctx->Texture._EnabledCoordUnits |= 1 << unit;
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_ci      update_tex_combine(ctx, texUnit, fftexUnit);
856bf215546Sopenharmony_ci   }
857bf215546Sopenharmony_ci}
858bf215546Sopenharmony_ci
859bf215546Sopenharmony_cistatic void
860bf215546Sopenharmony_cifix_missing_textures_for_atifs(struct gl_context *ctx,
861bf215546Sopenharmony_ci                               struct gl_program *prog,
862bf215546Sopenharmony_ci                               BITSET_WORD *enabled_texture_units)
863bf215546Sopenharmony_ci{
864bf215546Sopenharmony_ci   GLbitfield mask = prog->SamplersUsed;
865bf215546Sopenharmony_ci
866bf215546Sopenharmony_ci   while (mask) {
867bf215546Sopenharmony_ci      const int s = u_bit_scan(&mask);
868bf215546Sopenharmony_ci      const int unit = prog->SamplerUnits[s];
869bf215546Sopenharmony_ci      const gl_texture_index target_index = ffs(prog->TexturesUsed[unit]) - 1;
870bf215546Sopenharmony_ci
871bf215546Sopenharmony_ci      if (!ctx->Texture.Unit[unit]._Current) {
872bf215546Sopenharmony_ci         struct gl_texture_object *texObj =
873bf215546Sopenharmony_ci            _mesa_get_fallback_texture(ctx, target_index);
874bf215546Sopenharmony_ci         _mesa_reference_texobj(&ctx->Texture.Unit[unit]._Current, texObj);
875bf215546Sopenharmony_ci         BITSET_SET(enabled_texture_units, unit);
876bf215546Sopenharmony_ci         ctx->Texture._MaxEnabledTexImageUnit =
877bf215546Sopenharmony_ci            MAX2(ctx->Texture._MaxEnabledTexImageUnit, (int)unit);
878bf215546Sopenharmony_ci      }
879bf215546Sopenharmony_ci   }
880bf215546Sopenharmony_ci}
881bf215546Sopenharmony_ci
882bf215546Sopenharmony_ci/**
883bf215546Sopenharmony_ci * \note This routine refers to derived texture matrix values to
884bf215546Sopenharmony_ci * compute the ENABLE_TEXMAT flags, but is only called on
885bf215546Sopenharmony_ci * _NEW_TEXTURE_OBJECT/STATE.  On changes to _NEW_TEXTURE_MATRIX,
886bf215546Sopenharmony_ci * the ENABLE_TEXMAT flags are updated by _mesa_update_texture_matrices,
887bf215546Sopenharmony_ci * above.
888bf215546Sopenharmony_ci *
889bf215546Sopenharmony_ci * \param ctx GL context.
890bf215546Sopenharmony_ci */
891bf215546Sopenharmony_ciGLbitfield
892bf215546Sopenharmony_ci_mesa_update_texture_state(struct gl_context *ctx)
893bf215546Sopenharmony_ci{
894bf215546Sopenharmony_ci   struct gl_program *prog[MESA_SHADER_STAGES];
895bf215546Sopenharmony_ci   int i;
896bf215546Sopenharmony_ci   int old_max_unit = ctx->Texture._MaxEnabledTexImageUnit;
897bf215546Sopenharmony_ci   BITSET_DECLARE(enabled_texture_units, MAX_COMBINED_TEXTURE_IMAGE_UNITS);
898bf215546Sopenharmony_ci
899bf215546Sopenharmony_ci   memcpy(prog, ctx->_Shader->CurrentProgram, sizeof(prog));
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_ci   if (prog[MESA_SHADER_FRAGMENT] == NULL &&
902bf215546Sopenharmony_ci       _mesa_arb_fragment_program_enabled(ctx)) {
903bf215546Sopenharmony_ci      prog[MESA_SHADER_FRAGMENT] = ctx->FragmentProgram.Current;
904bf215546Sopenharmony_ci   }
905bf215546Sopenharmony_ci
906bf215546Sopenharmony_ci   /* TODO: only set this if there are actual changes */
907bf215546Sopenharmony_ci   ctx->NewState |= _NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE;
908bf215546Sopenharmony_ci
909bf215546Sopenharmony_ci   GLbitfield old_genflags = ctx->Texture._GenFlags;
910bf215546Sopenharmony_ci   GLbitfield old_enabled_coord_units = ctx->Texture._EnabledCoordUnits;
911bf215546Sopenharmony_ci   GLbitfield old_texgen_enabled = ctx->Texture._TexGenEnabled;
912bf215546Sopenharmony_ci   GLbitfield old_texmat_enabled = ctx->Texture._TexMatEnabled;
913bf215546Sopenharmony_ci
914bf215546Sopenharmony_ci   ctx->Texture._GenFlags = 0x0;
915bf215546Sopenharmony_ci   ctx->Texture._TexMatEnabled = 0x0;
916bf215546Sopenharmony_ci   ctx->Texture._TexGenEnabled = 0x0;
917bf215546Sopenharmony_ci   ctx->Texture._MaxEnabledTexImageUnit = -1;
918bf215546Sopenharmony_ci   ctx->Texture._EnabledCoordUnits = 0x0;
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci   memset(&enabled_texture_units, 0, sizeof(enabled_texture_units));
921bf215546Sopenharmony_ci
922bf215546Sopenharmony_ci   /* First, walk over our programs pulling in all the textures for them.
923bf215546Sopenharmony_ci    * Programs dictate specific texture targets to be enabled, and for a draw
924bf215546Sopenharmony_ci    * call to be valid they can't conflict about which texture targets are
925bf215546Sopenharmony_ci    * used.
926bf215546Sopenharmony_ci    */
927bf215546Sopenharmony_ci   update_program_texture_state(ctx, prog, enabled_texture_units);
928bf215546Sopenharmony_ci
929bf215546Sopenharmony_ci   /* Also pull in any textures necessary for fixed function fragment shading.
930bf215546Sopenharmony_ci    */
931bf215546Sopenharmony_ci   if (!prog[MESA_SHADER_FRAGMENT])
932bf215546Sopenharmony_ci      update_ff_texture_state(ctx, enabled_texture_units);
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ci   /* Now, clear out the _Current of any disabled texture units. */
935bf215546Sopenharmony_ci   for (i = 0; i <= ctx->Texture._MaxEnabledTexImageUnit; i++) {
936bf215546Sopenharmony_ci      if (!BITSET_TEST(enabled_texture_units, i))
937bf215546Sopenharmony_ci         _mesa_reference_texobj(&ctx->Texture.Unit[i]._Current, NULL);
938bf215546Sopenharmony_ci   }
939bf215546Sopenharmony_ci   for (i = ctx->Texture._MaxEnabledTexImageUnit + 1; i <= old_max_unit; i++) {
940bf215546Sopenharmony_ci      _mesa_reference_texobj(&ctx->Texture.Unit[i]._Current, NULL);
941bf215546Sopenharmony_ci   }
942bf215546Sopenharmony_ci
943bf215546Sopenharmony_ci   /* add fallback texture for SampleMapATI if there is nothing */
944bf215546Sopenharmony_ci   if (_mesa_ati_fragment_shader_enabled(ctx) &&
945bf215546Sopenharmony_ci       ctx->ATIFragmentShader.Current->Program)
946bf215546Sopenharmony_ci      fix_missing_textures_for_atifs(ctx,
947bf215546Sopenharmony_ci                                     ctx->ATIFragmentShader.Current->Program,
948bf215546Sopenharmony_ci                                     enabled_texture_units);
949bf215546Sopenharmony_ci
950bf215546Sopenharmony_ci   if (!prog[MESA_SHADER_FRAGMENT] || !prog[MESA_SHADER_VERTEX])
951bf215546Sopenharmony_ci      update_texgen(ctx);
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci   GLbitfield new_state = 0;
954bf215546Sopenharmony_ci
955bf215546Sopenharmony_ci   if (old_enabled_coord_units != ctx->Texture._EnabledCoordUnits ||
956bf215546Sopenharmony_ci       old_texgen_enabled != ctx->Texture._TexGenEnabled ||
957bf215546Sopenharmony_ci       old_texmat_enabled != ctx->Texture._TexMatEnabled) {
958bf215546Sopenharmony_ci      new_state |= _NEW_FF_VERT_PROGRAM | _NEW_FF_FRAG_PROGRAM;
959bf215546Sopenharmony_ci   }
960bf215546Sopenharmony_ci
961bf215546Sopenharmony_ci   if (old_genflags != ctx->Texture._GenFlags)
962bf215546Sopenharmony_ci      new_state |= _NEW_TNL_SPACES;
963bf215546Sopenharmony_ci
964bf215546Sopenharmony_ci   return new_state;
965bf215546Sopenharmony_ci}
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci
968bf215546Sopenharmony_ci/**********************************************************************/
969bf215546Sopenharmony_ci/*****                      Initialization                        *****/
970bf215546Sopenharmony_ci/**********************************************************************/
971bf215546Sopenharmony_ci
972bf215546Sopenharmony_ci/**
973bf215546Sopenharmony_ci * Allocate the proxy textures for the given context.
974bf215546Sopenharmony_ci *
975bf215546Sopenharmony_ci * \param ctx the context to allocate proxies for.
976bf215546Sopenharmony_ci *
977bf215546Sopenharmony_ci * \return GL_TRUE on success, or GL_FALSE on failure
978bf215546Sopenharmony_ci *
979bf215546Sopenharmony_ci * If run out of memory part way through the allocations, clean up and return
980bf215546Sopenharmony_ci * GL_FALSE.
981bf215546Sopenharmony_ci */
982bf215546Sopenharmony_cistatic GLboolean
983bf215546Sopenharmony_cialloc_proxy_textures( struct gl_context *ctx )
984bf215546Sopenharmony_ci{
985bf215546Sopenharmony_ci   /* NOTE: these values must be in the same order as the TEXTURE_x_INDEX
986bf215546Sopenharmony_ci    * values!
987bf215546Sopenharmony_ci    */
988bf215546Sopenharmony_ci   static const GLenum targets[] = {
989bf215546Sopenharmony_ci      GL_TEXTURE_2D_MULTISAMPLE,
990bf215546Sopenharmony_ci      GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
991bf215546Sopenharmony_ci      GL_TEXTURE_CUBE_MAP_ARRAY,
992bf215546Sopenharmony_ci      GL_TEXTURE_BUFFER,
993bf215546Sopenharmony_ci      GL_TEXTURE_2D_ARRAY_EXT,
994bf215546Sopenharmony_ci      GL_TEXTURE_1D_ARRAY_EXT,
995bf215546Sopenharmony_ci      GL_TEXTURE_EXTERNAL_OES,
996bf215546Sopenharmony_ci      GL_TEXTURE_CUBE_MAP,
997bf215546Sopenharmony_ci      GL_TEXTURE_3D,
998bf215546Sopenharmony_ci      GL_TEXTURE_RECTANGLE_NV,
999bf215546Sopenharmony_ci      GL_TEXTURE_2D,
1000bf215546Sopenharmony_ci      GL_TEXTURE_1D,
1001bf215546Sopenharmony_ci   };
1002bf215546Sopenharmony_ci   GLint tgt;
1003bf215546Sopenharmony_ci
1004bf215546Sopenharmony_ci   STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
1005bf215546Sopenharmony_ci   assert(targets[TEXTURE_2D_INDEX] == GL_TEXTURE_2D);
1006bf215546Sopenharmony_ci   assert(targets[TEXTURE_CUBE_INDEX] == GL_TEXTURE_CUBE_MAP);
1007bf215546Sopenharmony_ci
1008bf215546Sopenharmony_ci   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1009bf215546Sopenharmony_ci      if (!(ctx->Texture.ProxyTex[tgt]
1010bf215546Sopenharmony_ci            = _mesa_new_texture_object(ctx, 0, targets[tgt]))) {
1011bf215546Sopenharmony_ci         /* out of memory, free what we did allocate */
1012bf215546Sopenharmony_ci         while (--tgt >= 0) {
1013bf215546Sopenharmony_ci            _mesa_delete_texture_object(ctx, ctx->Texture.ProxyTex[tgt]);
1014bf215546Sopenharmony_ci         }
1015bf215546Sopenharmony_ci         return GL_FALSE;
1016bf215546Sopenharmony_ci      }
1017bf215546Sopenharmony_ci   }
1018bf215546Sopenharmony_ci
1019bf215546Sopenharmony_ci   assert(ctx->Texture.ProxyTex[0]->RefCount == 1); /* sanity check */
1020bf215546Sopenharmony_ci   return GL_TRUE;
1021bf215546Sopenharmony_ci}
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci
1024bf215546Sopenharmony_ci/**
1025bf215546Sopenharmony_ci * Initialize texture state for the given context.
1026bf215546Sopenharmony_ci */
1027bf215546Sopenharmony_ciGLboolean
1028bf215546Sopenharmony_ci_mesa_init_texture(struct gl_context *ctx)
1029bf215546Sopenharmony_ci{
1030bf215546Sopenharmony_ci   GLuint u;
1031bf215546Sopenharmony_ci
1032bf215546Sopenharmony_ci   /* Texture group */
1033bf215546Sopenharmony_ci   ctx->Texture.CurrentUnit = 0;      /* multitexture */
1034bf215546Sopenharmony_ci
1035bf215546Sopenharmony_ci   /* Appendix F.2 of the OpenGL ES 3.0 spec says:
1036bf215546Sopenharmony_ci    *
1037bf215546Sopenharmony_ci    *     "OpenGL ES 3.0 requires that all cube map filtering be
1038bf215546Sopenharmony_ci    *     seamless. OpenGL ES 2.0 specified that a single cube map face be
1039bf215546Sopenharmony_ci    *     selected and used for filtering."
1040bf215546Sopenharmony_ci    *
1041bf215546Sopenharmony_ci    * Unfortunatley, a call to _mesa_is_gles3 below will only work if
1042bf215546Sopenharmony_ci    * the driver has already computed and set ctx->Version, however drivers
1043bf215546Sopenharmony_ci    * seem to call _mesa_initialize_context (which calls this) early
1044bf215546Sopenharmony_ci    * in the CreateContext hook and _mesa_compute_version much later (since
1045bf215546Sopenharmony_ci    * it needs information about available extensions). So, we will
1046bf215546Sopenharmony_ci    * enable seamless cubemaps by default since GLES2. This should work
1047bf215546Sopenharmony_ci    * for most implementations and drivers that don't support seamless
1048bf215546Sopenharmony_ci    * cubemaps for GLES2 can still disable it.
1049bf215546Sopenharmony_ci    */
1050bf215546Sopenharmony_ci   ctx->Texture.CubeMapSeamless = ctx->API == API_OPENGLES2;
1051bf215546Sopenharmony_ci
1052bf215546Sopenharmony_ci   for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) {
1053bf215546Sopenharmony_ci      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
1054bf215546Sopenharmony_ci      GLuint tex;
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_ci      /* initialize current texture object ptrs to the shared default objects */
1057bf215546Sopenharmony_ci      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
1058bf215546Sopenharmony_ci         _mesa_reference_texobj(&texUnit->CurrentTex[tex],
1059bf215546Sopenharmony_ci                                ctx->Shared->DefaultTex[tex]);
1060bf215546Sopenharmony_ci      }
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_ci      texUnit->_BoundTextures = 0;
1063bf215546Sopenharmony_ci   }
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_ci   for (u = 0; u < ARRAY_SIZE(ctx->Texture.FixedFuncUnit); u++) {
1066bf215546Sopenharmony_ci      struct gl_fixedfunc_texture_unit *texUnit =
1067bf215546Sopenharmony_ci         &ctx->Texture.FixedFuncUnit[u];
1068bf215546Sopenharmony_ci
1069bf215546Sopenharmony_ci      texUnit->EnvMode = GL_MODULATE;
1070bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
1071bf215546Sopenharmony_ci
1072bf215546Sopenharmony_ci      texUnit->Combine = default_combine_state;
1073bf215546Sopenharmony_ci      texUnit->_EnvMode = default_combine_state;
1074bf215546Sopenharmony_ci      texUnit->_CurrentCombine = & texUnit->_EnvMode;
1075bf215546Sopenharmony_ci
1076bf215546Sopenharmony_ci      texUnit->TexGenEnabled = 0x0;
1077bf215546Sopenharmony_ci      texUnit->GenS.Mode = GL_EYE_LINEAR;
1078bf215546Sopenharmony_ci      texUnit->GenT.Mode = GL_EYE_LINEAR;
1079bf215546Sopenharmony_ci      texUnit->GenR.Mode = GL_EYE_LINEAR;
1080bf215546Sopenharmony_ci      texUnit->GenQ.Mode = GL_EYE_LINEAR;
1081bf215546Sopenharmony_ci      texUnit->GenS._ModeBit = TEXGEN_EYE_LINEAR;
1082bf215546Sopenharmony_ci      texUnit->GenT._ModeBit = TEXGEN_EYE_LINEAR;
1083bf215546Sopenharmony_ci      texUnit->GenR._ModeBit = TEXGEN_EYE_LINEAR;
1084bf215546Sopenharmony_ci      texUnit->GenQ._ModeBit = TEXGEN_EYE_LINEAR;
1085bf215546Sopenharmony_ci
1086bf215546Sopenharmony_ci      /* Yes, these plane coefficients are correct! */
1087bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->ObjectPlane[GEN_S], 1.0, 0.0, 0.0, 0.0 );
1088bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->ObjectPlane[GEN_T], 0.0, 1.0, 0.0, 0.0 );
1089bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->ObjectPlane[GEN_R], 0.0, 0.0, 0.0, 0.0 );
1090bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->ObjectPlane[GEN_Q], 0.0, 0.0, 0.0, 0.0 );
1091bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->EyePlane[GEN_S], 1.0, 0.0, 0.0, 0.0 );
1092bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->EyePlane[GEN_T], 0.0, 1.0, 0.0, 0.0 );
1093bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->EyePlane[GEN_R], 0.0, 0.0, 0.0, 0.0 );
1094bf215546Sopenharmony_ci      ASSIGN_4V( texUnit->EyePlane[GEN_Q], 0.0, 0.0, 0.0, 0.0 );
1095bf215546Sopenharmony_ci   }
1096bf215546Sopenharmony_ci
1097bf215546Sopenharmony_ci   /* After we're done initializing the context's texture state the default
1098bf215546Sopenharmony_ci    * texture objects' refcounts should be at least
1099bf215546Sopenharmony_ci    * MAX_COMBINED_TEXTURE_IMAGE_UNITS + 1.
1100bf215546Sopenharmony_ci    */
1101bf215546Sopenharmony_ci   assert(ctx->Shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount
1102bf215546Sopenharmony_ci          >= MAX_COMBINED_TEXTURE_IMAGE_UNITS + 1);
1103bf215546Sopenharmony_ci
1104bf215546Sopenharmony_ci   /* Allocate proxy textures */
1105bf215546Sopenharmony_ci   if (!alloc_proxy_textures( ctx ))
1106bf215546Sopenharmony_ci      return GL_FALSE;
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_ci   /* GL_ARB_texture_buffer_object */
1109bf215546Sopenharmony_ci   _mesa_reference_buffer_object(ctx, &ctx->Texture.BufferObject, NULL);
1110bf215546Sopenharmony_ci
1111bf215546Sopenharmony_ci   ctx->Texture.NumCurrentTexUsed = 0;
1112bf215546Sopenharmony_ci
1113bf215546Sopenharmony_ci   return GL_TRUE;
1114bf215546Sopenharmony_ci}
1115bf215546Sopenharmony_ci
1116bf215546Sopenharmony_ci
1117bf215546Sopenharmony_ci/**
1118bf215546Sopenharmony_ci * Free dynamically-allocted texture data attached to the given context.
1119bf215546Sopenharmony_ci */
1120bf215546Sopenharmony_civoid
1121bf215546Sopenharmony_ci_mesa_free_texture_data(struct gl_context *ctx)
1122bf215546Sopenharmony_ci{
1123bf215546Sopenharmony_ci   GLuint u, tgt;
1124bf215546Sopenharmony_ci
1125bf215546Sopenharmony_ci   /* unreference current textures */
1126bf215546Sopenharmony_ci   for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) {
1127bf215546Sopenharmony_ci      /* The _Current texture could account for another reference */
1128bf215546Sopenharmony_ci      _mesa_reference_texobj(&ctx->Texture.Unit[u]._Current, NULL);
1129bf215546Sopenharmony_ci
1130bf215546Sopenharmony_ci      for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1131bf215546Sopenharmony_ci         _mesa_reference_texobj(&ctx->Texture.Unit[u].CurrentTex[tgt], NULL);
1132bf215546Sopenharmony_ci      }
1133bf215546Sopenharmony_ci   }
1134bf215546Sopenharmony_ci
1135bf215546Sopenharmony_ci   /* Free proxy texture objects */
1136bf215546Sopenharmony_ci   for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++)
1137bf215546Sopenharmony_ci      _mesa_delete_texture_object(ctx, ctx->Texture.ProxyTex[tgt]);
1138bf215546Sopenharmony_ci
1139bf215546Sopenharmony_ci   /* GL_ARB_texture_buffer_object */
1140bf215546Sopenharmony_ci   _mesa_reference_buffer_object(ctx, &ctx->Texture.BufferObject, NULL);
1141bf215546Sopenharmony_ci
1142bf215546Sopenharmony_ci   for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) {
1143bf215546Sopenharmony_ci      _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit[u].Sampler, NULL);
1144bf215546Sopenharmony_ci   }
1145bf215546Sopenharmony_ci}
1146bf215546Sopenharmony_ci
1147bf215546Sopenharmony_ci
1148bf215546Sopenharmony_ci/**
1149bf215546Sopenharmony_ci * Update the default texture objects in the given context to reference those
1150bf215546Sopenharmony_ci * specified in the shared state and release those referencing the old
1151bf215546Sopenharmony_ci * shared state.
1152bf215546Sopenharmony_ci */
1153bf215546Sopenharmony_civoid
1154bf215546Sopenharmony_ci_mesa_update_default_objects_texture(struct gl_context *ctx)
1155bf215546Sopenharmony_ci{
1156bf215546Sopenharmony_ci   GLuint u, tex;
1157bf215546Sopenharmony_ci
1158bf215546Sopenharmony_ci   for (u = 0; u < ARRAY_SIZE(ctx->Texture.Unit); u++) {
1159bf215546Sopenharmony_ci      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
1160bf215546Sopenharmony_ci      for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
1161bf215546Sopenharmony_ci         _mesa_reference_texobj(&texUnit->CurrentTex[tex],
1162bf215546Sopenharmony_ci                                ctx->Shared->DefaultTex[tex]);
1163bf215546Sopenharmony_ci      }
1164bf215546Sopenharmony_ci   }
1165bf215546Sopenharmony_ci}
1166