ff0a0535f118216ab1350c66f5011d90f1a64b4b
[mesa.git] / src / mesa / main / texobj.c
1 /**
2 * \file texobj.c
3 * Texture object management.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 5.1
9 *
10 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "hash.h"
36 #include "imports.h"
37 #include "macros.h"
38 #include "teximage.h"
39 #include "texstate.h"
40 #include "texobj.h"
41 #include "mtypes.h"
42
43
44 /**********************************************************************/
45 /** \name Internal functions */
46 /*@{*/
47
48 /**
49 * Allocate and initialize a new texture object. But don't put it into the
50 * texture object hash table.
51 *
52 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
53 * driver.
54 *
55 * \param shared the shared GL state structure to contain the texture object
56 * \param name integer name for the texture object
57 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
58 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
59 * of GenTextures()
60 *
61 * \return pointer to new texture object.
62 *
63 * Allocate and initialize a gl_texture_object structure, and insert in the
64 * shared state texture list while holding its mutex.
65 * If <tt>name > 0</tt> then also insert the new texture object into the hash
66 * table.
67 *
68 */
69 struct gl_texture_object *
70 _mesa_new_texture_object( GLcontext *ctx, GLuint name, GLenum target )
71 {
72 struct gl_texture_object *obj;
73 obj = CALLOC_STRUCT(gl_texture_object);
74 _mesa_initialize_texture_object(obj, name, target);
75 return obj;
76 }
77
78
79 /**
80 * Initialize a texture object to default values.
81 * \param obj the texture object
82 * \param name the texture name
83 * \param target the texture target
84 */
85 void
86 _mesa_initialize_texture_object( struct gl_texture_object *obj,
87 GLuint name, GLenum target )
88 {
89 ASSERT(target == 0 ||
90 target == GL_TEXTURE_1D ||
91 target == GL_TEXTURE_2D ||
92 target == GL_TEXTURE_3D ||
93 target == GL_TEXTURE_CUBE_MAP_ARB ||
94 target == GL_TEXTURE_RECTANGLE_NV);
95
96 /* init the non-zero fields */
97 _glthread_INIT_MUTEX(obj->Mutex);
98 obj->RefCount = 1;
99 obj->Name = name;
100 obj->Target = target;
101 obj->Priority = 1.0F;
102 if (target == GL_TEXTURE_RECTANGLE_NV) {
103 obj->WrapS = GL_CLAMP_TO_EDGE;
104 obj->WrapT = GL_CLAMP_TO_EDGE;
105 obj->WrapR = GL_CLAMP_TO_EDGE;
106 obj->MinFilter = GL_LINEAR;
107 }
108 else {
109 obj->WrapS = GL_REPEAT;
110 obj->WrapT = GL_REPEAT;
111 obj->WrapR = GL_REPEAT;
112 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
113 }
114 obj->MagFilter = GL_LINEAR;
115 obj->MinLod = -1000.0;
116 obj->MaxLod = 1000.0;
117 obj->BaseLevel = 0;
118 obj->MaxLevel = 1000;
119 obj->MaxAnisotropy = 1.0;
120 obj->CompareFlag = GL_FALSE; /* SGIX_shadow */
121 obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX; /* SGIX_shadow */
122 obj->CompareMode = GL_NONE; /* ARB_shadow */
123 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
124 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
125 obj->ShadowAmbient = 0.0F; /* ARB/SGIX_shadow_ambient */
126 _mesa_init_colortable(&obj->Palette);
127 }
128
129
130 /**
131 * Deallocate a texture object struct. It should have already been
132 * removed from the texture object pool.
133 *
134 * \param shared the shared GL state to which the object belongs.
135 * \param texOjb the texture object to delete.
136 *
137 * Unlink the texture object from the shared state texture linked list while
138 * holding its lock. If the texture is a name number it's also removed from the
139 * hash table. Finally frees the texture images and the object itself.
140 */
141 void
142 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
143 {
144 GLuint i;
145
146 (void) ctx;
147
148 assert(texObj);
149
150 _mesa_free_colortable_data(&texObj->Palette);
151
152 /* free the texture images */
153 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
154 if (texObj->Image[i]) {
155 _mesa_delete_texture_image( texObj->Image[i] );
156 }
157 }
158
159 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
160 _glthread_DESTROY_MUTEX(texObj->Mutex);
161
162 /* free this object */
163 _mesa_free(texObj);
164 }
165
166
167 /**
168 * Add the given texture object to the texture object pool.
169 */
170 void
171 _mesa_save_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
172 {
173 /* insert into linked list */
174 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
175 texObj->Next = ctx->Shared->TexObjectList;
176 ctx->Shared->TexObjectList = texObj;
177 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
178
179 if (texObj->Name > 0) {
180 /* insert into hash table */
181 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
182 }
183 }
184
185
186 /**
187 * Remove the given texture object from the texture object pool.
188 * Do not deallocate the texture object though.
189 */
190 void
191 _mesa_remove_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
192 {
193 struct gl_texture_object *tprev, *tcurr;
194
195 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
196
197 /* unlink from the linked list */
198 tprev = NULL;
199 tcurr = ctx->Shared->TexObjectList;
200 while (tcurr) {
201 if (tcurr == texObj) {
202 if (tprev) {
203 tprev->Next = texObj->Next;
204 }
205 else {
206 ctx->Shared->TexObjectList = texObj->Next;
207 }
208 break;
209 }
210 tprev = tcurr;
211 tcurr = tcurr->Next;
212 }
213
214 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
215
216 if (texObj->Name > 0) {
217 /* remove from hash table */
218 _mesa_HashRemove(ctx->Shared->TexObjects, texObj->Name);
219 }
220 }
221
222 /**
223 * Copy texture object state from one texture object to another.
224 *
225 * \param dest destination texture object.
226 * \param src source texture object.
227 */
228 void
229 _mesa_copy_texture_object( struct gl_texture_object *dest,
230 const struct gl_texture_object *src )
231 {
232 dest->Name = src->Name;
233 dest->Priority = src->Priority;
234 dest->BorderColor[0] = src->BorderColor[0];
235 dest->BorderColor[1] = src->BorderColor[1];
236 dest->BorderColor[2] = src->BorderColor[2];
237 dest->BorderColor[3] = src->BorderColor[3];
238 dest->WrapS = src->WrapS;
239 dest->WrapT = src->WrapT;
240 dest->WrapR = src->WrapR;
241 dest->MinFilter = src->MinFilter;
242 dest->MagFilter = src->MagFilter;
243 dest->MinLod = src->MinLod;
244 dest->MaxLod = src->MaxLod;
245 dest->BaseLevel = src->BaseLevel;
246 dest->MaxLevel = src->MaxLevel;
247 dest->MaxAnisotropy = src->MaxAnisotropy;
248 dest->CompareFlag = src->CompareFlag;
249 dest->CompareOperator = src->CompareOperator;
250 dest->ShadowAmbient = src->ShadowAmbient;
251 dest->CompareMode = src->CompareMode;
252 dest->CompareFunc = src->CompareFunc;
253 dest->DepthMode = src->DepthMode;
254 dest->_MaxLevel = src->_MaxLevel;
255 dest->_MaxLambda = src->_MaxLambda;
256 dest->GenerateMipmap = src->GenerateMipmap;
257 dest->Palette = src->Palette;
258 dest->Complete = src->Complete;
259 dest->_IsPowerOfTwo = src->_IsPowerOfTwo;
260 }
261
262
263 /**
264 * Report why a texture object is incomplete.
265 *
266 * \param t texture object.
267 * \param why string describing why it's incomplete.
268 *
269 * \note For debug purposes only.
270 */
271 #if 0
272 static void
273 incomplete(const struct gl_texture_object *t, const char *why)
274 {
275 _mesa_printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
276 }
277 #else
278 #define incomplete(t, why)
279 #endif
280
281
282 /**
283 * Examine a texture object to determine if it is complete.
284 *
285 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
286 * accordingly.
287 *
288 * \param ctx GL context.
289 * \param t texture object.
290 *
291 * According to the texture target, verifies that each of the mipmaps is
292 * present and has the expected size.
293 */
294 void
295 _mesa_test_texobj_completeness( const GLcontext *ctx,
296 struct gl_texture_object *t )
297 {
298 const GLint baseLevel = t->BaseLevel;
299 GLint maxLog2 = 0, maxLevels = 0;
300
301 t->Complete = GL_TRUE; /* be optimistic */
302 t->_IsPowerOfTwo = GL_TRUE; /* may be set FALSE below */
303
304 /* Always need the base level image */
305 if (!t->Image[baseLevel]) {
306 incomplete(t, "Image[baseLevel] == NULL");
307 t->Complete = GL_FALSE;
308 return;
309 }
310
311 /* Compute _MaxLevel */
312 if (t->Target == GL_TEXTURE_1D) {
313 maxLog2 = t->Image[baseLevel]->WidthLog2;
314 maxLevels = ctx->Const.MaxTextureLevels;
315 }
316 else if (t->Target == GL_TEXTURE_2D) {
317 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
318 t->Image[baseLevel]->HeightLog2);
319 maxLevels = ctx->Const.MaxTextureLevels;
320 }
321 else if (t->Target == GL_TEXTURE_3D) {
322 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
323 t->Image[baseLevel]->HeightLog2);
324 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
325 maxLevels = ctx->Const.Max3DTextureLevels;
326 }
327 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
328 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
329 t->Image[baseLevel]->HeightLog2);
330 maxLevels = ctx->Const.MaxCubeTextureLevels;
331 }
332 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
333 maxLog2 = 0; /* not applicable */
334 maxLevels = 1; /* no mipmapping */
335 }
336 else {
337 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
338 return;
339 }
340
341 ASSERT(maxLevels > 0);
342
343 t->_MaxLevel = baseLevel + maxLog2;
344 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
345 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
346
347 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
348 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
349
350 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
351 /* make sure that all six cube map level 0 images are the same size */
352 const GLuint w = t->Image[baseLevel]->Width2;
353 const GLuint h = t->Image[baseLevel]->Height2;
354 if (!t->NegX[baseLevel] ||
355 t->NegX[baseLevel]->Width2 != w ||
356 t->NegX[baseLevel]->Height2 != h ||
357 !t->PosY[baseLevel] ||
358 t->PosY[baseLevel]->Width2 != w ||
359 t->PosY[baseLevel]->Height2 != h ||
360 !t->NegY[baseLevel] ||
361 t->NegY[baseLevel]->Width2 != w ||
362 t->NegY[baseLevel]->Height2 != h ||
363 !t->PosZ[baseLevel] ||
364 t->PosZ[baseLevel]->Width2 != w ||
365 t->PosZ[baseLevel]->Height2 != h ||
366 !t->NegZ[baseLevel] ||
367 t->NegZ[baseLevel]->Width2 != w ||
368 t->NegZ[baseLevel]->Height2 != h) {
369 t->Complete = GL_FALSE;
370 incomplete(t, "Non-quare cubemap image");
371 return;
372 }
373 }
374
375 /* check for non power of two */
376 if (!t->Image[baseLevel]->_IsPowerOfTwo) {
377 t->_IsPowerOfTwo = GL_FALSE;
378 }
379
380 /* extra checking for mipmaps */
381 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
382 /*
383 * Mipmapping: determine if we have a complete set of mipmaps
384 */
385 GLint i;
386 GLint minLevel = baseLevel;
387 GLint maxLevel = t->_MaxLevel;
388
389 if (minLevel > maxLevel) {
390 t->Complete = GL_FALSE;
391 incomplete(t, "minLevel > maxLevel");
392 return;
393 }
394
395 /* Test dimension-independent attributes */
396 for (i = minLevel; i <= maxLevel; i++) {
397 if (t->Image[i]) {
398 if (t->Image[i]->TexFormat != t->Image[baseLevel]->TexFormat) {
399 t->Complete = GL_FALSE;
400 incomplete(t, "Format[i] != Format[baseLevel]");
401 return;
402 }
403 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
404 t->Complete = GL_FALSE;
405 incomplete(t, "Border[i] != Border[baseLevel]");
406 return;
407 }
408 }
409 }
410
411 /* Test things which depend on number of texture image dimensions */
412 if (t->Target == GL_TEXTURE_1D) {
413 /* Test 1-D mipmaps */
414 GLuint width = t->Image[baseLevel]->Width2;
415 for (i = baseLevel + 1; i < maxLevels; i++) {
416 if (width > 1) {
417 width /= 2;
418 }
419 if (i >= minLevel && i <= maxLevel) {
420 if (!t->Image[i]) {
421 t->Complete = GL_FALSE;
422 incomplete(t, "1D Image[i] == NULL");
423 return;
424 }
425 if (t->Image[i]->Width2 != width ) {
426 t->Complete = GL_FALSE;
427 incomplete(t, "1D Image[i] bad width");
428 return;
429 }
430 }
431 if (width == 1) {
432 return; /* found smallest needed mipmap, all done! */
433 }
434 }
435 }
436 else if (t->Target == GL_TEXTURE_2D) {
437 /* Test 2-D mipmaps */
438 GLuint width = t->Image[baseLevel]->Width2;
439 GLuint height = t->Image[baseLevel]->Height2;
440 for (i = baseLevel + 1; i < maxLevels; i++) {
441 if (width > 1) {
442 width /= 2;
443 }
444 if (height > 1) {
445 height /= 2;
446 }
447 if (i >= minLevel && i <= maxLevel) {
448 if (!t->Image[i]) {
449 t->Complete = GL_FALSE;
450 incomplete(t, "2D Image[i] == NULL");
451 return;
452 }
453 if (t->Image[i]->Width2 != width) {
454 t->Complete = GL_FALSE;
455 incomplete(t, "2D Image[i] bad width");
456 return;
457 }
458 if (t->Image[i]->Height2 != height) {
459 t->Complete = GL_FALSE;
460 incomplete(t, "2D Image[i] bad height");
461 return;
462 }
463 if (width==1 && height==1) {
464 return; /* found smallest needed mipmap, all done! */
465 }
466 }
467 }
468 }
469 else if (t->Target == GL_TEXTURE_3D) {
470 /* Test 3-D mipmaps */
471 GLuint width = t->Image[baseLevel]->Width2;
472 GLuint height = t->Image[baseLevel]->Height2;
473 GLuint depth = t->Image[baseLevel]->Depth2;
474 for (i = baseLevel + 1; i < maxLevels; i++) {
475 if (width > 1) {
476 width /= 2;
477 }
478 if (height > 1) {
479 height /= 2;
480 }
481 if (depth > 1) {
482 depth /= 2;
483 }
484 if (i >= minLevel && i <= maxLevel) {
485 if (!t->Image[i]) {
486 incomplete(t, "3D Image[i] == NULL");
487 t->Complete = GL_FALSE;
488 return;
489 }
490 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
491 t->Complete = GL_FALSE;
492 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
493 return;
494 }
495 if (t->Image[i]->Width2 != width) {
496 t->Complete = GL_FALSE;
497 incomplete(t, "3D Image[i] bad width");
498 return;
499 }
500 if (t->Image[i]->Height2 != height) {
501 t->Complete = GL_FALSE;
502 incomplete(t, "3D Image[i] bad height");
503 return;
504 }
505 if (t->Image[i]->Depth2 != depth) {
506 t->Complete = GL_FALSE;
507 incomplete(t, "3D Image[i] bad depth");
508 return;
509 }
510 }
511 if (width == 1 && height == 1 && depth == 1) {
512 return; /* found smallest needed mipmap, all done! */
513 }
514 }
515 }
516 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
517 /* make sure 6 cube faces are consistant */
518 GLuint width = t->Image[baseLevel]->Width2;
519 GLuint height = t->Image[baseLevel]->Height2;
520 for (i = baseLevel + 1; i < maxLevels; i++) {
521 if (width > 1) {
522 width /= 2;
523 }
524 if (height > 1) {
525 height /= 2;
526 }
527 if (i >= minLevel && i <= maxLevel) {
528 /* check that we have images defined */
529 if (!t->Image[i] || !t->NegX[i] ||
530 !t->PosY[i] || !t->NegY[i] ||
531 !t->PosZ[i] || !t->NegZ[i]) {
532 t->Complete = GL_FALSE;
533 incomplete(t, "CubeMap Image[i] == NULL");
534 return;
535 }
536 /* Don't support GL_DEPTH_COMPONENT for cube maps */
537 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
538 t->Complete = GL_FALSE;
539 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
540 return;
541 }
542 /* check that all six images have same size */
543 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
544 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
545 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
546 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
547 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
548 t->Complete = GL_FALSE;
549 incomplete(t, "CubeMap Image[i] bad size");
550 return;
551 }
552 }
553 if (width == 1 && height == 1) {
554 return; /* found smallest needed mipmap, all done! */
555 }
556 }
557 }
558 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
559 /* XXX special checking? */
560
561 }
562 else {
563 /* Target = ??? */
564 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
565 }
566 }
567 }
568
569 /*@}*/
570
571
572 /***********************************************************************/
573 /** \name API functions */
574 /*@{*/
575
576 /**
577 * Texture name generation lock.
578 *
579 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
580 * of texture IDs is atomic.
581 */
582 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
583
584 /**
585 * Generate texture names.
586 *
587 * \param n number of texture names to be generated.
588 * \param texName an array in which will hold the generated texture names.
589 *
590 * \sa glGenTextures().
591 *
592 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
593 * to find a block of free texture IDs which are stored in \p texName.
594 * Corresponding empty texture objects are also generated.
595 */
596 void
597 _mesa_GenTextures( GLsizei n, GLuint *texName )
598 {
599 GET_CURRENT_CONTEXT(ctx);
600 GLuint first;
601 GLint i;
602 ASSERT_OUTSIDE_BEGIN_END(ctx);
603
604 if (n < 0) {
605 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
606 return;
607 }
608
609 if (!texName)
610 return;
611
612 /*
613 * This must be atomic (generation and allocation of texture IDs)
614 */
615 _glthread_LOCK_MUTEX(GenTexturesLock);
616
617 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
618
619 /* Allocate new, empty texture objects */
620 for (i = 0; i < n; i++) {
621 struct gl_texture_object *texObj;
622 GLuint name = first + i;
623 GLenum target = 0;
624 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
625 if (!texObj) {
626 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
627 return;
628 }
629 _mesa_save_texture_object(ctx, texObj);
630 texName[i] = name;
631 }
632
633 _glthread_UNLOCK_MUTEX(GenTexturesLock);
634 }
635
636 /**
637 * Delete named textures.
638 *
639 * \param n number of textures to be deleted.
640 * \param texName array of textures names to be deleted.
641 *
642 * \sa glDeleteTextures().
643 *
644 * For each texture checks if its bound to any of the texture units, unbinding
645 * it and decrementing the reference count if so. If the texture reference
646 * count is zero, delete its object.
647 */
648 void
649 _mesa_DeleteTextures( GLsizei n, const GLuint *texName)
650 {
651 GET_CURRENT_CONTEXT(ctx);
652 GLint i;
653 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
654
655 if (!texName)
656 return;
657
658 for (i=0;i<n;i++) {
659 if (texName[i] > 0) {
660 struct gl_texture_object *delObj = (struct gl_texture_object *)
661 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
662 if (delObj) {
663 /* First check if this texture is currently bound.
664 * If so, unbind it and decrement the reference count.
665 */
666 GLuint u;
667 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
668 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
669 if (delObj == unit->Current1D) {
670 unit->Current1D = ctx->Shared->Default1D;
671 ctx->Shared->Default1D->RefCount++;
672 delObj->RefCount--;
673 if (delObj == unit->_Current)
674 unit->_Current = unit->Current1D;
675 }
676 else if (delObj == unit->Current2D) {
677 unit->Current2D = ctx->Shared->Default2D;
678 ctx->Shared->Default2D->RefCount++;
679 delObj->RefCount--;
680 if (delObj == unit->_Current)
681 unit->_Current = unit->Current2D;
682 }
683 else if (delObj == unit->Current3D) {
684 unit->Current3D = ctx->Shared->Default3D;
685 ctx->Shared->Default3D->RefCount++;
686 delObj->RefCount--;
687 if (delObj == unit->_Current)
688 unit->_Current = unit->Current3D;
689 }
690 else if (delObj == unit->CurrentCubeMap) {
691 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
692 ctx->Shared->DefaultCubeMap->RefCount++;
693 delObj->RefCount--;
694 if (delObj == unit->_Current)
695 unit->_Current = unit->CurrentCubeMap;
696 }
697 else if (delObj == unit->CurrentRect) {
698 unit->CurrentRect = ctx->Shared->DefaultRect;
699 ctx->Shared->DefaultRect->RefCount++;
700 delObj->RefCount--;
701 if (delObj == unit->_Current)
702 unit->_Current = unit->CurrentRect;
703 }
704 }
705 ctx->NewState |= _NEW_TEXTURE;
706
707 /* Decrement reference count and delete if zero */
708 delObj->RefCount--;
709 ASSERT(delObj->RefCount >= 0);
710
711 if (delObj->RefCount == 0) {
712 ASSERT(delObj->Name != 0);
713 _mesa_remove_texture_object(ctx, delObj);
714 ASSERT(ctx->Driver.DeleteTexture);
715 (*ctx->Driver.DeleteTexture)(ctx, delObj);
716 }
717 }
718 }
719 }
720 }
721
722 /**
723 * Bind a named texture to a texturing target.
724 *
725 * \param target texture target.
726 * \param texName texture name.
727 *
728 * \sa glBindTexture().
729 *
730 * Determines the old texture object bound and returns immediately if rebinding
731 * the same texture. Get the current texture which is either a default texture
732 * if name is null, a named texture from the hash, or a new texture if the
733 * given texture name is new. Increments its reference count, binds it, and
734 * calls dd_function_table::BindTexture. Decrements the old texture reference
735 * count and deletes it if it reaches zero.
736 */
737 void
738 _mesa_BindTexture( GLenum target, GLuint texName )
739 {
740 GET_CURRENT_CONTEXT(ctx);
741 GLuint unit = ctx->Texture.CurrentUnit;
742 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
743 struct gl_texture_object *oldTexObj;
744 struct gl_texture_object *newTexObj = 0;
745 ASSERT_OUTSIDE_BEGIN_END(ctx);
746
747 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
748 _mesa_debug(ctx, "glBindTexture %s %d\n",
749 _mesa_lookup_enum_by_nr(target), (GLint) texName);
750
751 switch (target) {
752 case GL_TEXTURE_1D:
753 oldTexObj = texUnit->Current1D;
754 break;
755 case GL_TEXTURE_2D:
756 oldTexObj = texUnit->Current2D;
757 break;
758 case GL_TEXTURE_3D:
759 oldTexObj = texUnit->Current3D;
760 break;
761 case GL_TEXTURE_CUBE_MAP_ARB:
762 if (!ctx->Extensions.ARB_texture_cube_map) {
763 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
764 return;
765 }
766 oldTexObj = texUnit->CurrentCubeMap;
767 break;
768 case GL_TEXTURE_RECTANGLE_NV:
769 if (!ctx->Extensions.NV_texture_rectangle) {
770 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
771 return;
772 }
773 oldTexObj = texUnit->CurrentRect;
774 break;
775 default:
776 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
777 return;
778 }
779
780 if (oldTexObj->Name == texName)
781 return; /* rebinding the same texture- no change */
782
783 /*
784 * Get pointer to new texture object (newTexObj)
785 */
786 if (texName == 0) {
787 /* newTexObj = a default texture object */
788 switch (target) {
789 case GL_TEXTURE_1D:
790 newTexObj = ctx->Shared->Default1D;
791 break;
792 case GL_TEXTURE_2D:
793 newTexObj = ctx->Shared->Default2D;
794 break;
795 case GL_TEXTURE_3D:
796 newTexObj = ctx->Shared->Default3D;
797 break;
798 case GL_TEXTURE_CUBE_MAP_ARB:
799 newTexObj = ctx->Shared->DefaultCubeMap;
800 break;
801 case GL_TEXTURE_RECTANGLE_NV:
802 newTexObj = ctx->Shared->DefaultRect;
803 break;
804 default:
805 ; /* Bad targets are caught above */
806 }
807 }
808 else {
809 /* non-default texture object */
810 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
811 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
812 if (newTexObj) {
813 /* error checking */
814 if (newTexObj->Target != 0 && newTexObj->Target != target) {
815 /* the named texture object's dimensions don't match the target */
816 _mesa_error( ctx, GL_INVALID_OPERATION,
817 "glBindTexture(wrong dimensionality)" );
818 return;
819 }
820 if (newTexObj->Target == 0 && target == GL_TEXTURE_RECTANGLE_NV) {
821 /* have to init wrap and filter state here - kind of klunky */
822 newTexObj->WrapS = GL_CLAMP_TO_EDGE;
823 newTexObj->WrapT = GL_CLAMP_TO_EDGE;
824 newTexObj->WrapR = GL_CLAMP_TO_EDGE;
825 newTexObj->MinFilter = GL_LINEAR;
826 }
827 }
828 else {
829 /* if this is a new texture id, allocate a texture object now */
830 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
831 if (!newTexObj) {
832 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
833 return;
834 }
835 _mesa_save_texture_object(ctx, newTexObj);
836 }
837 newTexObj->Target = target;
838 }
839
840 newTexObj->RefCount++;
841
842 /* do the actual binding, but first flush outstanding vertices:
843 */
844 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
845
846 switch (target) {
847 case GL_TEXTURE_1D:
848 texUnit->Current1D = newTexObj;
849 break;
850 case GL_TEXTURE_2D:
851 texUnit->Current2D = newTexObj;
852 break;
853 case GL_TEXTURE_3D:
854 texUnit->Current3D = newTexObj;
855 break;
856 case GL_TEXTURE_CUBE_MAP_ARB:
857 texUnit->CurrentCubeMap = newTexObj;
858 break;
859 case GL_TEXTURE_RECTANGLE_NV:
860 texUnit->CurrentRect = newTexObj;
861 break;
862 default:
863 _mesa_problem(ctx, "bad target in BindTexture");
864 return;
865 }
866
867 /* Pass BindTexture call to device driver */
868 if (ctx->Driver.BindTexture)
869 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
870
871 oldTexObj->RefCount--;
872 assert(oldTexObj->RefCount >= 0);
873 if (oldTexObj->RefCount == 0) {
874 assert(oldTexObj->Name != 0);
875 _mesa_remove_texture_object(ctx, oldTexObj);
876 ASSERT(ctx->Driver.DeleteTexture);
877 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
878 }
879 }
880
881 /**
882 * Set texture priorities.
883 *
884 * \param n number of textures.
885 * \param texName texture names.
886 * \param priorities corresponding texture priorities.
887 *
888 * \sa glPrioritizeTextures().
889 *
890 * Looks up each texture in the hash, clamps the corresponding priority between
891 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
892 */
893 void
894 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
895 const GLclampf *priorities )
896 {
897 GET_CURRENT_CONTEXT(ctx);
898 GLint i;
899 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
900
901 if (n < 0) {
902 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
903 return;
904 }
905
906 if (!priorities)
907 return;
908
909 for (i = 0; i < n; i++) {
910 if (texName[i] > 0) {
911 struct gl_texture_object *t = (struct gl_texture_object *)
912 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
913 if (t) {
914 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
915 if (ctx->Driver.PrioritizeTexture)
916 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
917 }
918 }
919 }
920
921 ctx->NewState |= _NEW_TEXTURE;
922 }
923
924 /**
925 * See if textures are loaded in texture memory.
926 *
927 * \param n number of textures to query.
928 * \param texName array with the texture names.
929 * \param residences array which will hold the residence status.
930 *
931 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
932 *
933 * \sa glAreTexturesResident().
934 *
935 * Looks up each texture in the hash and calls
936 * dd_function_table::IsTextureResident.
937 */
938 GLboolean
939 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
940 GLboolean *residences)
941 {
942 GET_CURRENT_CONTEXT(ctx);
943 GLboolean allResident = GL_TRUE;
944 GLint i, j;
945 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
946
947 if (n < 0) {
948 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
949 return GL_FALSE;
950 }
951
952 if (!texName || !residences)
953 return GL_FALSE;
954
955 for (i = 0; i < n; i++) {
956 struct gl_texture_object *t;
957 if (texName[i] == 0) {
958 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
959 return GL_FALSE;
960 }
961 t = (struct gl_texture_object *)
962 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
963 if (!t) {
964 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
965 return GL_FALSE;
966 }
967 if (!ctx->Driver.IsTextureResident ||
968 ctx->Driver.IsTextureResident(ctx, t)) {
969 /* The texture is resident */
970 if (!allResident)
971 residences[i] = GL_TRUE;
972 }
973 else {
974 /* The texture is not resident */
975 if (allResident) {
976 allResident = GL_FALSE;
977 for (j = 0; j < i; j++)
978 residences[j] = GL_TRUE;
979 }
980 residences[i] = GL_FALSE;
981 }
982 }
983
984 return allResident;
985 }
986
987 /**
988 * See if a name corresponds to a texture.
989 *
990 * \param texture texture name.
991 *
992 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
993 * otherwise.
994 *
995 * \sa glIsTexture().
996 *
997 * Calls _mesa_HashLookup().
998 */
999 GLboolean
1000 _mesa_IsTexture( GLuint texture )
1001 {
1002 GET_CURRENT_CONTEXT(ctx);
1003 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1004 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
1005 }
1006
1007 /*@}*/