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