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