[MSHTML_WINETEST]
[reactos.git] / reactos / dll / opengl / mesa / main / texstate.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file texstate.c
27 *
28 * Texture state handling.
29 */
30
31 #include "glheader.h"
32 #include "mfeatures.h"
33 #include "bufferobj.h"
34 #include "colormac.h"
35 #include "colortab.h"
36 #include "context.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "texobj.h"
40 #include "teximage.h"
41 #include "texstate.h"
42 #include "mtypes.h"
43
44
45
46 /**
47 * Default texture combine environment state. This is used to initialize
48 * a context's texture units and as the basis for converting "classic"
49 * texture environmnets to ARB_texture_env_combine style values.
50 */
51 static const struct gl_tex_env_combine_state default_combine_state = {
52 GL_MODULATE, GL_MODULATE,
53 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
54 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
55 { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA, GL_SRC_ALPHA },
56 { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
57 0, 0,
58 2, 2
59 };
60
61
62
63 /**
64 * Used by glXCopyContext to copy texture state from one context to another.
65 */
66 void
67 _mesa_copy_texture_state( const struct gl_context *src, struct gl_context *dst )
68 {
69 GLuint tex;
70
71 ASSERT(src);
72 ASSERT(dst);
73
74 dst->Texture._GenFlags = src->Texture._GenFlags;
75 dst->Texture._TexGenEnabled = src->Texture._TexGenEnabled;
76 dst->Texture._TexMatEnabled = src->Texture._TexMatEnabled;
77
78 dst->Texture.Unit.Enabled = src->Texture.Unit.Enabled;
79 dst->Texture.Unit.EnvMode = src->Texture.Unit.EnvMode;
80 COPY_4V(dst->Texture.Unit.EnvColor, src->Texture.Unit.EnvColor);
81 dst->Texture.Unit.TexGenEnabled = src->Texture.Unit.TexGenEnabled;
82 dst->Texture.Unit.GenS = src->Texture.Unit.GenS;
83 dst->Texture.Unit.GenT = src->Texture.Unit.GenT;
84 dst->Texture.Unit.GenR = src->Texture.Unit.GenR;
85 dst->Texture.Unit.GenQ = src->Texture.Unit.GenQ;
86 dst->Texture.Unit.LodBias = src->Texture.Unit.LodBias;
87
88 /* GL_EXT_texture_env_combine */
89 dst->Texture.Unit.Combine = src->Texture.Unit.Combine;
90
91 /*
92 * XXX strictly speaking, we should compare texture names/ids and
93 * bind textures in the dest context according to id. For now, only
94 * copy bindings if the contexts share the same pool of textures to
95 * avoid refcounting bugs.
96 */
97 if (dst->Shared == src->Shared) {
98 /* copy texture object bindings, not contents of texture objects */
99 _mesa_lock_context_textures(dst);
100
101 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
102 _mesa_reference_texobj(&dst->Texture.Unit.CurrentTex[tex],
103 src->Texture.Unit.CurrentTex[tex]);
104 }
105 _mesa_unlock_context_textures(dst);
106 }
107 }
108
109
110 /*
111 * For debugging
112 */
113 void
114 _mesa_print_texunit_state( struct gl_context *ctx )
115 {
116 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit;
117 printf("Texture Unit\n");
118 printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit->EnvMode));
119 printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeRGB));
120 printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeA));
121 printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[0]));
122 printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[1]));
123 printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[2]));
124 printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[0]));
125 printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[1]));
126 printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[2]));
127 printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[0]));
128 printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[1]));
129 printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[2]));
130 printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[0]));
131 printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[1]));
132 printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[2]));
133 printf(" GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
134 printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
135 printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
136 }
137
138
139
140 /**********************************************************************/
141 /* Texture Environment */
142 /**********************************************************************/
143
144 /**
145 * Convert "classic" texture environment to ARB_texture_env_combine style
146 * environments.
147 *
148 * \param state texture_env_combine state vector to be filled-in.
149 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
150 * \c GL_BLEND, \c GL_DECAL, etc.).
151 * \param texBaseFormat Base format of the texture associated with the
152 * texture unit.
153 */
154 static void
155 calculate_derived_texenv( struct gl_tex_env_combine_state *state,
156 GLenum mode, GLenum texBaseFormat )
157 {
158 GLenum mode_rgb;
159 GLenum mode_a;
160
161 *state = default_combine_state;
162
163 switch (texBaseFormat) {
164 case GL_ALPHA:
165 state->SourceRGB[0] = GL_PREVIOUS;
166 break;
167
168 case GL_LUMINANCE_ALPHA:
169 case GL_INTENSITY:
170 case GL_RGBA:
171 break;
172
173 case GL_LUMINANCE:
174 case GL_RED:
175 case GL_RG:
176 case GL_RGB:
177 case GL_YCBCR_MESA:
178 state->SourceA[0] = GL_PREVIOUS;
179 break;
180
181 default:
182 _mesa_problem(NULL,
183 "Invalid texBaseFormat 0x%x in calculate_derived_texenv",
184 texBaseFormat);
185 return;
186 }
187
188 if (mode == GL_REPLACE_EXT)
189 mode = GL_REPLACE;
190
191 switch (mode) {
192 case GL_REPLACE:
193 case GL_MODULATE:
194 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
195 mode_a = mode;
196 break;
197
198 case GL_DECAL:
199 mode_rgb = GL_INTERPOLATE;
200 mode_a = GL_REPLACE;
201
202 state->SourceA[0] = GL_PREVIOUS;
203
204 /* Having alpha / luminance / intensity textures replace using the
205 * incoming fragment color matches the definition in NV_texture_shader.
206 * The 1.5 spec simply marks these as "undefined".
207 */
208 switch (texBaseFormat) {
209 case GL_ALPHA:
210 case GL_LUMINANCE:
211 case GL_LUMINANCE_ALPHA:
212 case GL_INTENSITY:
213 state->SourceRGB[0] = GL_PREVIOUS;
214 break;
215 case GL_RED:
216 case GL_RG:
217 case GL_RGB:
218 case GL_YCBCR_MESA:
219 mode_rgb = GL_REPLACE;
220 break;
221 case GL_RGBA:
222 state->SourceRGB[2] = GL_TEXTURE;
223 break;
224 }
225 break;
226
227 case GL_BLEND:
228 mode_rgb = GL_INTERPOLATE;
229 mode_a = GL_MODULATE;
230
231 switch (texBaseFormat) {
232 case GL_ALPHA:
233 mode_rgb = GL_REPLACE;
234 break;
235 case GL_INTENSITY:
236 mode_a = GL_INTERPOLATE;
237 state->SourceA[0] = GL_CONSTANT;
238 state->OperandA[2] = GL_SRC_ALPHA;
239 /* FALLTHROUGH */
240 case GL_LUMINANCE:
241 case GL_RED:
242 case GL_RG:
243 case GL_RGB:
244 case GL_LUMINANCE_ALPHA:
245 case GL_RGBA:
246 case GL_YCBCR_MESA:
247 state->SourceRGB[2] = GL_TEXTURE;
248 state->SourceA[2] = GL_TEXTURE;
249 state->SourceRGB[0] = GL_CONSTANT;
250 state->OperandRGB[2] = GL_SRC_COLOR;
251 break;
252 }
253 break;
254
255 case GL_ADD:
256 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
257 mode_a = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
258 break;
259
260 default:
261 _mesa_problem(NULL,
262 "Invalid texture env mode 0x%x in calculate_derived_texenv",
263 mode);
264 return;
265 }
266
267 state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
268 ? mode_rgb : GL_REPLACE;
269 state->ModeA = (state->SourceA[0] != GL_PREVIOUS)
270 ? mode_a : GL_REPLACE;
271 }
272
273 /**********************************************************************/
274 /***** State management *****/
275 /**********************************************************************/
276
277
278 /**
279 * \note This routine refers to derived texture attribute values to
280 * compute the ENABLE_TEXMAT flags, but is only called on
281 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
282 * flags are updated by _mesa_update_textures(), below.
283 *
284 * \param ctx GL context.
285 */
286 static void
287 update_texture_matrices( struct gl_context *ctx )
288 {
289 ctx->Texture._TexMatEnabled = 0x0;
290
291 if (_math_matrix_is_dirty(ctx->TextureMatrixStack.Top)) {
292 _math_matrix_analyse( ctx->TextureMatrixStack.Top );
293
294 if (ctx->Texture.Unit._ReallyEnabled &&
295 ctx->TextureMatrixStack.Top->type != MATRIX_IDENTITY)
296 ctx->Texture._TexMatEnabled = GL_TRUE;
297 }
298 }
299
300
301 /**
302 * Examine texture unit's combine/env state to update derived state.
303 */
304 static void
305 update_tex_combine(struct gl_context *ctx, struct gl_texture_unit *texUnit)
306 {
307 struct gl_tex_env_combine_state *combine;
308
309 /* Set the texUnit->_CurrentCombine field to point to the user's combiner
310 * state, or the combiner state which is derived from traditional texenv
311 * mode.
312 */
313 if (texUnit->EnvMode == GL_COMBINE ||
314 texUnit->EnvMode == GL_COMBINE4_NV) {
315 texUnit->_CurrentCombine = & texUnit->Combine;
316 }
317 else {
318 const struct gl_texture_object *texObj = texUnit->_Current;
319 GLenum format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
320
321 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
322 texUnit->_CurrentCombine = & texUnit->_EnvMode;
323 }
324
325 combine = texUnit->_CurrentCombine;
326
327 /* Determine number of source RGB terms in the combiner function */
328 switch (combine->ModeRGB) {
329 case GL_REPLACE:
330 combine->_NumArgsRGB = 1;
331 break;
332 case GL_ADD:
333 case GL_ADD_SIGNED:
334 if (texUnit->EnvMode == GL_COMBINE4_NV)
335 combine->_NumArgsRGB = 4;
336 else
337 combine->_NumArgsRGB = 2;
338 break;
339 case GL_MODULATE:
340 case GL_SUBTRACT:
341 case GL_DOT3_RGB:
342 case GL_DOT3_RGBA:
343 case GL_DOT3_RGB_EXT:
344 case GL_DOT3_RGBA_EXT:
345 combine->_NumArgsRGB = 2;
346 break;
347 case GL_INTERPOLATE:
348 case GL_MODULATE_ADD_ATI:
349 case GL_MODULATE_SIGNED_ADD_ATI:
350 case GL_MODULATE_SUBTRACT_ATI:
351 combine->_NumArgsRGB = 3;
352 break;
353 default:
354 combine->_NumArgsRGB = 0;
355 _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
356 return;
357 }
358
359 /* Determine number of source Alpha terms in the combiner function */
360 switch (combine->ModeA) {
361 case GL_REPLACE:
362 combine->_NumArgsA = 1;
363 break;
364 case GL_ADD:
365 case GL_ADD_SIGNED:
366 if (texUnit->EnvMode == GL_COMBINE4_NV)
367 combine->_NumArgsA = 4;
368 else
369 combine->_NumArgsA = 2;
370 break;
371 case GL_MODULATE:
372 case GL_SUBTRACT:
373 combine->_NumArgsA = 2;
374 break;
375 case GL_INTERPOLATE:
376 case GL_MODULATE_ADD_ATI:
377 case GL_MODULATE_SIGNED_ADD_ATI:
378 case GL_MODULATE_SUBTRACT_ATI:
379 combine->_NumArgsA = 3;
380 break;
381 default:
382 combine->_NumArgsA = 0;
383 _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
384 break;
385 }
386 }
387
388
389 /**
390 * \note This routine refers to derived texture matrix values to
391 * compute the ENABLE_TEXMAT flags, but is only called on
392 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
393 * flags are updated by _mesa_update_texture_matrices, above.
394 *
395 * \param ctx GL context.
396 */
397 static void
398 update_texture_state( struct gl_context *ctx )
399 {
400 /* TODO: only set this if there are actual changes */
401 ctx->NewState |= _NEW_TEXTURE;
402
403 ctx->Texture._Enabled = GL_FALSE;
404 ctx->Texture._GenFlags = 0x0;
405 ctx->Texture._TexMatEnabled = GL_FALSE;
406 ctx->Texture._TexGenEnabled = GL_FALSE;
407
408 /*
409 * Update texture unit state.
410 */
411 {
412 struct gl_texture_unit *texUnit = &ctx->Texture.Unit;
413 GLbitfield enabledTargets = texUnit->Enabled;
414
415 texUnit->_ReallyEnabled = 0x0;
416
417 if (enabledTargets) {
418 GLuint texIndex;
419 /* Look for the highest priority texture target that's enabled (or used
420 * by the vert/frag shaders) and "complete". That's the one we'll use
421 * for texturing. If we're using vert/frag program we're guaranteed
422 * that bitcount(enabledBits) <= 1.
423 * Note that the TEXTURE_x_INDEX values are in high to low priority.
424 */
425 for (texIndex = 0; texIndex < NUM_TEXTURE_TARGETS; texIndex++) {
426 if (enabledTargets & (1 << texIndex)) {
427 struct gl_texture_object *texObj = texUnit->CurrentTex[texIndex];
428 if (!texObj->_Complete) {
429 _mesa_test_texobj_completeness(ctx, texObj);
430 }
431 if (texObj->_Complete) {
432 texUnit->_ReallyEnabled = 1 << texIndex;
433 _mesa_reference_texobj(&texUnit->_Current, texObj);
434 break;
435 }
436 }
437 }
438
439 if (texUnit->_ReallyEnabled) {
440 /* if we get here, we know this texture unit is enabled */
441 ctx->Texture._Enabled = GL_TRUE;
442 update_tex_combine(ctx, texUnit);
443 }
444 }
445 }
446
447
448 /* Determine if texture coordinates are actually needed */
449 ctx->Texture._EnabledCoord = ctx->Texture._Enabled;
450
451 /* Setup texgen for those texture coordinate sets that are in use */
452 if(ctx->Texture._EnabledCoord)
453 {
454 struct gl_texture_unit *texUnit = &ctx->Texture.Unit;
455
456 texUnit->_GenFlags = 0x0;
457
458 if (texUnit->TexGenEnabled) {
459 if (texUnit->TexGenEnabled & S_BIT) {
460 texUnit->_GenFlags |= texUnit->GenS._ModeBit;
461 }
462 if (texUnit->TexGenEnabled & T_BIT) {
463 texUnit->_GenFlags |= texUnit->GenT._ModeBit;
464 }
465 if (texUnit->TexGenEnabled & R_BIT) {
466 texUnit->_GenFlags |= texUnit->GenR._ModeBit;
467 }
468 if (texUnit->TexGenEnabled & Q_BIT) {
469 texUnit->_GenFlags |= texUnit->GenQ._ModeBit;
470 }
471
472 ctx->Texture._TexGenEnabled = GL_TRUE;
473 ctx->Texture._GenFlags |= texUnit->_GenFlags;
474 }
475
476 if (ctx->TextureMatrixStack.Top->type != MATRIX_IDENTITY)
477 ctx->Texture._TexMatEnabled = GL_TRUE;
478 }
479 }
480
481
482 /**
483 * Update texture-related derived state.
484 */
485 void
486 _mesa_update_texture( struct gl_context *ctx, GLuint new_state )
487 {
488 if (new_state & _NEW_TEXTURE_MATRIX)
489 update_texture_matrices( ctx );
490
491 if (new_state & _NEW_TEXTURE)
492 update_texture_state( ctx );
493 }
494
495
496 /**********************************************************************/
497 /***** Initialization *****/
498 /**********************************************************************/
499
500 /**
501 * Allocate the proxy textures for the given context.
502 *
503 * \param ctx the context to allocate proxies for.
504 *
505 * \return GL_TRUE on success, or GL_FALSE on failure
506 *
507 * If run out of memory part way through the allocations, clean up and return
508 * GL_FALSE.
509 */
510 static GLboolean
511 alloc_proxy_textures( struct gl_context *ctx )
512 {
513 /* NOTE: these values must be in the same order as the TEXTURE_x_INDEX
514 * values!
515 */
516 static const GLenum targets[] = {
517 GL_TEXTURE_CUBE_MAP_ARB,
518 GL_TEXTURE_3D,
519 GL_TEXTURE_2D,
520 GL_TEXTURE_1D,
521 };
522 GLint tgt;
523
524 STATIC_ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
525 assert(targets[TEXTURE_2D_INDEX] == GL_TEXTURE_2D);
526 assert(targets[TEXTURE_CUBE_INDEX] == GL_TEXTURE_CUBE_MAP);
527
528 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
529 if (!(ctx->Texture.ProxyTex[tgt]
530 = ctx->Driver.NewTextureObject(ctx, 0, targets[tgt]))) {
531 /* out of memory, free what we did allocate */
532 while (--tgt >= 0) {
533 ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
534 }
535 return GL_FALSE;
536 }
537 }
538
539 assert(ctx->Texture.ProxyTex[0]->RefCount == 1); /* sanity check */
540 return GL_TRUE;
541 }
542
543
544 /**
545 * Initialize a texture unit.
546 *
547 * \param ctx GL context.
548 * \param unit texture unit number to be initialized.
549 */
550 static void
551 init_texture_unit( struct gl_context *ctx)
552 {
553 struct gl_texture_unit *texUnit = &ctx->Texture.Unit;
554 GLuint tex;
555
556 texUnit->EnvMode = GL_MODULATE;
557 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
558
559 texUnit->Combine = default_combine_state;
560 texUnit->_EnvMode = default_combine_state;
561 texUnit->_CurrentCombine = & texUnit->_EnvMode;
562
563 texUnit->TexGenEnabled = 0x0;
564 texUnit->GenS.Mode = GL_EYE_LINEAR;
565 texUnit->GenT.Mode = GL_EYE_LINEAR;
566 texUnit->GenR.Mode = GL_EYE_LINEAR;
567 texUnit->GenQ.Mode = GL_EYE_LINEAR;
568 texUnit->GenS._ModeBit = TEXGEN_EYE_LINEAR;
569 texUnit->GenT._ModeBit = TEXGEN_EYE_LINEAR;
570 texUnit->GenR._ModeBit = TEXGEN_EYE_LINEAR;
571 texUnit->GenQ._ModeBit = TEXGEN_EYE_LINEAR;
572
573 /* Yes, these plane coefficients are correct! */
574 ASSIGN_4V( texUnit->GenS.ObjectPlane, 1.0, 0.0, 0.0, 0.0 );
575 ASSIGN_4V( texUnit->GenT.ObjectPlane, 0.0, 1.0, 0.0, 0.0 );
576 ASSIGN_4V( texUnit->GenR.ObjectPlane, 0.0, 0.0, 0.0, 0.0 );
577 ASSIGN_4V( texUnit->GenQ.ObjectPlane, 0.0, 0.0, 0.0, 0.0 );
578 ASSIGN_4V( texUnit->GenS.EyePlane, 1.0, 0.0, 0.0, 0.0 );
579 ASSIGN_4V( texUnit->GenT.EyePlane, 0.0, 1.0, 0.0, 0.0 );
580 ASSIGN_4V( texUnit->GenR.EyePlane, 0.0, 0.0, 0.0, 0.0 );
581 ASSIGN_4V( texUnit->GenQ.EyePlane, 0.0, 0.0, 0.0, 0.0 );
582
583 /* initialize current texture object ptrs to the shared default objects */
584 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
585 _mesa_reference_texobj(&texUnit->CurrentTex[tex],
586 ctx->Shared->DefaultTex[tex]);
587 }
588 }
589
590
591 /**
592 * Initialize texture state for the given context.
593 */
594 GLboolean
595 _mesa_init_texture(struct gl_context *ctx)
596 {
597 /* Texture group */
598 ctx->Texture._Enabled = GL_FALSE;
599
600 init_texture_unit(ctx);
601
602 /* Allocate proxy textures */
603 if (!alloc_proxy_textures( ctx ))
604 return GL_FALSE;
605
606 return GL_TRUE;
607 }
608
609
610 /**
611 * Free dynamically-allocted texture data attached to the given context.
612 */
613 void
614 _mesa_free_texture_data(struct gl_context *ctx)
615 {
616 GLuint tgt;
617
618 /* unreference current textures */
619 /* The _Current texture could account for another reference */
620 _mesa_reference_texobj(&ctx->Texture.Unit._Current, NULL);
621
622 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
623 _mesa_reference_texobj(&ctx->Texture.Unit.CurrentTex[tgt], NULL);
624 }
625
626 /* Free proxy texture objects */
627 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++)
628 ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
629
630 #if FEATURE_sampler_objects
631 _mesa_reference_sampler_object(ctx, &ctx->Texture.Unit.Sampler, NULL);
632 #endif
633 }
634
635
636 /**
637 * Update the default texture objects in the given context to reference those
638 * specified in the shared state and release those referencing the old
639 * shared state.
640 */
641 void
642 _mesa_update_default_objects_texture(struct gl_context *ctx)
643 {
644 GLuint tex;
645 struct gl_texture_unit *texUnit = &ctx->Texture.Unit;
646 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
647 _mesa_reference_texobj(&texUnit->CurrentTex[tex],
648 ctx->Shared->DefaultTex[tex]);
649 }
650 }