3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
44 /**********************************************************************/
45 /** \name Internal functions */
49 * Allocate and initialize a new texture object. But don't put it into the
50 * texture object hash table.
52 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
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
61 * \return pointer to new texture object.
63 struct gl_texture_object
*
64 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
66 struct gl_texture_object
*obj
;
68 obj
= MALLOC_STRUCT(gl_texture_object
);
69 _mesa_initialize_texture_object(obj
, name
, target
);
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
81 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
82 GLuint name
, GLenum target
)
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
);
91 _mesa_bzero(obj
, sizeof(*obj
));
92 /* init the non-zero fields */
93 _glthread_INIT_MUTEX(obj
->Mutex
);
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
;
105 obj
->WrapS
= GL_REPEAT
;
106 obj
->WrapT
= GL_REPEAT
;
107 obj
->WrapR
= GL_REPEAT
;
108 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
110 obj
->MagFilter
= GL_LINEAR
;
111 obj
->MinLod
= -1000.0;
112 obj
->MaxLod
= 1000.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
);
128 * Deallocate a texture object struct. It should have already been
129 * removed from the texture object pool.
131 * \param shared the shared GL state to which the object belongs.
132 * \param texOjb the texture object to delete.
135 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
141 _mesa_free_colortable_data(&texObj
->Palette
);
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
] );
152 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
153 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
155 /* free this object */
163 * Copy texture object state from one texture object to another.
164 * Use for glPush/PopAttrib.
166 * \param dest destination texture object.
167 * \param src source texture object.
170 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
171 const struct gl_texture_object
*src
)
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
;
206 * Report why a texture object is incomplete.
208 * \param t texture object.
209 * \param why string describing why it's incomplete.
211 * \note For debug purposes only.
215 incomplete(const struct gl_texture_object
*t
, const char *why
)
217 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
220 #define incomplete(t, why)
225 * Examine a texture object to determine if it is complete.
227 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
230 * \param ctx GL context.
231 * \param t texture object.
233 * According to the texture target, verifies that each of the mipmaps is
234 * present and has the expected size.
237 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
238 struct gl_texture_object
*t
)
240 const GLint baseLevel
= t
->BaseLevel
;
241 GLint maxLog2
= 0, maxLevels
= 0;
243 t
->Complete
= GL_TRUE
; /* be optimistic */
244 t
->_IsPowerOfTwo
= GL_TRUE
; /* may be set FALSE below */
246 /* Always need the base level image */
247 if (!t
->Image
[0][baseLevel
]) {
249 sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
250 (void *) t
, t
->Name
, baseLevel
);
252 t
->Complete
= GL_FALSE
;
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
;
265 /* Compute _MaxLevel */
266 if (t
->Target
== GL_TEXTURE_1D
) {
267 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
268 maxLevels
= ctx
->Const
.MaxTextureLevels
;
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
;
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
;
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
;
286 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
287 maxLog2
= 0; /* not applicable */
288 maxLevels
= 1; /* no mipmapping */
291 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
295 ASSERT(maxLevels
> 0);
297 t
->_MaxLevel
= baseLevel
+ maxLog2
;
298 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
299 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
301 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
302 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
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
;
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");
320 /* check for non power of two */
321 if (!t
->Image
[0][baseLevel
]->_IsPowerOfTwo
) {
322 t
->_IsPowerOfTwo
= GL_FALSE
;
325 /* extra checking for mipmaps */
326 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
328 * Mipmapping: determine if we have a complete set of mipmaps
331 GLint minLevel
= baseLevel
;
332 GLint maxLevel
= t
->_MaxLevel
;
334 if (minLevel
> maxLevel
) {
335 t
->Complete
= GL_FALSE
;
336 incomplete(t
, "minLevel > maxLevel");
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]");
348 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
349 t
->Complete
= GL_FALSE
;
350 incomplete(t
, "Border[i] != Border[baseLevel]");
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
++) {
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");
370 if (t
->Image
[0][i
]->Width2
!= width
) {
371 t
->Complete
= GL_FALSE
;
372 incomplete(t
, "1D Image[0][i] bad width");
377 return; /* found smallest needed mipmap, all done! */
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
++) {
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");
398 if (t
->Image
[0][i
]->Width2
!= width
) {
399 t
->Complete
= GL_FALSE
;
400 incomplete(t
, "2D Image[0][i] bad width");
403 if (t
->Image
[0][i
]->Height2
!= height
) {
404 t
->Complete
= GL_FALSE
;
405 incomplete(t
, "2D Image[0][i] bad height");
408 if (width
==1 && height
==1) {
409 return; /* found smallest needed mipmap, all done! */
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
++) {
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
;
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");
440 if (t
->Image
[0][i
]->Width2
!= width
) {
441 t
->Complete
= GL_FALSE
;
442 incomplete(t
, "3D Image[0][i] bad width");
445 if (t
->Image
[0][i
]->Height2
!= height
) {
446 t
->Complete
= GL_FALSE
;
447 incomplete(t
, "3D Image[0][i] bad height");
450 if (t
->Image
[0][i
]->Depth2
!= depth
) {
451 t
->Complete
= GL_FALSE
;
452 incomplete(t
, "3D Image[0][i] bad depth");
456 if (width
== 1 && height
== 1 && depth
== 1) {
457 return; /* found smallest needed mipmap, all done! */
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
++) {
472 if (i
>= minLevel
&& i
<= maxLevel
) {
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");
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");
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");
496 if (width
== 1 && height
== 1) {
497 return; /* found smallest needed mipmap, all done! */
501 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
502 /* XXX special checking? */
506 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
514 /***********************************************************************/
515 /** \name API functions */
519 * Texture name generation lock.
521 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
522 * of texture IDs is atomic.
524 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
527 * Generate texture names.
529 * \param n number of texture names to be generated.
530 * \param textures an array in which will hold the generated texture names.
532 * \sa glGenTextures().
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.
539 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
541 GET_CURRENT_CONTEXT(ctx
);
544 ASSERT_OUTSIDE_BEGIN_END(ctx
);
547 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
555 * This must be atomic (generation and allocation of texture IDs)
557 _glthread_LOCK_MUTEX(GenTexturesLock
);
559 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
561 /* Allocate new, empty texture objects */
562 for (i
= 0; i
< n
; i
++) {
563 struct gl_texture_object
*texObj
;
564 GLuint name
= first
+ i
;
566 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
568 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
569 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
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
);
581 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
586 * Delete named textures.
588 * \param n number of textures to be deleted.
589 * \param textures array of texture IDs to be deleted.
591 * \sa glDeleteTextures().
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
600 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
602 GET_CURRENT_CONTEXT(ctx
);
604 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
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
]);
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.
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
++;
625 if (delObj
== unit
->_Current
)
626 unit
->_Current
= unit
->Current1D
;
628 else if (delObj
== unit
->Current2D
) {
629 unit
->Current2D
= ctx
->Shared
->Default2D
;
630 ctx
->Shared
->Default2D
->RefCount
++;
632 if (delObj
== unit
->_Current
)
633 unit
->_Current
= unit
->Current2D
;
635 else if (delObj
== unit
->Current3D
) {
636 unit
->Current3D
= ctx
->Shared
->Default3D
;
637 ctx
->Shared
->Default3D
->RefCount
++;
639 if (delObj
== unit
->_Current
)
640 unit
->_Current
= unit
->Current3D
;
642 else if (delObj
== unit
->CurrentCubeMap
) {
643 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
644 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
646 if (delObj
== unit
->_Current
)
647 unit
->_Current
= unit
->CurrentCubeMap
;
649 else if (delObj
== unit
->CurrentRect
) {
650 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
651 ctx
->Shared
->DefaultRect
->RefCount
++;
653 if (delObj
== unit
->_Current
)
654 unit
->_Current
= unit
->CurrentRect
;
657 ctx
->NewState
|= _NEW_TEXTURE
;
659 /* The texture _name_ is now free for re-use.
660 * Remove it from the hash table now.
662 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
663 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
664 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
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.
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
);
683 * Bind a named texture to a texturing target.
685 * \param target texture target.
686 * \param texName texture name.
688 * \sa glBindTexture().
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.
698 _mesa_BindTexture( GLenum target
, GLuint texName
)
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
);
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
);
712 * Get pointer to currently bound texture object (oldTexObj)
716 oldTexObj
= texUnit
->Current1D
;
719 oldTexObj
= texUnit
->Current2D
;
722 oldTexObj
= texUnit
->Current3D
;
724 case GL_TEXTURE_CUBE_MAP_ARB
:
725 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
726 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
729 oldTexObj
= texUnit
->CurrentCubeMap
;
731 case GL_TEXTURE_RECTANGLE_NV
:
732 if (!ctx
->Extensions
.NV_texture_rectangle
) {
733 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
736 oldTexObj
= texUnit
->CurrentRect
;
739 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
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.
748 return; /* rebinding the same texture- no change */
751 * Get pointer to new texture object (newTexObj)
754 /* newTexObj = a default texture object */
757 newTexObj
= ctx
->Shared
->Default1D
;
760 newTexObj
= ctx
->Shared
->Default2D
;
763 newTexObj
= ctx
->Shared
->Default3D
;
765 case GL_TEXTURE_CUBE_MAP_ARB
:
766 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
768 case GL_TEXTURE_RECTANGLE_NV
:
769 newTexObj
= ctx
->Shared
->DefaultRect
;
772 ; /* Bad targets are caught above */
776 /* non-default texture object */
777 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
778 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
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)" );
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
);
804 /* if this is a new texture id, allocate a texture object now */
805 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
807 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
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
);
816 newTexObj
->Target
= target
;
819 /* XXX all RefCount accesses should be protected by a mutex. */
820 newTexObj
->RefCount
++;
822 /* do the actual binding, but first flush outstanding vertices:
824 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
828 texUnit
->Current1D
= newTexObj
;
831 texUnit
->Current2D
= newTexObj
;
834 texUnit
->Current3D
= newTexObj
;
836 case GL_TEXTURE_CUBE_MAP_ARB
:
837 texUnit
->CurrentCubeMap
= newTexObj
;
839 case GL_TEXTURE_RECTANGLE_NV
:
840 texUnit
->CurrentRect
= newTexObj
;
843 _mesa_problem(ctx
, "bad target in BindTexture");
847 /* Pass BindTexture call to device driver */
848 if (ctx
->Driver
.BindTexture
)
849 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
851 /* Decrement the reference count on the old texture and check if it's
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
);
866 * Set texture priorities.
868 * \param n number of textures.
869 * \param texName texture names.
870 * \param priorities corresponding texture priorities.
872 * \sa glPrioritizeTextures().
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.
878 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
879 const GLclampf
*priorities
)
881 GET_CURRENT_CONTEXT(ctx
);
883 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
886 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
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
]);
898 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
899 if (ctx
->Driver
.PrioritizeTexture
)
900 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
905 ctx
->NewState
|= _NEW_TEXTURE
;
909 * See if textures are loaded in texture memory.
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.
915 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
917 * \sa glAreTexturesResident().
919 * Looks up each texture in the hash and calls
920 * dd_function_table::IsTextureResident.
923 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
924 GLboolean
*residences
)
926 GET_CURRENT_CONTEXT(ctx
);
927 GLboolean allResident
= GL_TRUE
;
929 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
932 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
936 if (!texName
|| !residences
)
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");
945 t
= (struct gl_texture_object
*)
946 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
948 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
951 if (!ctx
->Driver
.IsTextureResident
||
952 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
953 /* The texture is resident */
955 residences
[i
] = GL_TRUE
;
958 /* The texture is not resident */
960 allResident
= GL_FALSE
;
961 for (j
= 0; j
< i
; j
++)
962 residences
[j
] = GL_TRUE
;
964 residences
[i
] = GL_FALSE
;
972 * See if a name corresponds to a texture.
974 * \param texture texture name.
976 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
981 * Calls _mesa_HashLookup().
984 _mesa_IsTexture( GLuint texture
)
986 struct gl_texture_object
*t
;
987 GET_CURRENT_CONTEXT(ctx
);
988 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
993 t
= (struct gl_texture_object
*)
994 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);
996 /* IsTexture is true only after object has been bound once. */
997 return t
&& t
->Target
;