remove duplicate declaration
[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 char s[100];
309 sprintf(s, "obj %p (%d) Image[baseLevel=%d] == NULL",
310 (void *) t, t->Name, baseLevel);
311 incomplete(t, s);
312 t->Complete = GL_FALSE;
313 return;
314 }
315
316 /* Check width/height/depth for zero */
317 if (t->Image[baseLevel]->Width == 0 ||
318 t->Image[baseLevel]->Height == 0 ||
319 t->Image[baseLevel]->Depth == 0) {
320 incomplete(t, "texture width = 0");
321 t->Complete = GL_FALSE;
322 return;
323 }
324
325 /* Compute _MaxLevel */
326 if (t->Target == GL_TEXTURE_1D) {
327 maxLog2 = t->Image[baseLevel]->WidthLog2;
328 maxLevels = ctx->Const.MaxTextureLevels;
329 }
330 else if (t->Target == GL_TEXTURE_2D) {
331 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
332 t->Image[baseLevel]->HeightLog2);
333 maxLevels = ctx->Const.MaxTextureLevels;
334 }
335 else if (t->Target == GL_TEXTURE_3D) {
336 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
337 t->Image[baseLevel]->HeightLog2);
338 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
339 maxLevels = ctx->Const.Max3DTextureLevels;
340 }
341 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
342 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
343 t->Image[baseLevel]->HeightLog2);
344 maxLevels = ctx->Const.MaxCubeTextureLevels;
345 }
346 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
347 maxLog2 = 0; /* not applicable */
348 maxLevels = 1; /* no mipmapping */
349 }
350 else {
351 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
352 return;
353 }
354
355 ASSERT(maxLevels > 0);
356
357 t->_MaxLevel = baseLevel + maxLog2;
358 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
359 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
360
361 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
362 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
363
364 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
365 /* make sure that all six cube map level 0 images are the same size */
366 const GLuint w = t->Image[baseLevel]->Width2;
367 const GLuint h = t->Image[baseLevel]->Height2;
368 if (!t->NegX[baseLevel] ||
369 t->NegX[baseLevel]->Width2 != w ||
370 t->NegX[baseLevel]->Height2 != h ||
371 !t->PosY[baseLevel] ||
372 t->PosY[baseLevel]->Width2 != w ||
373 t->PosY[baseLevel]->Height2 != h ||
374 !t->NegY[baseLevel] ||
375 t->NegY[baseLevel]->Width2 != w ||
376 t->NegY[baseLevel]->Height2 != h ||
377 !t->PosZ[baseLevel] ||
378 t->PosZ[baseLevel]->Width2 != w ||
379 t->PosZ[baseLevel]->Height2 != h ||
380 !t->NegZ[baseLevel] ||
381 t->NegZ[baseLevel]->Width2 != w ||
382 t->NegZ[baseLevel]->Height2 != h) {
383 t->Complete = GL_FALSE;
384 incomplete(t, "Non-quare cubemap image");
385 return;
386 }
387 }
388
389 /* check for non power of two */
390 if (!t->Image[baseLevel]->_IsPowerOfTwo) {
391 t->_IsPowerOfTwo = GL_FALSE;
392 }
393
394 /* extra checking for mipmaps */
395 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
396 /*
397 * Mipmapping: determine if we have a complete set of mipmaps
398 */
399 GLint i;
400 GLint minLevel = baseLevel;
401 GLint maxLevel = t->_MaxLevel;
402
403 if (minLevel > maxLevel) {
404 t->Complete = GL_FALSE;
405 incomplete(t, "minLevel > maxLevel");
406 return;
407 }
408
409 /* Test dimension-independent attributes */
410 for (i = minLevel; i <= maxLevel; i++) {
411 if (t->Image[i]) {
412 if (t->Image[i]->TexFormat != t->Image[baseLevel]->TexFormat) {
413 t->Complete = GL_FALSE;
414 incomplete(t, "Format[i] != Format[baseLevel]");
415 return;
416 }
417 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
418 t->Complete = GL_FALSE;
419 incomplete(t, "Border[i] != Border[baseLevel]");
420 return;
421 }
422 }
423 }
424
425 /* Test things which depend on number of texture image dimensions */
426 if (t->Target == GL_TEXTURE_1D) {
427 /* Test 1-D mipmaps */
428 GLuint width = t->Image[baseLevel]->Width2;
429 for (i = baseLevel + 1; i < maxLevels; i++) {
430 if (width > 1) {
431 width /= 2;
432 }
433 if (i >= minLevel && i <= maxLevel) {
434 if (!t->Image[i]) {
435 t->Complete = GL_FALSE;
436 incomplete(t, "1D Image[i] == NULL");
437 return;
438 }
439 if (t->Image[i]->Width2 != width ) {
440 t->Complete = GL_FALSE;
441 incomplete(t, "1D Image[i] bad width");
442 return;
443 }
444 }
445 if (width == 1) {
446 return; /* found smallest needed mipmap, all done! */
447 }
448 }
449 }
450 else if (t->Target == GL_TEXTURE_2D) {
451 /* Test 2-D mipmaps */
452 GLuint width = t->Image[baseLevel]->Width2;
453 GLuint height = t->Image[baseLevel]->Height2;
454 for (i = baseLevel + 1; i < maxLevels; i++) {
455 if (width > 1) {
456 width /= 2;
457 }
458 if (height > 1) {
459 height /= 2;
460 }
461 if (i >= minLevel && i <= maxLevel) {
462 if (!t->Image[i]) {
463 t->Complete = GL_FALSE;
464 incomplete(t, "2D Image[i] == NULL");
465 return;
466 }
467 if (t->Image[i]->Width2 != width) {
468 t->Complete = GL_FALSE;
469 incomplete(t, "2D Image[i] bad width");
470 return;
471 }
472 if (t->Image[i]->Height2 != height) {
473 t->Complete = GL_FALSE;
474 incomplete(t, "2D Image[i] bad height");
475 return;
476 }
477 if (width==1 && height==1) {
478 return; /* found smallest needed mipmap, all done! */
479 }
480 }
481 }
482 }
483 else if (t->Target == GL_TEXTURE_3D) {
484 /* Test 3-D mipmaps */
485 GLuint width = t->Image[baseLevel]->Width2;
486 GLuint height = t->Image[baseLevel]->Height2;
487 GLuint depth = t->Image[baseLevel]->Depth2;
488 for (i = baseLevel + 1; i < maxLevels; i++) {
489 if (width > 1) {
490 width /= 2;
491 }
492 if (height > 1) {
493 height /= 2;
494 }
495 if (depth > 1) {
496 depth /= 2;
497 }
498 if (i >= minLevel && i <= maxLevel) {
499 if (!t->Image[i]) {
500 incomplete(t, "3D Image[i] == NULL");
501 t->Complete = GL_FALSE;
502 return;
503 }
504 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
505 t->Complete = GL_FALSE;
506 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
507 return;
508 }
509 if (t->Image[i]->Width2 != width) {
510 t->Complete = GL_FALSE;
511 incomplete(t, "3D Image[i] bad width");
512 return;
513 }
514 if (t->Image[i]->Height2 != height) {
515 t->Complete = GL_FALSE;
516 incomplete(t, "3D Image[i] bad height");
517 return;
518 }
519 if (t->Image[i]->Depth2 != depth) {
520 t->Complete = GL_FALSE;
521 incomplete(t, "3D Image[i] bad depth");
522 return;
523 }
524 }
525 if (width == 1 && height == 1 && depth == 1) {
526 return; /* found smallest needed mipmap, all done! */
527 }
528 }
529 }
530 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
531 /* make sure 6 cube faces are consistant */
532 GLuint width = t->Image[baseLevel]->Width2;
533 GLuint height = t->Image[baseLevel]->Height2;
534 for (i = baseLevel + 1; i < maxLevels; i++) {
535 if (width > 1) {
536 width /= 2;
537 }
538 if (height > 1) {
539 height /= 2;
540 }
541 if (i >= minLevel && i <= maxLevel) {
542 /* check that we have images defined */
543 if (!t->Image[i] || !t->NegX[i] ||
544 !t->PosY[i] || !t->NegY[i] ||
545 !t->PosZ[i] || !t->NegZ[i]) {
546 t->Complete = GL_FALSE;
547 incomplete(t, "CubeMap Image[i] == NULL");
548 return;
549 }
550 /* Don't support GL_DEPTH_COMPONENT for cube maps */
551 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
552 t->Complete = GL_FALSE;
553 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
554 return;
555 }
556 /* check that all six images have same size */
557 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
558 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
559 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
560 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
561 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
562 t->Complete = GL_FALSE;
563 incomplete(t, "CubeMap Image[i] bad size");
564 return;
565 }
566 }
567 if (width == 1 && height == 1) {
568 return; /* found smallest needed mipmap, all done! */
569 }
570 }
571 }
572 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
573 /* XXX special checking? */
574 }
575 else {
576 /* Target = ??? */
577 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
578 }
579 }
580 }
581
582 /*@}*/
583
584
585 /***********************************************************************/
586 /** \name API functions */
587 /*@{*/
588
589 /**
590 * Texture name generation lock.
591 *
592 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
593 * of texture IDs is atomic.
594 */
595 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
596
597 /**
598 * Generate texture names.
599 *
600 * \param n number of texture names to be generated.
601 * \param texName an array in which will hold the generated texture names.
602 *
603 * \sa glGenTextures().
604 *
605 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
606 * to find a block of free texture IDs which are stored in \p texName.
607 * Corresponding empty texture objects are also generated.
608 */
609 void GLAPIENTRY
610 _mesa_GenTextures( GLsizei n, GLuint *texName )
611 {
612 GET_CURRENT_CONTEXT(ctx);
613 GLuint first;
614 GLint i;
615 ASSERT_OUTSIDE_BEGIN_END(ctx);
616
617 if (n < 0) {
618 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
619 return;
620 }
621
622 if (!texName)
623 return;
624
625 /*
626 * This must be atomic (generation and allocation of texture IDs)
627 */
628 _glthread_LOCK_MUTEX(GenTexturesLock);
629
630 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
631
632 /* Allocate new, empty texture objects */
633 for (i = 0; i < n; i++) {
634 struct gl_texture_object *texObj;
635 GLuint name = first + i;
636 GLenum target = 0;
637 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
638 if (!texObj) {
639 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
640 return;
641 }
642 _mesa_save_texture_object(ctx, texObj);
643 texName[i] = name;
644 }
645
646 _glthread_UNLOCK_MUTEX(GenTexturesLock);
647 }
648
649 /**
650 * Delete named textures.
651 *
652 * \param n number of textures to be deleted.
653 * \param texName array of textures names to be deleted.
654 *
655 * \sa glDeleteTextures().
656 *
657 * For each texture checks if its bound to any of the texture units, unbinding
658 * it and decrementing the reference count if so. If the texture reference
659 * count is zero, delete its object.
660 */
661 void GLAPIENTRY
662 _mesa_DeleteTextures( GLsizei n, const GLuint *texName)
663 {
664 GET_CURRENT_CONTEXT(ctx);
665 GLint i;
666 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
667
668 if (!texName)
669 return;
670
671 for (i=0;i<n;i++) {
672 if (texName[i] > 0) {
673 struct gl_texture_object *delObj = (struct gl_texture_object *)
674 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
675 if (delObj) {
676 /* First check if this texture is currently bound.
677 * If so, unbind it and decrement the reference count.
678 */
679 GLuint u;
680 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
681 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
682 if (delObj == unit->Current1D) {
683 unit->Current1D = ctx->Shared->Default1D;
684 ctx->Shared->Default1D->RefCount++;
685 delObj->RefCount--;
686 if (delObj == unit->_Current)
687 unit->_Current = unit->Current1D;
688 }
689 else if (delObj == unit->Current2D) {
690 unit->Current2D = ctx->Shared->Default2D;
691 ctx->Shared->Default2D->RefCount++;
692 delObj->RefCount--;
693 if (delObj == unit->_Current)
694 unit->_Current = unit->Current2D;
695 }
696 else if (delObj == unit->Current3D) {
697 unit->Current3D = ctx->Shared->Default3D;
698 ctx->Shared->Default3D->RefCount++;
699 delObj->RefCount--;
700 if (delObj == unit->_Current)
701 unit->_Current = unit->Current3D;
702 }
703 else if (delObj == unit->CurrentCubeMap) {
704 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
705 ctx->Shared->DefaultCubeMap->RefCount++;
706 delObj->RefCount--;
707 if (delObj == unit->_Current)
708 unit->_Current = unit->CurrentCubeMap;
709 }
710 else if (delObj == unit->CurrentRect) {
711 unit->CurrentRect = ctx->Shared->DefaultRect;
712 ctx->Shared->DefaultRect->RefCount++;
713 delObj->RefCount--;
714 if (delObj == unit->_Current)
715 unit->_Current = unit->CurrentRect;
716 }
717 }
718 ctx->NewState |= _NEW_TEXTURE;
719
720 /* Decrement reference count and delete if zero */
721 delObj->RefCount--;
722 ASSERT(delObj->RefCount >= 0);
723
724 if (delObj->RefCount == 0) {
725 ASSERT(delObj->Name != 0);
726 _mesa_remove_texture_object(ctx, delObj);
727 ASSERT(ctx->Driver.DeleteTexture);
728 (*ctx->Driver.DeleteTexture)(ctx, delObj);
729 }
730 }
731 }
732 }
733 }
734
735 /**
736 * Bind a named texture to a texturing target.
737 *
738 * \param target texture target.
739 * \param texName texture name.
740 *
741 * \sa glBindTexture().
742 *
743 * Determines the old texture object bound and returns immediately if rebinding
744 * the same texture. Get the current texture which is either a default texture
745 * if name is null, a named texture from the hash, or a new texture if the
746 * given texture name is new. Increments its reference count, binds it, and
747 * calls dd_function_table::BindTexture. Decrements the old texture reference
748 * count and deletes it if it reaches zero.
749 */
750 void GLAPIENTRY
751 _mesa_BindTexture( GLenum target, GLuint texName )
752 {
753 GET_CURRENT_CONTEXT(ctx);
754 GLuint unit = ctx->Texture.CurrentUnit;
755 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
756 struct gl_texture_object *oldTexObj;
757 struct gl_texture_object *newTexObj = 0;
758 ASSERT_OUTSIDE_BEGIN_END(ctx);
759
760 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
761 _mesa_debug(ctx, "glBindTexture %s %d\n",
762 _mesa_lookup_enum_by_nr(target), (GLint) texName);
763
764 switch (target) {
765 case GL_TEXTURE_1D:
766 oldTexObj = texUnit->Current1D;
767 break;
768 case GL_TEXTURE_2D:
769 oldTexObj = texUnit->Current2D;
770 break;
771 case GL_TEXTURE_3D:
772 oldTexObj = texUnit->Current3D;
773 break;
774 case GL_TEXTURE_CUBE_MAP_ARB:
775 if (!ctx->Extensions.ARB_texture_cube_map) {
776 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
777 return;
778 }
779 oldTexObj = texUnit->CurrentCubeMap;
780 break;
781 case GL_TEXTURE_RECTANGLE_NV:
782 if (!ctx->Extensions.NV_texture_rectangle) {
783 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
784 return;
785 }
786 oldTexObj = texUnit->CurrentRect;
787 break;
788 default:
789 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
790 return;
791 }
792
793 if (oldTexObj->Name == texName)
794 return; /* rebinding the same texture- no change */
795
796 /*
797 * Get pointer to new texture object (newTexObj)
798 */
799 if (texName == 0) {
800 /* newTexObj = a default texture object */
801 switch (target) {
802 case GL_TEXTURE_1D:
803 newTexObj = ctx->Shared->Default1D;
804 break;
805 case GL_TEXTURE_2D:
806 newTexObj = ctx->Shared->Default2D;
807 break;
808 case GL_TEXTURE_3D:
809 newTexObj = ctx->Shared->Default3D;
810 break;
811 case GL_TEXTURE_CUBE_MAP_ARB:
812 newTexObj = ctx->Shared->DefaultCubeMap;
813 break;
814 case GL_TEXTURE_RECTANGLE_NV:
815 newTexObj = ctx->Shared->DefaultRect;
816 break;
817 default:
818 ; /* Bad targets are caught above */
819 }
820 }
821 else {
822 /* non-default texture object */
823 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
824 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
825 if (newTexObj) {
826 /* error checking */
827 if (newTexObj->Target != 0 && newTexObj->Target != target) {
828 /* the named texture object's dimensions don't match the target */
829 _mesa_error( ctx, GL_INVALID_OPERATION,
830 "glBindTexture(wrong dimensionality)" );
831 return;
832 }
833 if (newTexObj->Target == 0 && target == GL_TEXTURE_RECTANGLE_NV) {
834 /* have to init wrap and filter state here - kind of klunky */
835 newTexObj->WrapS = GL_CLAMP_TO_EDGE;
836 newTexObj->WrapT = GL_CLAMP_TO_EDGE;
837 newTexObj->WrapR = GL_CLAMP_TO_EDGE;
838 newTexObj->MinFilter = GL_LINEAR;
839 }
840 }
841 else {
842 /* if this is a new texture id, allocate a texture object now */
843 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
844 if (!newTexObj) {
845 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
846 return;
847 }
848 _mesa_save_texture_object(ctx, newTexObj);
849 }
850 newTexObj->Target = target;
851 }
852
853 newTexObj->RefCount++;
854
855 /* do the actual binding, but first flush outstanding vertices:
856 */
857 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
858
859 switch (target) {
860 case GL_TEXTURE_1D:
861 texUnit->Current1D = newTexObj;
862 break;
863 case GL_TEXTURE_2D:
864 texUnit->Current2D = newTexObj;
865 break;
866 case GL_TEXTURE_3D:
867 texUnit->Current3D = newTexObj;
868 break;
869 case GL_TEXTURE_CUBE_MAP_ARB:
870 texUnit->CurrentCubeMap = newTexObj;
871 break;
872 case GL_TEXTURE_RECTANGLE_NV:
873 texUnit->CurrentRect = newTexObj;
874 break;
875 default:
876 _mesa_problem(ctx, "bad target in BindTexture");
877 return;
878 }
879
880 /* Pass BindTexture call to device driver */
881 if (ctx->Driver.BindTexture)
882 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
883
884 oldTexObj->RefCount--;
885 assert(oldTexObj->RefCount >= 0);
886 if (oldTexObj->RefCount == 0) {
887 assert(oldTexObj->Name != 0);
888 _mesa_remove_texture_object(ctx, oldTexObj);
889 ASSERT(ctx->Driver.DeleteTexture);
890 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
891 }
892 }
893
894 /**
895 * Set texture priorities.
896 *
897 * \param n number of textures.
898 * \param texName texture names.
899 * \param priorities corresponding texture priorities.
900 *
901 * \sa glPrioritizeTextures().
902 *
903 * Looks up each texture in the hash, clamps the corresponding priority between
904 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
905 */
906 void GLAPIENTRY
907 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
908 const GLclampf *priorities )
909 {
910 GET_CURRENT_CONTEXT(ctx);
911 GLint i;
912 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
913
914 if (n < 0) {
915 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
916 return;
917 }
918
919 if (!priorities)
920 return;
921
922 for (i = 0; i < n; i++) {
923 if (texName[i] > 0) {
924 struct gl_texture_object *t = (struct gl_texture_object *)
925 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
926 if (t) {
927 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
928 if (ctx->Driver.PrioritizeTexture)
929 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
930 }
931 }
932 }
933
934 ctx->NewState |= _NEW_TEXTURE;
935 }
936
937 /**
938 * See if textures are loaded in texture memory.
939 *
940 * \param n number of textures to query.
941 * \param texName array with the texture names.
942 * \param residences array which will hold the residence status.
943 *
944 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
945 *
946 * \sa glAreTexturesResident().
947 *
948 * Looks up each texture in the hash and calls
949 * dd_function_table::IsTextureResident.
950 */
951 GLboolean GLAPIENTRY
952 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
953 GLboolean *residences)
954 {
955 GET_CURRENT_CONTEXT(ctx);
956 GLboolean allResident = GL_TRUE;
957 GLint i, j;
958 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
959
960 if (n < 0) {
961 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
962 return GL_FALSE;
963 }
964
965 if (!texName || !residences)
966 return GL_FALSE;
967
968 for (i = 0; i < n; i++) {
969 struct gl_texture_object *t;
970 if (texName[i] == 0) {
971 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
972 return GL_FALSE;
973 }
974 t = (struct gl_texture_object *)
975 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
976 if (!t) {
977 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
978 return GL_FALSE;
979 }
980 if (!ctx->Driver.IsTextureResident ||
981 ctx->Driver.IsTextureResident(ctx, t)) {
982 /* The texture is resident */
983 if (!allResident)
984 residences[i] = GL_TRUE;
985 }
986 else {
987 /* The texture is not resident */
988 if (allResident) {
989 allResident = GL_FALSE;
990 for (j = 0; j < i; j++)
991 residences[j] = GL_TRUE;
992 }
993 residences[i] = GL_FALSE;
994 }
995 }
996
997 return allResident;
998 }
999
1000 /**
1001 * See if a name corresponds to a texture.
1002 *
1003 * \param texture texture name.
1004 *
1005 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1006 * otherwise.
1007 *
1008 * \sa glIsTexture().
1009 *
1010 * Calls _mesa_HashLookup().
1011 */
1012 GLboolean GLAPIENTRY
1013 _mesa_IsTexture( GLuint texture )
1014 {
1015 GET_CURRENT_CONTEXT(ctx);
1016 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1017 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
1018 }
1019
1020 /*@}*/