7b61bfadd99a9883b27be463c027e1ac5782bafb
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 #define _mesa_sprintf sprintf
47 /**********************************************************************/
48 /** \name Internal functions */
52 * Allocate and initialize a new texture object. But don't put it into the
53 * texture object hash table.
55 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
58 * \param shared the shared GL state structure to contain the texture object
59 * \param name integer name for the texture object
60 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
61 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
64 * \return pointer to new texture object.
66 struct gl_texture_object
*
67 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
69 struct gl_texture_object
*obj
;
71 obj
= MALLOC_STRUCT(gl_texture_object
);
72 _mesa_initialize_texture_object(obj
, name
, target
);
78 * Initialize a new texture object to default values.
79 * \param obj the texture object
80 * \param name the texture name
81 * \param target the texture target
84 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
85 GLuint name
, GLenum target
)
88 target
== GL_TEXTURE_1D
||
89 target
== GL_TEXTURE_2D
||
90 target
== GL_TEXTURE_3D
||
91 target
== GL_TEXTURE_CUBE_MAP_ARB
||
92 target
== GL_TEXTURE_RECTANGLE_NV
);
94 _mesa_bzero(obj
, sizeof(*obj
));
95 /* init the non-zero fields */
96 _glthread_INIT_MUTEX(obj
->Mutex
);
100 obj
->Priority
= 1.0F
;
101 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
102 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
103 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
104 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
105 obj
->MinFilter
= GL_LINEAR
;
108 obj
->WrapS
= GL_REPEAT
;
109 obj
->WrapT
= GL_REPEAT
;
110 obj
->WrapR
= GL_REPEAT
;
111 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
113 obj
->MagFilter
= GL_LINEAR
;
114 obj
->MinLod
= -1000.0;
115 obj
->MaxLod
= 1000.0;
118 obj
->MaxLevel
= 1000;
119 obj
->MaxAnisotropy
= 1.0;
120 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
121 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
122 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
123 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
124 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
125 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
126 _mesa_init_colortable(&obj
->Palette
);
131 * Deallocate a texture object struct. It should have already been
132 * removed from the texture object pool.
134 * \param shared the shared GL state to which the object belongs.
135 * \param texOjb the texture object to delete.
138 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
144 _mesa_free_colortable_data(&texObj
->Palette
);
146 /* free the texture images */
147 for (face
= 0; face
< 6; face
++) {
148 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
149 if (texObj
->Image
[face
][i
]) {
150 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
155 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
156 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
158 /* free this object */
166 * Copy texture object state from one texture object to another.
167 * Use for glPush/PopAttrib.
169 * \param dest destination texture object.
170 * \param src source texture object.
173 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
174 const struct gl_texture_object
*src
)
176 dest
->Name
= src
->Name
;
177 dest
->Priority
= src
->Priority
;
178 dest
->BorderColor
[0] = src
->BorderColor
[0];
179 dest
->BorderColor
[1] = src
->BorderColor
[1];
180 dest
->BorderColor
[2] = src
->BorderColor
[2];
181 dest
->BorderColor
[3] = src
->BorderColor
[3];
182 dest
->WrapS
= src
->WrapS
;
183 dest
->WrapT
= src
->WrapT
;
184 dest
->WrapR
= src
->WrapR
;
185 dest
->MinFilter
= src
->MinFilter
;
186 dest
->MagFilter
= src
->MagFilter
;
187 dest
->MinLod
= src
->MinLod
;
188 dest
->MaxLod
= src
->MaxLod
;
189 dest
->LodBias
= src
->LodBias
;
190 dest
->BaseLevel
= src
->BaseLevel
;
191 dest
->MaxLevel
= src
->MaxLevel
;
192 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
193 dest
->CompareFlag
= src
->CompareFlag
;
194 dest
->CompareOperator
= src
->CompareOperator
;
195 dest
->ShadowAmbient
= src
->ShadowAmbient
;
196 dest
->CompareMode
= src
->CompareMode
;
197 dest
->CompareFunc
= src
->CompareFunc
;
198 dest
->DepthMode
= src
->DepthMode
;
199 dest
->_MaxLevel
= src
->_MaxLevel
;
200 dest
->_MaxLambda
= src
->_MaxLambda
;
201 dest
->GenerateMipmap
= src
->GenerateMipmap
;
202 dest
->Palette
= src
->Palette
;
203 dest
->Complete
= src
->Complete
;
204 dest
->_IsPowerOfTwo
= src
->_IsPowerOfTwo
;
209 * Report why a texture object is incomplete.
211 * \param t texture object.
212 * \param why string describing why it's incomplete.
214 * \note For debug purposes only.
218 incomplete(const struct gl_texture_object
*t
, const char *why
)
220 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
223 #define incomplete(t, why)
228 * Examine a texture object to determine if it is complete.
230 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
233 * \param ctx GL context.
234 * \param t texture object.
236 * According to the texture target, verifies that each of the mipmaps is
237 * present and has the expected size.
240 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
241 struct gl_texture_object
*t
)
243 const GLint baseLevel
= t
->BaseLevel
;
244 GLint maxLog2
= 0, maxLevels
= 0;
246 t
->Complete
= GL_TRUE
; /* be optimistic */
247 t
->_IsPowerOfTwo
= GL_TRUE
; /* may be set FALSE below */
249 /* Always need the base level image */
250 if (!t
->Image
[0][baseLevel
]) {
252 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
253 (void *) t
, t
->Name
, baseLevel
);
255 t
->Complete
= GL_FALSE
;
259 /* Check width/height/depth for zero */
260 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
261 t
->Image
[0][baseLevel
]->Height
== 0 ||
262 t
->Image
[0][baseLevel
]->Depth
== 0) {
263 incomplete(t
, "texture width = 0");
264 t
->Complete
= GL_FALSE
;
268 /* Compute _MaxLevel */
269 if (t
->Target
== GL_TEXTURE_1D
) {
270 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
271 maxLevels
= ctx
->Const
.MaxTextureLevels
;
273 else if (t
->Target
== GL_TEXTURE_2D
) {
274 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
275 t
->Image
[0][baseLevel
]->HeightLog2
);
276 maxLevels
= ctx
->Const
.MaxTextureLevels
;
278 else if (t
->Target
== GL_TEXTURE_3D
) {
279 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
280 t
->Image
[0][baseLevel
]->HeightLog2
);
281 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
282 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
284 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
285 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
286 t
->Image
[0][baseLevel
]->HeightLog2
);
287 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
289 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
290 maxLog2
= 0; /* not applicable */
291 maxLevels
= 1; /* no mipmapping */
294 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
298 ASSERT(maxLevels
> 0);
300 t
->_MaxLevel
= baseLevel
+ maxLog2
;
301 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
302 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
304 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
305 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
307 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
308 /* make sure that all six cube map level 0 images are the same size */
309 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
310 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
312 for (face
= 1; face
< 6; face
++) {
313 if (t
->Image
[face
][baseLevel
] == NULL
||
314 t
->Image
[face
][baseLevel
]->Width2
!= w
||
315 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
316 t
->Complete
= GL_FALSE
;
317 incomplete(t
, "Non-quare cubemap image");
323 /* check for non power of two */
324 if (!t
->Image
[0][baseLevel
]->_IsPowerOfTwo
) {
325 t
->_IsPowerOfTwo
= GL_FALSE
;
328 /* extra checking for mipmaps */
329 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
331 * Mipmapping: determine if we have a complete set of mipmaps
334 GLint minLevel
= baseLevel
;
335 GLint maxLevel
= t
->_MaxLevel
;
337 if (minLevel
> maxLevel
) {
338 t
->Complete
= GL_FALSE
;
339 incomplete(t
, "minLevel > maxLevel");
343 /* Test dimension-independent attributes */
344 for (i
= minLevel
; i
<= maxLevel
; i
++) {
345 if (t
->Image
[0][i
]) {
346 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
347 t
->Complete
= GL_FALSE
;
348 incomplete(t
, "Format[i] != Format[baseLevel]");
351 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
352 t
->Complete
= GL_FALSE
;
353 incomplete(t
, "Border[i] != Border[baseLevel]");
359 /* Test things which depend on number of texture image dimensions */
360 if (t
->Target
== GL_TEXTURE_1D
) {
361 /* Test 1-D mipmaps */
362 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
363 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
367 if (i
>= minLevel
&& i
<= maxLevel
) {
368 if (!t
->Image
[0][i
]) {
369 t
->Complete
= GL_FALSE
;
370 incomplete(t
, "1D Image[0][i] == NULL");
373 if (t
->Image
[0][i
]->Width2
!= width
) {
374 t
->Complete
= GL_FALSE
;
375 incomplete(t
, "1D Image[0][i] bad width");
380 return; /* found smallest needed mipmap, all done! */
384 else if (t
->Target
== GL_TEXTURE_2D
) {
385 /* Test 2-D mipmaps */
386 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
387 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
388 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
395 if (i
>= minLevel
&& i
<= maxLevel
) {
396 if (!t
->Image
[0][i
]) {
397 t
->Complete
= GL_FALSE
;
398 incomplete(t
, "2D Image[0][i] == NULL");
401 if (t
->Image
[0][i
]->Width2
!= width
) {
402 t
->Complete
= GL_FALSE
;
403 incomplete(t
, "2D Image[0][i] bad width");
406 if (t
->Image
[0][i
]->Height2
!= height
) {
407 t
->Complete
= GL_FALSE
;
408 incomplete(t
, "2D Image[0][i] bad height");
411 if (width
==1 && height
==1) {
412 return; /* found smallest needed mipmap, all done! */
417 else if (t
->Target
== GL_TEXTURE_3D
) {
418 /* Test 3-D mipmaps */
419 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
420 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
421 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
422 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
432 if (i
>= minLevel
&& i
<= maxLevel
) {
433 if (!t
->Image
[0][i
]) {
434 incomplete(t
, "3D Image[0][i] == NULL");
435 t
->Complete
= GL_FALSE
;
438 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
439 t
->Complete
= GL_FALSE
;
440 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
443 if (t
->Image
[0][i
]->Width2
!= width
) {
444 t
->Complete
= GL_FALSE
;
445 incomplete(t
, "3D Image[0][i] bad width");
448 if (t
->Image
[0][i
]->Height2
!= height
) {
449 t
->Complete
= GL_FALSE
;
450 incomplete(t
, "3D Image[0][i] bad height");
453 if (t
->Image
[0][i
]->Depth2
!= depth
) {
454 t
->Complete
= GL_FALSE
;
455 incomplete(t
, "3D Image[0][i] bad depth");
459 if (width
== 1 && height
== 1 && depth
== 1) {
460 return; /* found smallest needed mipmap, all done! */
464 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
465 /* make sure 6 cube faces are consistant */
466 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
467 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
468 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
475 if (i
>= minLevel
&& i
<= maxLevel
) {
477 for (face
= 0; face
< 6; face
++) {
478 /* check that we have images defined */
479 if (!t
->Image
[face
][i
]) {
480 t
->Complete
= GL_FALSE
;
481 incomplete(t
, "CubeMap Image[n][i] == NULL");
484 /* Don't support GL_DEPTH_COMPONENT for cube maps */
485 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
486 t
->Complete
= GL_FALSE
;
487 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
490 /* check that all six images have same size */
491 if (t
->Image
[face
][i
]->Width2
!=width
||
492 t
->Image
[face
][i
]->Height2
!=height
) {
493 t
->Complete
= GL_FALSE
;
494 incomplete(t
, "CubeMap Image[n][i] bad size");
499 if (width
== 1 && height
== 1) {
500 return; /* found smallest needed mipmap, all done! */
504 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
505 /* XXX special checking? */
509 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
517 /***********************************************************************/
518 /** \name API functions */
522 * Texture name generation lock.
524 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
525 * of texture IDs is atomic.
527 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
530 * Generate texture names.
532 * \param n number of texture names to be generated.
533 * \param textures an array in which will hold the generated texture names.
535 * \sa glGenTextures().
537 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
538 * to find a block of free texture IDs which are stored in \p textures.
539 * Corresponding empty texture objects are also generated.
542 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
544 GET_CURRENT_CONTEXT(ctx
);
547 ASSERT_OUTSIDE_BEGIN_END(ctx
);
550 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
558 * This must be atomic (generation and allocation of texture IDs)
560 _glthread_LOCK_MUTEX(GenTexturesLock
);
562 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
564 /* Allocate new, empty texture objects */
565 for (i
= 0; i
< n
; i
++) {
566 struct gl_texture_object
*texObj
;
567 GLuint name
= first
+ i
;
569 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
571 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
572 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
576 /* insert into hash table */
577 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
578 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
579 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
584 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
589 * Delete named textures.
591 * \param n number of textures to be deleted.
592 * \param textures array of texture IDs to be deleted.
594 * \sa glDeleteTextures().
596 * If we're about to delete a texture that's currently bound to any
597 * texture unit, unbind the texture first. Decrement the reference
598 * count on the texture object and delete it if it's zero.
599 * Recall that texture objects can be shared among several rendering
603 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
605 GET_CURRENT_CONTEXT(ctx
);
607 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
612 for (i
= 0; i
< n
; i
++) {
613 if (textures
[i
] > 0) {
614 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
615 _mesa_HashLookup(ctx
->Shared
->TexObjects
, textures
[i
]);
617 /* First check if this texture is currently bound.
618 * If so, unbind it and decrement the reference count.
619 * XXX all RefCount accesses should be protected by a mutex.
622 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
623 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
624 if (delObj
== unit
->Current1D
) {
625 unit
->Current1D
= ctx
->Shared
->Default1D
;
626 ctx
->Shared
->Default1D
->RefCount
++;
628 if (delObj
== unit
->_Current
)
629 unit
->_Current
= unit
->Current1D
;
631 else if (delObj
== unit
->Current2D
) {
632 unit
->Current2D
= ctx
->Shared
->Default2D
;
633 ctx
->Shared
->Default2D
->RefCount
++;
635 if (delObj
== unit
->_Current
)
636 unit
->_Current
= unit
->Current2D
;
638 else if (delObj
== unit
->Current3D
) {
639 unit
->Current3D
= ctx
->Shared
->Default3D
;
640 ctx
->Shared
->Default3D
->RefCount
++;
642 if (delObj
== unit
->_Current
)
643 unit
->_Current
= unit
->Current3D
;
645 else if (delObj
== unit
->CurrentCubeMap
) {
646 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
647 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
649 if (delObj
== unit
->_Current
)
650 unit
->_Current
= unit
->CurrentCubeMap
;
652 else if (delObj
== unit
->CurrentRect
) {
653 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
654 ctx
->Shared
->DefaultRect
->RefCount
++;
656 if (delObj
== unit
->_Current
)
657 unit
->_Current
= unit
->CurrentRect
;
660 ctx
->NewState
|= _NEW_TEXTURE
;
662 /* The texture _name_ is now free for re-use.
663 * Remove it from the hash table now.
665 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
666 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
667 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
669 /* The actual texture object will not be freed until it's no
670 * longer bound in any context.
671 * XXX all RefCount accesses should be protected by a mutex.
674 if (delObj
->RefCount
== 0) {
675 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objs */
676 ASSERT(ctx
->Driver
.DeleteTexture
);
677 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
686 * Bind a named texture to a texturing target.
688 * \param target texture target.
689 * \param texName texture name.
691 * \sa glBindTexture().
693 * Determines the old texture object bound and returns immediately if rebinding
694 * the same texture. Get the current texture which is either a default texture
695 * if name is null, a named texture from the hash, or a new texture if the
696 * given texture name is new. Increments its reference count, binds it, and
697 * calls dd_function_table::BindTexture. Decrements the old texture reference
698 * count and deletes it if it reaches zero.
701 _mesa_BindTexture( GLenum target
, GLuint texName
)
703 GET_CURRENT_CONTEXT(ctx
);
704 GLuint unit
= ctx
->Texture
.CurrentUnit
;
705 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
706 struct gl_texture_object
*oldTexObj
;
707 struct gl_texture_object
*newTexObj
= NULL
;
708 ASSERT_OUTSIDE_BEGIN_END(ctx
);
710 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
711 _mesa_debug(ctx
, "glBindTexture %s %d\n",
712 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
715 * Get pointer to currently bound texture object (oldTexObj)
719 oldTexObj
= texUnit
->Current1D
;
722 oldTexObj
= texUnit
->Current2D
;
725 oldTexObj
= texUnit
->Current3D
;
727 case GL_TEXTURE_CUBE_MAP_ARB
:
728 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
729 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
732 oldTexObj
= texUnit
->CurrentCubeMap
;
734 case GL_TEXTURE_RECTANGLE_NV
:
735 if (!ctx
->Extensions
.NV_texture_rectangle
) {
736 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
739 oldTexObj
= texUnit
->CurrentRect
;
742 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
746 if (oldTexObj
->Name
== texName
)
747 /* XXX this might be wrong. If the texobj is in use by another
748 * context and a texobj parameter was changed, this might be our
749 * only chance to update this context's hardware state.
751 return; /* rebinding the same texture- no change */
754 * Get pointer to new texture object (newTexObj)
757 /* newTexObj = a default texture object */
760 newTexObj
= ctx
->Shared
->Default1D
;
763 newTexObj
= ctx
->Shared
->Default2D
;
766 newTexObj
= ctx
->Shared
->Default3D
;
768 case GL_TEXTURE_CUBE_MAP_ARB
:
769 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
771 case GL_TEXTURE_RECTANGLE_NV
:
772 newTexObj
= ctx
->Shared
->DefaultRect
;
775 ; /* Bad targets are caught above */
779 /* non-default texture object */
780 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
781 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
784 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
785 /* the named texture object's dimensions don't match the target */
786 _mesa_error( ctx
, GL_INVALID_OPERATION
,
787 "glBindTexture(wrong dimensionality)" );
790 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
791 /* have to init wrap and filter state here - kind of klunky */
792 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
793 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
794 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
795 newTexObj
->MinFilter
= GL_LINEAR
;
796 if (ctx
->Driver
.TexParameter
) {
797 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
798 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
799 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
800 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
801 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
802 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
807 /* if this is a new texture id, allocate a texture object now */
808 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
810 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
814 /* and insert it into hash table */
815 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
816 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
817 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
819 newTexObj
->Target
= target
;
822 /* XXX all RefCount accesses should be protected by a mutex. */
823 newTexObj
->RefCount
++;
825 /* do the actual binding, but first flush outstanding vertices:
827 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
831 texUnit
->Current1D
= newTexObj
;
834 texUnit
->Current2D
= newTexObj
;
837 texUnit
->Current3D
= newTexObj
;
839 case GL_TEXTURE_CUBE_MAP_ARB
:
840 texUnit
->CurrentCubeMap
= newTexObj
;
842 case GL_TEXTURE_RECTANGLE_NV
:
843 texUnit
->CurrentRect
= newTexObj
;
846 _mesa_problem(ctx
, "bad target in BindTexture");
850 /* Pass BindTexture call to device driver */
851 if (ctx
->Driver
.BindTexture
)
852 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
854 /* Decrement the reference count on the old texture and check if it's
857 /* XXX all RefCount accesses should be protected by a mutex. */
858 oldTexObj
->RefCount
--;
859 ASSERT(oldTexObj
->RefCount
>= 0);
860 if (oldTexObj
->RefCount
== 0) {
861 ASSERT(oldTexObj
->Name
!= 0);
862 ASSERT(ctx
->Driver
.DeleteTexture
);
863 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
869 * Set texture priorities.
871 * \param n number of textures.
872 * \param texName texture names.
873 * \param priorities corresponding texture priorities.
875 * \sa glPrioritizeTextures().
877 * Looks up each texture in the hash, clamps the corresponding priority between
878 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
881 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
882 const GLclampf
*priorities
)
884 GET_CURRENT_CONTEXT(ctx
);
886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
889 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
896 for (i
= 0; i
< n
; i
++) {
897 if (texName
[i
] > 0) {
898 struct gl_texture_object
*t
= (struct gl_texture_object
*)
899 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
901 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
902 if (ctx
->Driver
.PrioritizeTexture
)
903 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
908 ctx
->NewState
|= _NEW_TEXTURE
;
912 * See if textures are loaded in texture memory.
914 * \param n number of textures to query.
915 * \param texName array with the texture names.
916 * \param residences array which will hold the residence status.
918 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
920 * \sa glAreTexturesResident().
922 * Looks up each texture in the hash and calls
923 * dd_function_table::IsTextureResident.
926 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
927 GLboolean
*residences
)
929 GET_CURRENT_CONTEXT(ctx
);
930 GLboolean allResident
= GL_TRUE
;
932 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
935 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
939 if (!texName
|| !residences
)
942 for (i
= 0; i
< n
; i
++) {
943 struct gl_texture_object
*t
;
944 if (texName
[i
] == 0) {
945 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
948 t
= (struct gl_texture_object
*)
949 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
951 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
954 if (!ctx
->Driver
.IsTextureResident
||
955 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
956 /* The texture is resident */
958 residences
[i
] = GL_TRUE
;
961 /* The texture is not resident */
963 allResident
= GL_FALSE
;
964 for (j
= 0; j
< i
; j
++)
965 residences
[j
] = GL_TRUE
;
967 residences
[i
] = GL_FALSE
;
975 * See if a name corresponds to a texture.
977 * \param texture texture name.
979 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
984 * Calls _mesa_HashLookup().
987 _mesa_IsTexture( GLuint texture
)
989 struct gl_texture_object
*t
;
990 GET_CURRENT_CONTEXT(ctx
);
991 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
996 t
= (struct gl_texture_object
*)
997 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);
999 /* IsTexture is true only after object has been bound once. */
1000 return t
&& t
->Target
;