47d9dd5dcb2c9a90cf4c4420de3bc96bcad46f96
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.
45 /**********************************************************************/
46 /** \name Internal functions */
51 * Return the gl_texture_object for a given ID.
53 struct gl_texture_object
*
54 _mesa_lookup_texture(GLcontext
*ctx
, GLuint id
)
56 return (struct gl_texture_object
*)
57 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
63 * Allocate and initialize a new texture object. But don't put it into the
64 * texture object hash table.
66 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
69 * \param shared the shared GL state structure to contain the texture object
70 * \param name integer name for the texture object
71 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
72 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
75 * \return pointer to new texture object.
77 struct gl_texture_object
*
78 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
80 struct gl_texture_object
*obj
;
82 obj
= MALLOC_STRUCT(gl_texture_object
);
83 _mesa_initialize_texture_object(obj
, name
, target
);
89 * Initialize a new texture object to default values.
90 * \param obj the texture object
91 * \param name the texture name
92 * \param target the texture target
95 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
96 GLuint name
, GLenum target
)
99 target
== GL_TEXTURE_1D
||
100 target
== GL_TEXTURE_2D
||
101 target
== GL_TEXTURE_3D
||
102 target
== GL_TEXTURE_CUBE_MAP_ARB
||
103 target
== GL_TEXTURE_RECTANGLE_NV
||
104 target
== GL_TEXTURE_1D_ARRAY_EXT
||
105 target
== GL_TEXTURE_2D_ARRAY_EXT
);
107 _mesa_bzero(obj
, sizeof(*obj
));
108 /* init the non-zero fields */
109 _glthread_INIT_MUTEX(obj
->Mutex
);
112 obj
->Target
= target
;
113 obj
->Priority
= 1.0F
;
114 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
115 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
116 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
117 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
118 obj
->MinFilter
= GL_LINEAR
;
121 obj
->WrapS
= GL_REPEAT
;
122 obj
->WrapT
= GL_REPEAT
;
123 obj
->WrapR
= GL_REPEAT
;
124 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
126 obj
->MagFilter
= GL_LINEAR
;
127 obj
->MinLod
= -1000.0;
128 obj
->MaxLod
= 1000.0;
131 obj
->MaxLevel
= 1000;
132 obj
->MaxAnisotropy
= 1.0;
133 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
134 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
135 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
136 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
137 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
138 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
139 _mesa_init_colortable(&obj
->Palette
);
144 * Deallocate a texture object struct. It should have already been
145 * removed from the texture object pool.
147 * \param shared the shared GL state to which the object belongs.
148 * \param texOjb the texture object to delete.
151 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
157 _mesa_free_colortable_data(&texObj
->Palette
);
159 /* free the texture images */
160 for (face
= 0; face
< 6; face
++) {
161 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
162 if (texObj
->Image
[face
][i
]) {
163 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
168 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
169 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
171 /* free this object */
179 * Copy texture object state from one texture object to another.
180 * Use for glPush/PopAttrib.
182 * \param dest destination texture object.
183 * \param src source texture object.
186 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
187 const struct gl_texture_object
*src
)
189 dest
->Name
= src
->Name
;
190 dest
->Priority
= src
->Priority
;
191 dest
->BorderColor
[0] = src
->BorderColor
[0];
192 dest
->BorderColor
[1] = src
->BorderColor
[1];
193 dest
->BorderColor
[2] = src
->BorderColor
[2];
194 dest
->BorderColor
[3] = src
->BorderColor
[3];
195 dest
->WrapS
= src
->WrapS
;
196 dest
->WrapT
= src
->WrapT
;
197 dest
->WrapR
= src
->WrapR
;
198 dest
->MinFilter
= src
->MinFilter
;
199 dest
->MagFilter
= src
->MagFilter
;
200 dest
->MinLod
= src
->MinLod
;
201 dest
->MaxLod
= src
->MaxLod
;
202 dest
->LodBias
= src
->LodBias
;
203 dest
->BaseLevel
= src
->BaseLevel
;
204 dest
->MaxLevel
= src
->MaxLevel
;
205 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
206 dest
->CompareFlag
= src
->CompareFlag
;
207 dest
->CompareOperator
= src
->CompareOperator
;
208 dest
->ShadowAmbient
= src
->ShadowAmbient
;
209 dest
->CompareMode
= src
->CompareMode
;
210 dest
->CompareFunc
= src
->CompareFunc
;
211 dest
->DepthMode
= src
->DepthMode
;
212 dest
->_MaxLevel
= src
->_MaxLevel
;
213 dest
->_MaxLambda
= src
->_MaxLambda
;
214 dest
->GenerateMipmap
= src
->GenerateMipmap
;
215 dest
->Palette
= src
->Palette
;
216 dest
->Complete
= src
->Complete
;
221 * Report why a texture object is incomplete.
223 * \param t texture object.
224 * \param why string describing why it's incomplete.
226 * \note For debug purposes only.
230 incomplete(const struct gl_texture_object
*t
, const char *why
)
232 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
235 #define incomplete(t, why)
240 * Examine a texture object to determine if it is complete.
242 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
245 * \param ctx GL context.
246 * \param t texture object.
248 * According to the texture target, verifies that each of the mipmaps is
249 * present and has the expected size.
252 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
253 struct gl_texture_object
*t
)
255 const GLint baseLevel
= t
->BaseLevel
;
256 GLint maxLog2
= 0, maxLevels
= 0;
258 t
->Complete
= GL_TRUE
; /* be optimistic */
260 /* Always need the base level image */
261 if (!t
->Image
[0][baseLevel
]) {
263 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
264 (void *) t
, t
->Name
, baseLevel
);
266 t
->Complete
= GL_FALSE
;
270 /* Check width/height/depth for zero */
271 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
272 t
->Image
[0][baseLevel
]->Height
== 0 ||
273 t
->Image
[0][baseLevel
]->Depth
== 0) {
274 incomplete(t
, "texture width = 0");
275 t
->Complete
= GL_FALSE
;
279 /* Compute _MaxLevel */
280 if ((t
->Target
== GL_TEXTURE_1D
) ||
281 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
282 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
283 maxLevels
= ctx
->Const
.MaxTextureLevels
;
285 else if ((t
->Target
== GL_TEXTURE_2D
) ||
286 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
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 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
370 /* Test 1-D mipmaps */
371 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
372 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
376 if (i
>= minLevel
&& i
<= maxLevel
) {
377 if (!t
->Image
[0][i
]) {
378 t
->Complete
= GL_FALSE
;
379 incomplete(t
, "1D Image[0][i] == NULL");
382 if (t
->Image
[0][i
]->Width2
!= width
) {
383 t
->Complete
= GL_FALSE
;
384 incomplete(t
, "1D Image[0][i] bad width");
389 return; /* found smallest needed mipmap, all done! */
393 else if ((t
->Target
== GL_TEXTURE_2D
) ||
394 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
395 /* Test 2-D mipmaps */
396 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
397 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
398 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
405 if (i
>= minLevel
&& i
<= maxLevel
) {
406 if (!t
->Image
[0][i
]) {
407 t
->Complete
= GL_FALSE
;
408 incomplete(t
, "2D Image[0][i] == NULL");
411 if (t
->Image
[0][i
]->Width2
!= width
) {
412 t
->Complete
= GL_FALSE
;
413 incomplete(t
, "2D Image[0][i] bad width");
416 if (t
->Image
[0][i
]->Height2
!= height
) {
417 t
->Complete
= GL_FALSE
;
418 incomplete(t
, "2D Image[0][i] bad height");
421 if (width
==1 && height
==1) {
422 return; /* found smallest needed mipmap, all done! */
427 else if (t
->Target
== GL_TEXTURE_3D
) {
428 /* Test 3-D mipmaps */
429 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
430 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
431 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
432 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
442 if (i
>= minLevel
&& i
<= maxLevel
) {
443 if (!t
->Image
[0][i
]) {
444 incomplete(t
, "3D Image[0][i] == NULL");
445 t
->Complete
= GL_FALSE
;
448 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
449 t
->Complete
= GL_FALSE
;
450 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
453 if (t
->Image
[0][i
]->Width2
!= width
) {
454 t
->Complete
= GL_FALSE
;
455 incomplete(t
, "3D Image[0][i] bad width");
458 if (t
->Image
[0][i
]->Height2
!= height
) {
459 t
->Complete
= GL_FALSE
;
460 incomplete(t
, "3D Image[0][i] bad height");
463 if (t
->Image
[0][i
]->Depth2
!= depth
) {
464 t
->Complete
= GL_FALSE
;
465 incomplete(t
, "3D Image[0][i] bad depth");
469 if (width
== 1 && height
== 1 && depth
== 1) {
470 return; /* found smallest needed mipmap, all done! */
474 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
475 /* make sure 6 cube faces are consistant */
476 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
477 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
478 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
485 if (i
>= minLevel
&& i
<= maxLevel
) {
487 for (face
= 0; face
< 6; face
++) {
488 /* check that we have images defined */
489 if (!t
->Image
[face
][i
]) {
490 t
->Complete
= GL_FALSE
;
491 incomplete(t
, "CubeMap Image[n][i] == NULL");
494 /* Don't support GL_DEPTH_COMPONENT for cube maps */
495 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
496 t
->Complete
= GL_FALSE
;
497 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
500 /* check that all six images have same size */
501 if (t
->Image
[face
][i
]->Width2
!=width
||
502 t
->Image
[face
][i
]->Height2
!=height
) {
503 t
->Complete
= GL_FALSE
;
504 incomplete(t
, "CubeMap Image[n][i] bad size");
509 if (width
== 1 && height
== 1) {
510 return; /* found smallest needed mipmap, all done! */
514 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
515 /* XXX special checking? */
519 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
527 /***********************************************************************/
528 /** \name API functions */
532 * Texture name generation lock.
534 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
535 * of texture IDs is atomic.
537 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
540 * Generate texture names.
542 * \param n number of texture names to be generated.
543 * \param textures an array in which will hold the generated texture names.
545 * \sa glGenTextures().
547 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
548 * to find a block of free texture IDs which are stored in \p textures.
549 * Corresponding empty texture objects are also generated.
552 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
554 GET_CURRENT_CONTEXT(ctx
);
557 ASSERT_OUTSIDE_BEGIN_END(ctx
);
560 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
568 * This must be atomic (generation and allocation of texture IDs)
570 _glthread_LOCK_MUTEX(GenTexturesLock
);
572 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
574 /* Allocate new, empty texture objects */
575 for (i
= 0; i
< n
; i
++) {
576 struct gl_texture_object
*texObj
;
577 GLuint name
= first
+ i
;
579 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
581 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
582 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
586 /* insert into hash table */
587 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
588 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
589 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
594 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
599 * Check if the given texture object is bound to the current draw or
600 * read framebuffer. If so, Unbind it.
603 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
605 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
608 for (i
= 0; i
< n
; i
++) {
609 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
612 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
613 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
614 fb
->Attachment
[j
].Texture
== texObj
) {
615 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
624 * Check if the given texture object is bound to any texture image units and
626 * XXX all RefCount accesses should be protected by a mutex.
629 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
633 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
634 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
635 struct gl_texture_object
**curr
= NULL
;
637 if (texObj
== unit
->Current1D
) {
638 curr
= &unit
->Current1D
;
639 unit
->Current1D
= ctx
->Shared
->Default1D
;
641 else if (texObj
== unit
->Current2D
) {
642 curr
= &unit
->Current2D
;
643 unit
->Current2D
= ctx
->Shared
->Default2D
;
645 else if (texObj
== unit
->Current3D
) {
646 curr
= &unit
->Current3D
;
647 unit
->Current3D
= ctx
->Shared
->Default3D
;
649 else if (texObj
== unit
->CurrentCubeMap
) {
650 curr
= &unit
->CurrentCubeMap
;
651 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
653 else if (texObj
== unit
->CurrentRect
) {
654 curr
= &unit
->CurrentRect
;
655 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
657 else if (texObj
== unit
->Current1DArray
) {
658 curr
= &unit
->Current1DArray
;
659 unit
->CurrentRect
= ctx
->Shared
->Default1DArray
;
661 else if (texObj
== unit
->Current2DArray
) {
662 curr
= &unit
->Current1DArray
;
663 unit
->CurrentRect
= ctx
->Shared
->Default2DArray
;
669 if (texObj
== unit
->_Current
)
670 unit
->_Current
= *curr
;
677 * Delete named textures.
679 * \param n number of textures to be deleted.
680 * \param textures array of texture IDs to be deleted.
682 * \sa glDeleteTextures().
684 * If we're about to delete a texture that's currently bound to any
685 * texture unit, unbind the texture first. Decrement the reference
686 * count on the texture object and delete it if it's zero.
687 * Recall that texture objects can be shared among several rendering
691 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
693 GET_CURRENT_CONTEXT(ctx
);
695 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
700 for (i
= 0; i
< n
; i
++) {
701 if (textures
[i
] > 0) {
702 struct gl_texture_object
*delObj
703 = _mesa_lookup_texture(ctx
, textures
[i
]);
708 _mesa_lock_texture(ctx
, delObj
);
710 /* Check if texture is bound to any framebuffer objects.
712 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
714 unbind_texobj_from_fbo(ctx
, delObj
);
716 /* Check if this texture is currently bound to any texture units.
717 * If so, unbind it and decrement the reference count.
719 unbind_texobj_from_texunits(ctx
, delObj
);
721 ctx
->NewState
|= _NEW_TEXTURE
;
723 /* The texture _name_ is now free for re-use.
724 * Remove it from the hash table now.
726 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
727 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
728 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
730 /* The actual texture object will not be freed until it's no
731 * longer bound in any context.
732 * XXX all RefCount accesses should be protected by a mutex.
735 deleted
= (delObj
->RefCount
== 0);
736 _mesa_unlock_texture(ctx
, delObj
);
738 /* We know that refcount went to zero above, so this is
739 * the only pointer left to delObj, so we don't have to
740 * worry about locking any more:
743 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objs */
744 ASSERT(ctx
->Driver
.DeleteTexture
);
745 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
754 * Bind a named texture to a texturing target.
756 * \param target texture target.
757 * \param texName texture name.
759 * \sa glBindTexture().
761 * Determines the old texture object bound and returns immediately if rebinding
762 * the same texture. Get the current texture which is either a default texture
763 * if name is null, a named texture from the hash, or a new texture if the
764 * given texture name is new. Increments its reference count, binds it, and
765 * calls dd_function_table::BindTexture. Decrements the old texture reference
766 * count and deletes it if it reaches zero.
769 _mesa_BindTexture( GLenum target
, GLuint texName
)
771 GET_CURRENT_CONTEXT(ctx
);
772 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
773 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
774 struct gl_texture_object
*oldTexObj
;
775 struct gl_texture_object
*newTexObj
= NULL
;
776 ASSERT_OUTSIDE_BEGIN_END(ctx
);
778 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
779 _mesa_debug(ctx
, "glBindTexture %s %d\n",
780 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
783 * Get pointer to currently bound texture object (oldTexObj)
787 oldTexObj
= texUnit
->Current1D
;
790 oldTexObj
= texUnit
->Current2D
;
793 oldTexObj
= texUnit
->Current3D
;
795 case GL_TEXTURE_CUBE_MAP_ARB
:
796 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
797 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
800 oldTexObj
= texUnit
->CurrentCubeMap
;
802 case GL_TEXTURE_RECTANGLE_NV
:
803 if (!ctx
->Extensions
.NV_texture_rectangle
) {
804 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
807 oldTexObj
= texUnit
->CurrentRect
;
809 case GL_TEXTURE_1D_ARRAY_EXT
:
810 if (!ctx
->Extensions
.MESA_texture_array
) {
811 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
814 oldTexObj
= texUnit
->Current1DArray
;
816 case GL_TEXTURE_2D_ARRAY_EXT
:
817 if (!ctx
->Extensions
.MESA_texture_array
) {
818 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
821 oldTexObj
= texUnit
->Current2DArray
;
824 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
828 if (oldTexObj
->Name
== texName
) {
829 /* XXX this might be wrong. If the texobj is in use by another
830 * context and a texobj parameter was changed, this might be our
831 * only chance to update this context's hardware state.
832 * Note that some applications re-bind the same texture a lot so we
833 * want to handle that case quickly.
835 return; /* rebinding the same texture- no change */
839 * Get pointer to new texture object (newTexObj)
842 /* newTexObj = a default texture object */
845 newTexObj
= ctx
->Shared
->Default1D
;
848 newTexObj
= ctx
->Shared
->Default2D
;
851 newTexObj
= ctx
->Shared
->Default3D
;
853 case GL_TEXTURE_CUBE_MAP_ARB
:
854 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
856 case GL_TEXTURE_RECTANGLE_NV
:
857 newTexObj
= ctx
->Shared
->DefaultRect
;
859 case GL_TEXTURE_1D_ARRAY_EXT
:
860 newTexObj
= ctx
->Shared
->Default1DArray
;
862 case GL_TEXTURE_2D_ARRAY_EXT
:
863 newTexObj
= ctx
->Shared
->Default2DArray
;
866 ; /* Bad targets are caught above */
870 /* non-default texture object */
871 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
874 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
875 /* the named texture object's dimensions don't match the target */
876 _mesa_error( ctx
, GL_INVALID_OPERATION
,
877 "glBindTexture(wrong dimensionality)" );
880 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
881 /* have to init wrap and filter state here - kind of klunky */
882 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
883 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
884 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
885 newTexObj
->MinFilter
= GL_LINEAR
;
886 if (ctx
->Driver
.TexParameter
) {
887 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
888 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
889 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
890 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
891 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
892 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
897 /* if this is a new texture id, allocate a texture object now */
898 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
900 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
904 /* and insert it into hash table */
905 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
906 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
907 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
909 newTexObj
->Target
= target
;
912 /* XXX all RefCount accesses should be protected by a mutex. */
913 newTexObj
->RefCount
++;
915 /* do the actual binding, but first flush outstanding vertices:
917 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
921 texUnit
->Current1D
= newTexObj
;
924 texUnit
->Current2D
= newTexObj
;
927 texUnit
->Current3D
= newTexObj
;
929 case GL_TEXTURE_CUBE_MAP_ARB
:
930 texUnit
->CurrentCubeMap
= newTexObj
;
932 case GL_TEXTURE_RECTANGLE_NV
:
933 texUnit
->CurrentRect
= newTexObj
;
935 case GL_TEXTURE_1D_ARRAY_EXT
:
936 texUnit
->Current1DArray
= newTexObj
;
938 case GL_TEXTURE_2D_ARRAY_EXT
:
939 texUnit
->Current2DArray
= newTexObj
;
942 _mesa_problem(ctx
, "bad target in BindTexture");
946 /* Pass BindTexture call to device driver */
947 if (ctx
->Driver
.BindTexture
)
948 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
950 /* Decrement the reference count on the old texture and check if it's
953 /* XXX all RefCount accesses should be protected by a mutex. */
954 oldTexObj
->RefCount
--;
955 ASSERT(oldTexObj
->RefCount
>= 0);
956 if (oldTexObj
->RefCount
== 0) {
957 ASSERT(oldTexObj
->Name
!= 0);
958 ASSERT(ctx
->Driver
.DeleteTexture
);
959 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
965 * Set texture priorities.
967 * \param n number of textures.
968 * \param texName texture names.
969 * \param priorities corresponding texture priorities.
971 * \sa glPrioritizeTextures().
973 * Looks up each texture in the hash, clamps the corresponding priority between
974 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
977 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
978 const GLclampf
*priorities
)
980 GET_CURRENT_CONTEXT(ctx
);
982 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
985 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
992 for (i
= 0; i
< n
; i
++) {
993 if (texName
[i
] > 0) {
994 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
996 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
997 if (ctx
->Driver
.PrioritizeTexture
)
998 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
1003 ctx
->NewState
|= _NEW_TEXTURE
;
1007 * See if textures are loaded in texture memory.
1009 * \param n number of textures to query.
1010 * \param texName array with the texture names.
1011 * \param residences array which will hold the residence status.
1013 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1015 * \sa glAreTexturesResident().
1017 * Looks up each texture in the hash and calls
1018 * dd_function_table::IsTextureResident.
1020 GLboolean GLAPIENTRY
1021 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1022 GLboolean
*residences
)
1024 GET_CURRENT_CONTEXT(ctx
);
1025 GLboolean allResident
= GL_TRUE
;
1027 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1030 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1034 if (!texName
|| !residences
)
1037 for (i
= 0; i
< n
; i
++) {
1038 struct gl_texture_object
*t
;
1039 if (texName
[i
] == 0) {
1040 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1043 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1045 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1048 if (!ctx
->Driver
.IsTextureResident
||
1049 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1050 /* The texture is resident */
1052 residences
[i
] = GL_TRUE
;
1055 /* The texture is not resident */
1057 allResident
= GL_FALSE
;
1058 for (j
= 0; j
< i
; j
++)
1059 residences
[j
] = GL_TRUE
;
1061 residences
[i
] = GL_FALSE
;
1069 * See if a name corresponds to a texture.
1071 * \param texture texture name.
1073 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1076 * \sa glIsTexture().
1078 * Calls _mesa_HashLookup().
1080 GLboolean GLAPIENTRY
1081 _mesa_IsTexture( GLuint texture
)
1083 struct gl_texture_object
*t
;
1084 GET_CURRENT_CONTEXT(ctx
);
1085 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1090 t
= _mesa_lookup_texture(ctx
, texture
);
1092 /* IsTexture is true only after object has been bound once. */
1093 return t
&& t
->Target
;
1098 * Simplest implementation of texture locking: Grab the a new mutex in
1099 * the shared context. Examine the shared context state timestamp and
1100 * if there has been a change, set the appropriate bits in
1103 * This is used to deal with synchronizing things when a texture object
1104 * is used/modified by different contexts (or threads) which are sharing
1107 * See also _mesa_lock/unlock_texture() in teximage.h
1110 _mesa_lock_context_textures( GLcontext
*ctx
)
1112 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1114 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1115 ctx
->NewState
|= _NEW_TEXTURE
;
1116 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1122 _mesa_unlock_context_textures( GLcontext
*ctx
)
1124 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1125 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);