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