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