3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2006 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.
46 #define _mesa_sprintf sprintf
49 /**********************************************************************/
50 /** \name Internal functions */
55 * Return the gl_texture_object for a given ID.
57 struct gl_texture_object
*
58 _mesa_lookup_texture(GLcontext
*ctx
, GLuint id
)
60 return (struct gl_texture_object
*)
61 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
79 * \return pointer to new texture object.
81 struct gl_texture_object
*
82 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
84 struct gl_texture_object
*obj
;
86 obj
= MALLOC_STRUCT(gl_texture_object
);
87 _mesa_initialize_texture_object(obj
, name
, target
);
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
99 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
100 GLuint name
, GLenum target
)
102 ASSERT(target
== 0 ||
103 target
== GL_TEXTURE_1D
||
104 target
== GL_TEXTURE_2D
||
105 target
== GL_TEXTURE_3D
||
106 target
== GL_TEXTURE_CUBE_MAP_ARB
||
107 target
== GL_TEXTURE_RECTANGLE_NV
);
109 _mesa_bzero(obj
, sizeof(*obj
));
110 /* init the non-zero fields */
111 _glthread_INIT_MUTEX(obj
->Mutex
);
114 obj
->Target
= target
;
115 obj
->Priority
= 1.0F
;
116 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
117 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
118 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
119 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
120 obj
->MinFilter
= GL_LINEAR
;
123 obj
->WrapS
= GL_REPEAT
;
124 obj
->WrapT
= GL_REPEAT
;
125 obj
->WrapR
= GL_REPEAT
;
126 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
128 obj
->MagFilter
= GL_LINEAR
;
129 obj
->MinLod
= -1000.0;
130 obj
->MaxLod
= 1000.0;
133 obj
->MaxLevel
= 1000;
134 obj
->MaxAnisotropy
= 1.0;
135 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
136 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
137 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
138 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
139 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
140 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
141 _mesa_init_colortable(&obj
->Palette
);
146 * Deallocate a texture object struct. It should have already been
147 * removed from the texture object pool.
149 * \param shared the shared GL state to which the object belongs.
150 * \param texOjb the texture object to delete.
153 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
159 _mesa_free_colortable_data(&texObj
->Palette
);
161 /* free the texture images */
162 for (face
= 0; face
< 6; face
++) {
163 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
164 if (texObj
->Image
[face
][i
]) {
165 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
170 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
171 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
173 /* free this object */
181 * Copy texture object state from one texture object to another.
182 * Use for glPush/PopAttrib.
184 * \param dest destination texture object.
185 * \param src source texture object.
188 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
189 const struct gl_texture_object
*src
)
191 dest
->Name
= src
->Name
;
192 dest
->Priority
= src
->Priority
;
193 dest
->BorderColor
[0] = src
->BorderColor
[0];
194 dest
->BorderColor
[1] = src
->BorderColor
[1];
195 dest
->BorderColor
[2] = src
->BorderColor
[2];
196 dest
->BorderColor
[3] = src
->BorderColor
[3];
197 dest
->WrapS
= src
->WrapS
;
198 dest
->WrapT
= src
->WrapT
;
199 dest
->WrapR
= src
->WrapR
;
200 dest
->MinFilter
= src
->MinFilter
;
201 dest
->MagFilter
= src
->MagFilter
;
202 dest
->MinLod
= src
->MinLod
;
203 dest
->MaxLod
= src
->MaxLod
;
204 dest
->LodBias
= src
->LodBias
;
205 dest
->BaseLevel
= src
->BaseLevel
;
206 dest
->MaxLevel
= src
->MaxLevel
;
207 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
208 dest
->CompareFlag
= src
->CompareFlag
;
209 dest
->CompareOperator
= src
->CompareOperator
;
210 dest
->ShadowAmbient
= src
->ShadowAmbient
;
211 dest
->CompareMode
= src
->CompareMode
;
212 dest
->CompareFunc
= src
->CompareFunc
;
213 dest
->DepthMode
= src
->DepthMode
;
214 dest
->_MaxLevel
= src
->_MaxLevel
;
215 dest
->_MaxLambda
= src
->_MaxLambda
;
216 dest
->GenerateMipmap
= src
->GenerateMipmap
;
217 dest
->Palette
= src
->Palette
;
218 dest
->Complete
= src
->Complete
;
223 * Report why a texture object is incomplete.
225 * \param t texture object.
226 * \param why string describing why it's incomplete.
228 * \note For debug purposes only.
232 incomplete(const struct gl_texture_object
*t
, const char *why
)
234 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
237 #define incomplete(t, why)
242 * Examine a texture object to determine if it is complete.
244 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
247 * \param ctx GL context.
248 * \param t texture object.
250 * According to the texture target, verifies that each of the mipmaps is
251 * present and has the expected size.
254 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
255 struct gl_texture_object
*t
)
257 const GLint baseLevel
= t
->BaseLevel
;
258 GLint maxLog2
= 0, maxLevels
= 0;
260 t
->Complete
= GL_TRUE
; /* be optimistic */
262 /* Always need the base level image */
263 if (!t
->Image
[0][baseLevel
]) {
265 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
266 (void *) t
, t
->Name
, baseLevel
);
268 t
->Complete
= GL_FALSE
;
272 /* Check width/height/depth for zero */
273 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
274 t
->Image
[0][baseLevel
]->Height
== 0 ||
275 t
->Image
[0][baseLevel
]->Depth
== 0) {
276 incomplete(t
, "texture width = 0");
277 t
->Complete
= GL_FALSE
;
281 /* Compute _MaxLevel */
282 if (t
->Target
== GL_TEXTURE_1D
) {
283 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
284 maxLevels
= ctx
->Const
.MaxTextureLevels
;
286 else if (t
->Target
== GL_TEXTURE_2D
) {
287 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
288 t
->Image
[0][baseLevel
]->HeightLog2
);
289 maxLevels
= ctx
->Const
.MaxTextureLevels
;
291 else if (t
->Target
== GL_TEXTURE_3D
) {
292 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
293 t
->Image
[0][baseLevel
]->HeightLog2
);
294 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
295 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
297 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
298 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
299 t
->Image
[0][baseLevel
]->HeightLog2
);
300 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
302 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
303 maxLog2
= 0; /* not applicable */
304 maxLevels
= 1; /* no mipmapping */
307 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
311 ASSERT(maxLevels
> 0);
313 t
->_MaxLevel
= baseLevel
+ maxLog2
;
314 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
315 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
317 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
318 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
320 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
321 /* make sure that all six cube map level 0 images are the same size */
322 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
323 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
325 for (face
= 1; face
< 6; face
++) {
326 if (t
->Image
[face
][baseLevel
] == NULL
||
327 t
->Image
[face
][baseLevel
]->Width2
!= w
||
328 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
329 t
->Complete
= GL_FALSE
;
330 incomplete(t
, "Non-quare cubemap image");
336 /* extra checking for mipmaps */
337 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
339 * Mipmapping: determine if we have a complete set of mipmaps
342 GLint minLevel
= baseLevel
;
343 GLint maxLevel
= t
->_MaxLevel
;
345 if (minLevel
> maxLevel
) {
346 t
->Complete
= GL_FALSE
;
347 incomplete(t
, "minLevel > maxLevel");
351 /* Test dimension-independent attributes */
352 for (i
= minLevel
; i
<= maxLevel
; i
++) {
353 if (t
->Image
[0][i
]) {
354 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
355 t
->Complete
= GL_FALSE
;
356 incomplete(t
, "Format[i] != Format[baseLevel]");
359 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
360 t
->Complete
= GL_FALSE
;
361 incomplete(t
, "Border[i] != Border[baseLevel]");
367 /* Test things which depend on number of texture image dimensions */
368 if (t
->Target
== GL_TEXTURE_1D
) {
369 /* Test 1-D mipmaps */
370 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
371 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
375 if (i
>= minLevel
&& i
<= maxLevel
) {
376 if (!t
->Image
[0][i
]) {
377 t
->Complete
= GL_FALSE
;
378 incomplete(t
, "1D Image[0][i] == NULL");
381 if (t
->Image
[0][i
]->Width2
!= width
) {
382 t
->Complete
= GL_FALSE
;
383 incomplete(t
, "1D Image[0][i] bad width");
388 return; /* found smallest needed mipmap, all done! */
392 else if (t
->Target
== GL_TEXTURE_2D
) {
393 /* Test 2-D mipmaps */
394 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
395 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
396 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
403 if (i
>= minLevel
&& i
<= maxLevel
) {
404 if (!t
->Image
[0][i
]) {
405 t
->Complete
= GL_FALSE
;
406 incomplete(t
, "2D Image[0][i] == NULL");
409 if (t
->Image
[0][i
]->Width2
!= width
) {
410 t
->Complete
= GL_FALSE
;
411 incomplete(t
, "2D Image[0][i] bad width");
414 if (t
->Image
[0][i
]->Height2
!= height
) {
415 t
->Complete
= GL_FALSE
;
416 incomplete(t
, "2D Image[0][i] bad height");
419 if (width
==1 && height
==1) {
420 return; /* found smallest needed mipmap, all done! */
425 else if (t
->Target
== GL_TEXTURE_3D
) {
426 /* Test 3-D mipmaps */
427 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
428 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
429 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
430 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
440 if (i
>= minLevel
&& i
<= maxLevel
) {
441 if (!t
->Image
[0][i
]) {
442 incomplete(t
, "3D Image[0][i] == NULL");
443 t
->Complete
= GL_FALSE
;
446 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
447 t
->Complete
= GL_FALSE
;
448 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
451 if (t
->Image
[0][i
]->Width2
!= width
) {
452 t
->Complete
= GL_FALSE
;
453 incomplete(t
, "3D Image[0][i] bad width");
456 if (t
->Image
[0][i
]->Height2
!= height
) {
457 t
->Complete
= GL_FALSE
;
458 incomplete(t
, "3D Image[0][i] bad height");
461 if (t
->Image
[0][i
]->Depth2
!= depth
) {
462 t
->Complete
= GL_FALSE
;
463 incomplete(t
, "3D Image[0][i] bad depth");
467 if (width
== 1 && height
== 1 && depth
== 1) {
468 return; /* found smallest needed mipmap, all done! */
472 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
473 /* make sure 6 cube faces are consistant */
474 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
475 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
476 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
483 if (i
>= minLevel
&& i
<= maxLevel
) {
485 for (face
= 0; face
< 6; face
++) {
486 /* check that we have images defined */
487 if (!t
->Image
[face
][i
]) {
488 t
->Complete
= GL_FALSE
;
489 incomplete(t
, "CubeMap Image[n][i] == NULL");
492 /* Don't support GL_DEPTH_COMPONENT for cube maps */
493 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
494 t
->Complete
= GL_FALSE
;
495 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
498 /* check that all six images have same size */
499 if (t
->Image
[face
][i
]->Width2
!=width
||
500 t
->Image
[face
][i
]->Height2
!=height
) {
501 t
->Complete
= GL_FALSE
;
502 incomplete(t
, "CubeMap Image[n][i] bad size");
507 if (width
== 1 && height
== 1) {
508 return; /* found smallest needed mipmap, all done! */
512 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
513 /* XXX special checking? */
517 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
525 /***********************************************************************/
526 /** \name API functions */
530 * Texture name generation lock.
532 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
533 * of texture IDs is atomic.
535 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
538 * Generate texture names.
540 * \param n number of texture names to be generated.
541 * \param textures an array in which will hold the generated texture names.
543 * \sa glGenTextures().
545 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
546 * to find a block of free texture IDs which are stored in \p textures.
547 * Corresponding empty texture objects are also generated.
550 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
552 GET_CURRENT_CONTEXT(ctx
);
555 ASSERT_OUTSIDE_BEGIN_END(ctx
);
558 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
566 * This must be atomic (generation and allocation of texture IDs)
568 _glthread_LOCK_MUTEX(GenTexturesLock
);
570 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
572 /* Allocate new, empty texture objects */
573 for (i
= 0; i
< n
; i
++) {
574 struct gl_texture_object
*texObj
;
575 GLuint name
= first
+ i
;
577 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
579 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
580 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
584 /* insert into hash table */
585 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
586 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
587 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
592 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
597 * Check if the given texture object is bound to the current draw or
598 * read framebuffer. If so, Unbind it.
601 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
603 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
606 for (i
= 0; i
< n
; i
++) {
607 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
610 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
611 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
612 fb
->Attachment
[j
].Texture
== texObj
) {
613 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
622 * Check if the given texture object is bound to any texture image units and
624 * XXX all RefCount accesses should be protected by a mutex.
627 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
631 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
632 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
633 if (texObj
== unit
->Current1D
) {
634 unit
->Current1D
= ctx
->Shared
->Default1D
;
635 ctx
->Shared
->Default1D
->RefCount
++;
637 if (texObj
== unit
->_Current
)
638 unit
->_Current
= unit
->Current1D
;
640 else if (texObj
== unit
->Current2D
) {
641 unit
->Current2D
= ctx
->Shared
->Default2D
;
642 ctx
->Shared
->Default2D
->RefCount
++;
644 if (texObj
== unit
->_Current
)
645 unit
->_Current
= unit
->Current2D
;
647 else if (texObj
== unit
->Current3D
) {
648 unit
->Current3D
= ctx
->Shared
->Default3D
;
649 ctx
->Shared
->Default3D
->RefCount
++;
651 if (texObj
== unit
->_Current
)
652 unit
->_Current
= unit
->Current3D
;
654 else if (texObj
== unit
->CurrentCubeMap
) {
655 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
656 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
658 if (texObj
== unit
->_Current
)
659 unit
->_Current
= unit
->CurrentCubeMap
;
661 else if (texObj
== unit
->CurrentRect
) {
662 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
663 ctx
->Shared
->DefaultRect
->RefCount
++;
665 if (texObj
== unit
->_Current
)
666 unit
->_Current
= unit
->CurrentRect
;
673 * Delete named textures.
675 * \param n number of textures to be deleted.
676 * \param textures array of texture IDs to be deleted.
678 * \sa glDeleteTextures().
680 * If we're about to delete a texture that's currently bound to any
681 * texture unit, unbind the texture first. Decrement the reference
682 * count on the texture object and delete it if it's zero.
683 * Recall that texture objects can be shared among several rendering
687 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
689 GET_CURRENT_CONTEXT(ctx
);
691 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
696 for (i
= 0; i
< n
; i
++) {
697 if (textures
[i
] > 0) {
698 struct gl_texture_object
*delObj
699 = _mesa_lookup_texture(ctx
, textures
[i
]);
704 _mesa_lock_texture(ctx
, delObj
);
706 /* Check if texture is bound to any framebuffer objects.
708 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
710 unbind_texobj_from_fbo(ctx
, delObj
);
712 /* Check if this texture is currently bound to any texture units.
713 * If so, unbind it and decrement the reference count.
715 unbind_texobj_from_texunits(ctx
, delObj
);
717 ctx
->NewState
|= _NEW_TEXTURE
;
719 /* The texture _name_ is now free for re-use.
720 * Remove it from the hash table now.
722 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
723 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
724 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
726 /* The actual texture object will not be freed until it's no
727 * longer bound in any context.
728 * XXX all RefCount accesses should be protected by a mutex.
731 delete = (delObj
->RefCount
== 0);
732 _mesa_unlock_texture(ctx
, delObj
);
734 /* We know that refcount went to zero above, so this is
735 * the only pointer left to delObj, so we don't have to
736 * worry about locking any more:
739 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objs */
740 ASSERT(ctx
->Driver
.DeleteTexture
);
741 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
750 * Bind a named texture to a texturing target.
752 * \param target texture target.
753 * \param texName texture name.
755 * \sa glBindTexture().
757 * Determines the old texture object bound and returns immediately if rebinding
758 * the same texture. Get the current texture which is either a default texture
759 * if name is null, a named texture from the hash, or a new texture if the
760 * given texture name is new. Increments its reference count, binds it, and
761 * calls dd_function_table::BindTexture. Decrements the old texture reference
762 * count and deletes it if it reaches zero.
765 _mesa_BindTexture( GLenum target
, GLuint texName
)
767 GET_CURRENT_CONTEXT(ctx
);
768 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
769 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
770 struct gl_texture_object
*oldTexObj
;
771 struct gl_texture_object
*newTexObj
= NULL
;
772 ASSERT_OUTSIDE_BEGIN_END(ctx
);
774 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
775 _mesa_debug(ctx
, "glBindTexture %s %d\n",
776 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
779 * Get pointer to currently bound texture object (oldTexObj)
783 oldTexObj
= texUnit
->Current1D
;
786 oldTexObj
= texUnit
->Current2D
;
789 oldTexObj
= texUnit
->Current3D
;
791 case GL_TEXTURE_CUBE_MAP_ARB
:
792 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
793 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
796 oldTexObj
= texUnit
->CurrentCubeMap
;
798 case GL_TEXTURE_RECTANGLE_NV
:
799 if (!ctx
->Extensions
.NV_texture_rectangle
) {
800 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
803 oldTexObj
= texUnit
->CurrentRect
;
806 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
810 if (oldTexObj
->Name
== texName
) {
811 /* XXX this might be wrong. If the texobj is in use by another
812 * context and a texobj parameter was changed, this might be our
813 * only chance to update this context's hardware state.
814 * Note that some applications re-bind the same texture a lot so we
815 * want to handle that case quickly.
817 return; /* rebinding the same texture- no change */
821 * Get pointer to new texture object (newTexObj)
824 /* newTexObj = a default texture object */
827 newTexObj
= ctx
->Shared
->Default1D
;
830 newTexObj
= ctx
->Shared
->Default2D
;
833 newTexObj
= ctx
->Shared
->Default3D
;
835 case GL_TEXTURE_CUBE_MAP_ARB
:
836 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
838 case GL_TEXTURE_RECTANGLE_NV
:
839 newTexObj
= ctx
->Shared
->DefaultRect
;
842 ; /* Bad targets are caught above */
846 /* non-default texture object */
847 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
850 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
851 /* the named texture object's dimensions don't match the target */
852 _mesa_error( ctx
, GL_INVALID_OPERATION
,
853 "glBindTexture(wrong dimensionality)" );
856 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
857 /* have to init wrap and filter state here - kind of klunky */
858 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
859 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
860 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
861 newTexObj
->MinFilter
= GL_LINEAR
;
862 if (ctx
->Driver
.TexParameter
) {
863 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
864 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
865 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
866 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
867 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
868 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
873 /* if this is a new texture id, allocate a texture object now */
874 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
876 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
880 /* and insert it into hash table */
881 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
882 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
883 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
885 newTexObj
->Target
= target
;
888 /* XXX all RefCount accesses should be protected by a mutex. */
889 newTexObj
->RefCount
++;
891 /* do the actual binding, but first flush outstanding vertices:
893 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
897 texUnit
->Current1D
= newTexObj
;
900 texUnit
->Current2D
= newTexObj
;
903 texUnit
->Current3D
= newTexObj
;
905 case GL_TEXTURE_CUBE_MAP_ARB
:
906 texUnit
->CurrentCubeMap
= newTexObj
;
908 case GL_TEXTURE_RECTANGLE_NV
:
909 texUnit
->CurrentRect
= newTexObj
;
912 _mesa_problem(ctx
, "bad target in BindTexture");
916 /* Pass BindTexture call to device driver */
917 if (ctx
->Driver
.BindTexture
)
918 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
920 /* Decrement the reference count on the old texture and check if it's
923 /* XXX all RefCount accesses should be protected by a mutex. */
924 oldTexObj
->RefCount
--;
925 ASSERT(oldTexObj
->RefCount
>= 0);
926 if (oldTexObj
->RefCount
== 0) {
927 ASSERT(oldTexObj
->Name
!= 0);
928 ASSERT(ctx
->Driver
.DeleteTexture
);
929 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
935 * Set texture priorities.
937 * \param n number of textures.
938 * \param texName texture names.
939 * \param priorities corresponding texture priorities.
941 * \sa glPrioritizeTextures().
943 * Looks up each texture in the hash, clamps the corresponding priority between
944 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
947 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
948 const GLclampf
*priorities
)
950 GET_CURRENT_CONTEXT(ctx
);
952 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
955 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
962 for (i
= 0; i
< n
; i
++) {
963 if (texName
[i
] > 0) {
964 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
966 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
967 if (ctx
->Driver
.PrioritizeTexture
)
968 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
973 ctx
->NewState
|= _NEW_TEXTURE
;
977 * See if textures are loaded in texture memory.
979 * \param n number of textures to query.
980 * \param texName array with the texture names.
981 * \param residences array which will hold the residence status.
983 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
985 * \sa glAreTexturesResident().
987 * Looks up each texture in the hash and calls
988 * dd_function_table::IsTextureResident.
991 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
992 GLboolean
*residences
)
994 GET_CURRENT_CONTEXT(ctx
);
995 GLboolean allResident
= GL_TRUE
;
997 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1000 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1004 if (!texName
|| !residences
)
1007 for (i
= 0; i
< n
; i
++) {
1008 struct gl_texture_object
*t
;
1009 if (texName
[i
] == 0) {
1010 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1013 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1015 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1018 if (!ctx
->Driver
.IsTextureResident
||
1019 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1020 /* The texture is resident */
1022 residences
[i
] = GL_TRUE
;
1025 /* The texture is not resident */
1027 allResident
= GL_FALSE
;
1028 for (j
= 0; j
< i
; j
++)
1029 residences
[j
] = GL_TRUE
;
1031 residences
[i
] = GL_FALSE
;
1039 * See if a name corresponds to a texture.
1041 * \param texture texture name.
1043 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1046 * \sa glIsTexture().
1048 * Calls _mesa_HashLookup().
1050 GLboolean GLAPIENTRY
1051 _mesa_IsTexture( GLuint texture
)
1053 struct gl_texture_object
*t
;
1054 GET_CURRENT_CONTEXT(ctx
);
1055 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1060 t
= _mesa_lookup_texture(ctx
, texture
);
1062 /* IsTexture is true only after object has been bound once. */
1063 return t
&& t
->Target
;
1066 /* Simplest implementation of texture locking: Grab the a new mutex in
1067 * the shared context. Examine the shared context state timestamp and
1068 * if there has been a change, set the appropriate bits in
1071 * See also _mesa_lock/unlock_texture in texobj.h
1073 void _mesa_lock_context_textures( GLcontext
*ctx
)
1075 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1077 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1078 ctx
->NewState
|= _NEW_TEXTURE
;
1079 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1084 void _mesa_unlock_context_textures( GLcontext
*ctx
)
1086 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1087 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);