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