3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2004 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( 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 */
161 * Add the given texture object to the texture object pool.
164 _mesa_save_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
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
);
172 if (texObj
->Name
> 0) {
173 /* insert into hash table */
174 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
180 * Remove the given texture object from the texture object pool.
181 * Do not deallocate the texture object though.
184 _mesa_remove_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
186 struct gl_texture_object
*tprev
, *tcurr
;
188 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
190 /* unlink from the linked list */
192 tcurr
= ctx
->Shared
->TexObjectList
;
194 if (tcurr
== texObj
) {
196 tprev
->Next
= texObj
->Next
;
199 ctx
->Shared
->TexObjectList
= texObj
->Next
;
207 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
209 if (texObj
->Name
> 0) {
210 /* remove from hash table */
211 _mesa_HashRemove(ctx
->Shared
->TexObjects
, texObj
->Name
);
216 * Copy texture object state from one texture object to another.
218 * \param dest destination texture object.
219 * \param src source texture object.
222 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
223 const struct gl_texture_object
*src
)
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
;
258 * Report why a texture object is incomplete.
260 * \param t texture object.
261 * \param why string describing why it's incomplete.
263 * \note For debug purposes only.
267 incomplete(const struct gl_texture_object
*t
, const char *why
)
269 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
272 #define incomplete(t, why)
277 * Examine a texture object to determine if it is complete.
279 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
282 * \param ctx GL context.
283 * \param t texture object.
285 * According to the texture target, verifies that each of the mipmaps is
286 * present and has the expected size.
289 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
290 struct gl_texture_object
*t
)
292 const GLint baseLevel
= t
->BaseLevel
;
293 GLint maxLog2
= 0, maxLevels
= 0;
295 t
->Complete
= GL_TRUE
; /* be optimistic */
296 t
->_IsPowerOfTwo
= GL_TRUE
; /* may be set FALSE below */
298 /* Always need the base level image */
299 if (!t
->Image
[0][baseLevel
]) {
301 sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
302 (void *) t
, t
->Name
, baseLevel
);
304 t
->Complete
= GL_FALSE
;
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
;
317 /* Compute _MaxLevel */
318 if (t
->Target
== GL_TEXTURE_1D
) {
319 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
320 maxLevels
= ctx
->Const
.MaxTextureLevels
;
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
;
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
;
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
;
338 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
339 maxLog2
= 0; /* not applicable */
340 maxLevels
= 1; /* no mipmapping */
343 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
347 ASSERT(maxLevels
> 0);
349 t
->_MaxLevel
= baseLevel
+ maxLog2
;
350 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
351 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
353 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
354 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
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
;
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");
372 /* check for non power of two */
373 if (!t
->Image
[0][baseLevel
]->_IsPowerOfTwo
) {
374 t
->_IsPowerOfTwo
= GL_FALSE
;
377 /* extra checking for mipmaps */
378 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
380 * Mipmapping: determine if we have a complete set of mipmaps
383 GLint minLevel
= baseLevel
;
384 GLint maxLevel
= t
->_MaxLevel
;
386 if (minLevel
> maxLevel
) {
387 t
->Complete
= GL_FALSE
;
388 incomplete(t
, "minLevel > maxLevel");
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]");
400 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
401 t
->Complete
= GL_FALSE
;
402 incomplete(t
, "Border[i] != Border[baseLevel]");
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
++) {
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");
422 if (t
->Image
[0][i
]->Width2
!= width
) {
423 t
->Complete
= GL_FALSE
;
424 incomplete(t
, "1D Image[0][i] bad width");
429 return; /* found smallest needed mipmap, all done! */
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
++) {
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");
450 if (t
->Image
[0][i
]->Width2
!= width
) {
451 t
->Complete
= GL_FALSE
;
452 incomplete(t
, "2D Image[0][i] bad width");
455 if (t
->Image
[0][i
]->Height2
!= height
) {
456 t
->Complete
= GL_FALSE
;
457 incomplete(t
, "2D Image[0][i] bad height");
460 if (width
==1 && height
==1) {
461 return; /* found smallest needed mipmap, all done! */
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
++) {
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
;
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");
492 if (t
->Image
[0][i
]->Width2
!= width
) {
493 t
->Complete
= GL_FALSE
;
494 incomplete(t
, "3D Image[0][i] bad width");
497 if (t
->Image
[0][i
]->Height2
!= height
) {
498 t
->Complete
= GL_FALSE
;
499 incomplete(t
, "3D Image[0][i] bad height");
502 if (t
->Image
[0][i
]->Depth2
!= depth
) {
503 t
->Complete
= GL_FALSE
;
504 incomplete(t
, "3D Image[0][i] bad depth");
508 if (width
== 1 && height
== 1 && depth
== 1) {
509 return; /* found smallest needed mipmap, all done! */
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
++) {
524 if (i
>= minLevel
&& i
<= maxLevel
) {
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");
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");
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");
548 if (width
== 1 && height
== 1) {
549 return; /* found smallest needed mipmap, all done! */
553 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
554 /* XXX special checking? */
558 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
566 /***********************************************************************/
567 /** \name API functions */
571 * Texture name generation lock.
573 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
574 * of texture IDs is atomic.
576 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
579 * Generate texture names.
581 * \param n number of texture names to be generated.
582 * \param textures an array in which will hold the generated texture names.
584 * \sa glGenTextures().
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.
591 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
593 GET_CURRENT_CONTEXT(ctx
);
596 ASSERT_OUTSIDE_BEGIN_END(ctx
);
599 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
607 * This must be atomic (generation and allocation of texture IDs)
609 _glthread_LOCK_MUTEX(GenTexturesLock
);
611 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
613 /* Allocate new, empty texture objects */
614 for (i
= 0; i
< n
; i
++) {
615 struct gl_texture_object
*texObj
;
616 GLuint name
= first
+ i
;
618 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
620 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
623 _mesa_save_texture_object(ctx
, texObj
);
627 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
632 * Delete named textures.
634 * \param n number of textures to be deleted.
635 * \param textures array of texture IDs to be deleted.
637 * \sa glDeleteTextures().
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
646 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
648 GET_CURRENT_CONTEXT(ctx
);
650 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
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
]);
660 /* First check if this texture is currently bound.
661 * If so, unbind it and decrement the reference count.
664 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
665 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
666 if (delObj
== unit
->Current1D
) {
667 unit
->Current1D
= ctx
->Shared
->Default1D
;
668 ctx
->Shared
->Default1D
->RefCount
++;
670 if (delObj
== unit
->_Current
)
671 unit
->_Current
= unit
->Current1D
;
673 else if (delObj
== unit
->Current2D
) {
674 unit
->Current2D
= ctx
->Shared
->Default2D
;
675 ctx
->Shared
->Default2D
->RefCount
++;
677 if (delObj
== unit
->_Current
)
678 unit
->_Current
= unit
->Current2D
;
680 else if (delObj
== unit
->Current3D
) {
681 unit
->Current3D
= ctx
->Shared
->Default3D
;
682 ctx
->Shared
->Default3D
->RefCount
++;
684 if (delObj
== unit
->_Current
)
685 unit
->_Current
= unit
->Current3D
;
687 else if (delObj
== unit
->CurrentCubeMap
) {
688 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
689 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
691 if (delObj
== unit
->_Current
)
692 unit
->_Current
= unit
->CurrentCubeMap
;
694 else if (delObj
== unit
->CurrentRect
) {
695 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
696 ctx
->Shared
->DefaultRect
->RefCount
++;
698 if (delObj
== unit
->_Current
)
699 unit
->_Current
= unit
->CurrentRect
;
702 ctx
->NewState
|= _NEW_TEXTURE
;
704 /* If user hasn't already tried to delete the texture... */
705 if (!delObj
->DeletePending
) {
706 delObj
->DeletePending
= GL_TRUE
;
708 ASSERT(delObj
->RefCount
>= 0);
711 /* See if we can really delete the texture now */
712 if (delObj
->RefCount
== 0) {
713 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objects */
714 _mesa_remove_texture_object(ctx
, delObj
);
715 ASSERT(ctx
->Driver
.DeleteTexture
);
716 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
725 * Bind a named texture to a texturing target.
727 * \param target texture target.
728 * \param texName texture name.
730 * \sa glBindTexture().
732 * Determines the old texture object bound and returns immediately if rebinding
733 * the same texture. Get the current texture which is either a default texture
734 * if name is null, a named texture from the hash, or a new texture if the
735 * given texture name is new. Increments its reference count, binds it, and
736 * calls dd_function_table::BindTexture. Decrements the old texture reference
737 * count and deletes it if it reaches zero.
740 _mesa_BindTexture( GLenum target
, GLuint texName
)
742 GET_CURRENT_CONTEXT(ctx
);
743 GLuint unit
= ctx
->Texture
.CurrentUnit
;
744 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
745 struct gl_texture_object
*oldTexObj
;
746 struct gl_texture_object
*newTexObj
= 0;
747 ASSERT_OUTSIDE_BEGIN_END(ctx
);
749 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
750 _mesa_debug(ctx
, "glBindTexture %s %d\n",
751 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
754 * Get pointer to currently bound texture object (oldTexObj)
758 oldTexObj
= texUnit
->Current1D
;
761 oldTexObj
= texUnit
->Current2D
;
764 oldTexObj
= texUnit
->Current3D
;
766 case GL_TEXTURE_CUBE_MAP_ARB
:
767 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
768 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
771 oldTexObj
= texUnit
->CurrentCubeMap
;
773 case GL_TEXTURE_RECTANGLE_NV
:
774 if (!ctx
->Extensions
.NV_texture_rectangle
) {
775 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
778 oldTexObj
= texUnit
->CurrentRect
;
781 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
785 if (oldTexObj
->Name
== texName
)
786 /* XXX this might be wrong. If the texobj is in use by another
787 * context and a texobj parameter was changed, this might be our
788 * only chance to update this context's hardware state.
790 return; /* rebinding the same texture- no change */
793 * Get pointer to new texture object (newTexObj)
796 /* newTexObj = a default texture object */
799 newTexObj
= ctx
->Shared
->Default1D
;
802 newTexObj
= ctx
->Shared
->Default2D
;
805 newTexObj
= ctx
->Shared
->Default3D
;
807 case GL_TEXTURE_CUBE_MAP_ARB
:
808 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
810 case GL_TEXTURE_RECTANGLE_NV
:
811 newTexObj
= ctx
->Shared
->DefaultRect
;
814 ; /* Bad targets are caught above */
818 /* non-default texture object */
819 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
820 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
823 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
824 /* the named texture object's dimensions don't match the target */
825 _mesa_error( ctx
, GL_INVALID_OPERATION
,
826 "glBindTexture(wrong dimensionality)" );
829 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
830 /* have to init wrap and filter state here - kind of klunky */
831 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
832 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
833 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
834 newTexObj
->MinFilter
= GL_LINEAR
;
835 if (ctx
->Driver
.TexParameter
) {
836 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
837 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
838 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
839 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
840 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
841 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
846 /* if this is a new texture id, allocate a texture object now */
847 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
849 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
852 _mesa_save_texture_object(ctx
, newTexObj
);
854 newTexObj
->Target
= target
;
857 newTexObj
->RefCount
++;
859 /* do the actual binding, but first flush outstanding vertices:
861 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
865 texUnit
->Current1D
= newTexObj
;
868 texUnit
->Current2D
= newTexObj
;
871 texUnit
->Current3D
= newTexObj
;
873 case GL_TEXTURE_CUBE_MAP_ARB
:
874 texUnit
->CurrentCubeMap
= newTexObj
;
876 case GL_TEXTURE_RECTANGLE_NV
:
877 texUnit
->CurrentRect
= newTexObj
;
880 _mesa_problem(ctx
, "bad target in BindTexture");
884 /* Pass BindTexture call to device driver */
885 if (ctx
->Driver
.BindTexture
)
886 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
888 /* Decrement the reference count on the old texture and check if it's
891 oldTexObj
->RefCount
--;
892 ASSERT(oldTexObj
->RefCount
>= 0);
893 if (oldTexObj
->RefCount
== 0) {
894 ASSERT(oldTexObj
->Name
!= 0);
895 ASSERT(oldTexObj
->DeletePending
);
896 _mesa_remove_texture_object(ctx
, oldTexObj
);
897 ASSERT(ctx
->Driver
.DeleteTexture
);
898 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
904 * Set texture priorities.
906 * \param n number of textures.
907 * \param texName texture names.
908 * \param priorities corresponding texture priorities.
910 * \sa glPrioritizeTextures().
912 * Looks up each texture in the hash, clamps the corresponding priority between
913 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
916 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
917 const GLclampf
*priorities
)
919 GET_CURRENT_CONTEXT(ctx
);
921 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
924 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
931 for (i
= 0; i
< n
; i
++) {
932 if (texName
[i
] > 0) {
933 struct gl_texture_object
*t
= (struct gl_texture_object
*)
934 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
936 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
937 if (ctx
->Driver
.PrioritizeTexture
)
938 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
943 ctx
->NewState
|= _NEW_TEXTURE
;
947 * See if textures are loaded in texture memory.
949 * \param n number of textures to query.
950 * \param texName array with the texture names.
951 * \param residences array which will hold the residence status.
953 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
955 * \sa glAreTexturesResident().
957 * Looks up each texture in the hash and calls
958 * dd_function_table::IsTextureResident.
961 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
962 GLboolean
*residences
)
964 GET_CURRENT_CONTEXT(ctx
);
965 GLboolean allResident
= GL_TRUE
;
967 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
970 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
974 if (!texName
|| !residences
)
977 for (i
= 0; i
< n
; i
++) {
978 struct gl_texture_object
*t
;
979 if (texName
[i
] == 0) {
980 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
983 t
= (struct gl_texture_object
*)
984 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
986 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
989 if (!ctx
->Driver
.IsTextureResident
||
990 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
991 /* The texture is resident */
993 residences
[i
] = GL_TRUE
;
996 /* The texture is not resident */
998 allResident
= GL_FALSE
;
999 for (j
= 0; j
< i
; j
++)
1000 residences
[j
] = GL_TRUE
;
1002 residences
[i
] = GL_FALSE
;
1010 * See if a name corresponds to a texture.
1012 * \param texture texture name.
1014 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1017 * \sa glIsTexture().
1019 * Calls _mesa_HashLookup().
1021 GLboolean GLAPIENTRY
1022 _mesa_IsTexture( GLuint texture
)
1024 GET_CURRENT_CONTEXT(ctx
);
1025 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1026 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);