55dce60d675f5e66bb7ef18158a25578e451cd6f
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 */
111 obj
->Target
= target
;
112 obj
->Priority
= 1.0F
;
113 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
114 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
115 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
116 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
117 obj
->MinFilter
= GL_LINEAR
;
120 obj
->WrapS
= GL_REPEAT
;
121 obj
->WrapT
= GL_REPEAT
;
122 obj
->WrapR
= GL_REPEAT
;
123 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
125 obj
->MagFilter
= GL_LINEAR
;
126 obj
->MinLod
= -1000.0;
127 obj
->MaxLod
= 1000.0;
130 obj
->MaxLevel
= 1000;
131 obj
->MaxAnisotropy
= 1.0;
132 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
133 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
134 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
135 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
136 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
137 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
142 * Deallocate a texture object struct. It should have already been
143 * removed from the texture object pool.
145 * \param shared the shared GL state to which the object belongs.
146 * \param texOjb the texture object to delete.
149 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
155 _mesa_free_colortable_data(&texObj
->Palette
);
157 /* free the texture images */
158 for (face
= 0; face
< 6; face
++) {
159 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
160 if (texObj
->Image
[face
][i
]) {
161 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
166 /* free this object */
174 * Copy texture object state from one texture object to another.
175 * Use for glPush/PopAttrib.
177 * \param dest destination texture object.
178 * \param src source texture object.
181 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
182 const struct gl_texture_object
*src
)
184 dest
->Name
= src
->Name
;
185 dest
->Priority
= src
->Priority
;
186 dest
->BorderColor
[0] = src
->BorderColor
[0];
187 dest
->BorderColor
[1] = src
->BorderColor
[1];
188 dest
->BorderColor
[2] = src
->BorderColor
[2];
189 dest
->BorderColor
[3] = src
->BorderColor
[3];
190 dest
->WrapS
= src
->WrapS
;
191 dest
->WrapT
= src
->WrapT
;
192 dest
->WrapR
= src
->WrapR
;
193 dest
->MinFilter
= src
->MinFilter
;
194 dest
->MagFilter
= src
->MagFilter
;
195 dest
->MinLod
= src
->MinLod
;
196 dest
->MaxLod
= src
->MaxLod
;
197 dest
->LodBias
= src
->LodBias
;
198 dest
->BaseLevel
= src
->BaseLevel
;
199 dest
->MaxLevel
= src
->MaxLevel
;
200 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
201 dest
->CompareFlag
= src
->CompareFlag
;
202 dest
->CompareOperator
= src
->CompareOperator
;
203 dest
->ShadowAmbient
= src
->ShadowAmbient
;
204 dest
->CompareMode
= src
->CompareMode
;
205 dest
->CompareFunc
= src
->CompareFunc
;
206 dest
->DepthMode
= src
->DepthMode
;
207 dest
->_MaxLevel
= src
->_MaxLevel
;
208 dest
->_MaxLambda
= src
->_MaxLambda
;
209 dest
->GenerateMipmap
= src
->GenerateMipmap
;
210 dest
->Palette
= src
->Palette
;
211 dest
->Complete
= src
->Complete
;
216 * Report why a texture object is incomplete.
218 * \param t texture object.
219 * \param why string describing why it's incomplete.
221 * \note For debug purposes only.
225 incomplete(const struct gl_texture_object
*t
, const char *why
)
227 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
230 #define incomplete(t, why)
235 * Examine a texture object to determine if it is complete.
237 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
240 * \param ctx GL context.
241 * \param t texture object.
243 * According to the texture target, verifies that each of the mipmaps is
244 * present and has the expected size.
247 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
248 struct gl_texture_object
*t
)
250 const GLint baseLevel
= t
->BaseLevel
;
251 GLint maxLog2
= 0, maxLevels
= 0;
253 t
->Complete
= GL_TRUE
; /* be optimistic */
255 /* Always need the base level image */
256 if (!t
->Image
[0][baseLevel
]) {
258 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
259 (void *) t
, t
->Name
, baseLevel
);
261 t
->Complete
= GL_FALSE
;
265 /* Check width/height/depth for zero */
266 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
267 t
->Image
[0][baseLevel
]->Height
== 0 ||
268 t
->Image
[0][baseLevel
]->Depth
== 0) {
269 incomplete(t
, "texture width = 0");
270 t
->Complete
= GL_FALSE
;
274 /* Compute _MaxLevel */
275 if ((t
->Target
== GL_TEXTURE_1D
) ||
276 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
277 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
278 maxLevels
= ctx
->Const
.MaxTextureLevels
;
280 else if ((t
->Target
== GL_TEXTURE_2D
) ||
281 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
282 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
283 t
->Image
[0][baseLevel
]->HeightLog2
);
284 maxLevels
= ctx
->Const
.MaxTextureLevels
;
286 else if (t
->Target
== GL_TEXTURE_3D
) {
287 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
288 t
->Image
[0][baseLevel
]->HeightLog2
);
289 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
290 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
292 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
293 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
294 t
->Image
[0][baseLevel
]->HeightLog2
);
295 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
297 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
298 maxLog2
= 0; /* not applicable */
299 maxLevels
= 1; /* no mipmapping */
302 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
306 ASSERT(maxLevels
> 0);
308 t
->_MaxLevel
= baseLevel
+ maxLog2
;
309 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
310 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
312 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
313 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
315 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
316 /* make sure that all six cube map level 0 images are the same size */
317 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
318 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
320 for (face
= 1; face
< 6; face
++) {
321 if (t
->Image
[face
][baseLevel
] == NULL
||
322 t
->Image
[face
][baseLevel
]->Width2
!= w
||
323 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
324 t
->Complete
= GL_FALSE
;
325 incomplete(t
, "Non-quare cubemap image");
331 /* extra checking for mipmaps */
332 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
334 * Mipmapping: determine if we have a complete set of mipmaps
337 GLint minLevel
= baseLevel
;
338 GLint maxLevel
= t
->_MaxLevel
;
340 if (minLevel
> maxLevel
) {
341 t
->Complete
= GL_FALSE
;
342 incomplete(t
, "minLevel > maxLevel");
346 /* Test dimension-independent attributes */
347 for (i
= minLevel
; i
<= maxLevel
; i
++) {
348 if (t
->Image
[0][i
]) {
349 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
350 t
->Complete
= GL_FALSE
;
351 incomplete(t
, "Format[i] != Format[baseLevel]");
354 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
355 t
->Complete
= GL_FALSE
;
356 incomplete(t
, "Border[i] != Border[baseLevel]");
362 /* Test things which depend on number of texture image dimensions */
363 if ((t
->Target
== GL_TEXTURE_1D
) ||
364 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
365 /* Test 1-D mipmaps */
366 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
367 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
371 if (i
>= minLevel
&& i
<= maxLevel
) {
372 if (!t
->Image
[0][i
]) {
373 t
->Complete
= GL_FALSE
;
374 incomplete(t
, "1D Image[0][i] == NULL");
377 if (t
->Image
[0][i
]->Width2
!= width
) {
378 t
->Complete
= GL_FALSE
;
379 incomplete(t
, "1D Image[0][i] bad width");
384 return; /* found smallest needed mipmap, all done! */
388 else if ((t
->Target
== GL_TEXTURE_2D
) ||
389 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
390 /* Test 2-D mipmaps */
391 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
392 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
393 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
400 if (i
>= minLevel
&& i
<= maxLevel
) {
401 if (!t
->Image
[0][i
]) {
402 t
->Complete
= GL_FALSE
;
403 incomplete(t
, "2D Image[0][i] == NULL");
406 if (t
->Image
[0][i
]->Width2
!= width
) {
407 t
->Complete
= GL_FALSE
;
408 incomplete(t
, "2D Image[0][i] bad width");
411 if (t
->Image
[0][i
]->Height2
!= height
) {
412 t
->Complete
= GL_FALSE
;
413 incomplete(t
, "2D Image[0][i] bad height");
416 if (width
==1 && height
==1) {
417 return; /* found smallest needed mipmap, all done! */
422 else if (t
->Target
== GL_TEXTURE_3D
) {
423 /* Test 3-D mipmaps */
424 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
425 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
426 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
427 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
437 if (i
>= minLevel
&& i
<= maxLevel
) {
438 if (!t
->Image
[0][i
]) {
439 incomplete(t
, "3D Image[0][i] == NULL");
440 t
->Complete
= GL_FALSE
;
443 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
444 t
->Complete
= GL_FALSE
;
445 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
448 if (t
->Image
[0][i
]->Width2
!= width
) {
449 t
->Complete
= GL_FALSE
;
450 incomplete(t
, "3D Image[0][i] bad width");
453 if (t
->Image
[0][i
]->Height2
!= height
) {
454 t
->Complete
= GL_FALSE
;
455 incomplete(t
, "3D Image[0][i] bad height");
458 if (t
->Image
[0][i
]->Depth2
!= depth
) {
459 t
->Complete
= GL_FALSE
;
460 incomplete(t
, "3D Image[0][i] bad depth");
464 if (width
== 1 && height
== 1 && depth
== 1) {
465 return; /* found smallest needed mipmap, all done! */
469 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
470 /* make sure 6 cube faces are consistant */
471 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
472 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
473 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
480 if (i
>= minLevel
&& i
<= maxLevel
) {
482 for (face
= 0; face
< 6; face
++) {
483 /* check that we have images defined */
484 if (!t
->Image
[face
][i
]) {
485 t
->Complete
= GL_FALSE
;
486 incomplete(t
, "CubeMap Image[n][i] == NULL");
489 /* Don't support GL_DEPTH_COMPONENT for cube maps */
490 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
491 t
->Complete
= GL_FALSE
;
492 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
495 /* check that all six images have same size */
496 if (t
->Image
[face
][i
]->Width2
!=width
||
497 t
->Image
[face
][i
]->Height2
!=height
) {
498 t
->Complete
= GL_FALSE
;
499 incomplete(t
, "CubeMap Image[n][i] bad size");
504 if (width
== 1 && height
== 1) {
505 return; /* found smallest needed mipmap, all done! */
509 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
510 /* XXX special checking? */
514 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
522 /***********************************************************************/
523 /** \name API functions */
527 * Texture name generation lock.
529 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
530 * of texture IDs is atomic.
532 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
535 * Generate texture names.
537 * \param n number of texture names to be generated.
538 * \param textures an array in which will hold the generated texture names.
540 * \sa glGenTextures().
542 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
543 * to find a block of free texture IDs which are stored in \p textures.
544 * Corresponding empty texture objects are also generated.
547 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
549 GET_CURRENT_CONTEXT(ctx
);
552 ASSERT_OUTSIDE_BEGIN_END(ctx
);
555 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
563 * This must be atomic (generation and allocation of texture IDs)
565 _glthread_LOCK_MUTEX(GenTexturesLock
);
567 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
569 /* Allocate new, empty texture objects */
570 for (i
= 0; i
< n
; i
++) {
571 struct gl_texture_object
*texObj
;
572 GLuint name
= first
+ i
;
574 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
576 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
577 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
581 /* insert into hash table */
582 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
583 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
584 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
589 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
594 * Check if the given texture object is bound to the current draw or
595 * read framebuffer. If so, Unbind it.
598 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
600 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
603 for (i
= 0; i
< n
; i
++) {
604 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
607 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
608 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
609 fb
->Attachment
[j
].Texture
== texObj
) {
610 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
619 * Check if the given texture object is bound to any texture image units and
621 * XXX all RefCount accesses should be protected by a mutex.
624 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
628 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
629 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
630 struct gl_texture_object
**curr
= NULL
;
632 if (texObj
== unit
->Current1D
) {
633 curr
= &unit
->Current1D
;
634 unit
->Current1D
= ctx
->Shared
->Default1D
;
636 else if (texObj
== unit
->Current2D
) {
637 curr
= &unit
->Current2D
;
638 unit
->Current2D
= ctx
->Shared
->Default2D
;
640 else if (texObj
== unit
->Current3D
) {
641 curr
= &unit
->Current3D
;
642 unit
->Current3D
= ctx
->Shared
->Default3D
;
644 else if (texObj
== unit
->CurrentCubeMap
) {
645 curr
= &unit
->CurrentCubeMap
;
646 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
648 else if (texObj
== unit
->CurrentRect
) {
649 curr
= &unit
->CurrentRect
;
650 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
652 else if (texObj
== unit
->Current1DArray
) {
653 curr
= &unit
->Current1DArray
;
654 unit
->CurrentRect
= ctx
->Shared
->Default1DArray
;
656 else if (texObj
== unit
->Current2DArray
) {
657 curr
= &unit
->Current1DArray
;
658 unit
->CurrentRect
= ctx
->Shared
->Default2DArray
;
664 if (texObj
== unit
->_Current
)
665 unit
->_Current
= *curr
;
672 * Delete named textures.
674 * \param n number of textures to be deleted.
675 * \param textures array of texture IDs to be deleted.
677 * \sa glDeleteTextures().
679 * If we're about to delete a texture that's currently bound to any
680 * texture unit, unbind the texture first. Decrement the reference
681 * count on the texture object and delete it if it's zero.
682 * Recall that texture objects can be shared among several rendering
686 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
688 GET_CURRENT_CONTEXT(ctx
);
690 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
695 for (i
= 0; i
< n
; i
++) {
696 if (textures
[i
] > 0) {
697 struct gl_texture_object
*delObj
698 = _mesa_lookup_texture(ctx
, textures
[i
]);
703 _mesa_lock_texture(ctx
, delObj
);
705 /* Check if texture is bound to any framebuffer objects.
707 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
709 unbind_texobj_from_fbo(ctx
, delObj
);
711 /* Check if this texture is currently bound to any texture units.
712 * If so, unbind it and decrement the reference count.
714 unbind_texobj_from_texunits(ctx
, delObj
);
716 ctx
->NewState
|= _NEW_TEXTURE
;
718 /* The texture _name_ is now free for re-use.
719 * Remove it from the hash table now.
721 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
722 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
723 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
725 /* The actual texture object will not be freed until it's no
726 * longer bound in any context.
727 * XXX all RefCount accesses should be protected by a mutex.
730 deleted
= (delObj
->RefCount
== 0);
731 _mesa_unlock_texture(ctx
, delObj
);
733 /* We know that refcount went to zero above, so this is
734 * the only pointer left to delObj, so we don't have to
735 * worry about locking any more:
738 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objs */
739 ASSERT(ctx
->Driver
.DeleteTexture
);
740 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
749 * Bind a named texture to a texturing target.
751 * \param target texture target.
752 * \param texName texture name.
754 * \sa glBindTexture().
756 * Determines the old texture object bound and returns immediately if rebinding
757 * the same texture. Get the current texture which is either a default texture
758 * if name is null, a named texture from the hash, or a new texture if the
759 * given texture name is new. Increments its reference count, binds it, and
760 * calls dd_function_table::BindTexture. Decrements the old texture reference
761 * count and deletes it if it reaches zero.
764 _mesa_BindTexture( GLenum target
, GLuint texName
)
766 GET_CURRENT_CONTEXT(ctx
);
767 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
768 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
769 struct gl_texture_object
*oldTexObj
;
770 struct gl_texture_object
*newTexObj
= NULL
;
771 ASSERT_OUTSIDE_BEGIN_END(ctx
);
773 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
774 _mesa_debug(ctx
, "glBindTexture %s %d\n",
775 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
778 * Get pointer to currently bound texture object (oldTexObj)
782 oldTexObj
= texUnit
->Current1D
;
785 oldTexObj
= texUnit
->Current2D
;
788 oldTexObj
= texUnit
->Current3D
;
790 case GL_TEXTURE_CUBE_MAP_ARB
:
791 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
792 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
795 oldTexObj
= texUnit
->CurrentCubeMap
;
797 case GL_TEXTURE_RECTANGLE_NV
:
798 if (!ctx
->Extensions
.NV_texture_rectangle
) {
799 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
802 oldTexObj
= texUnit
->CurrentRect
;
804 case GL_TEXTURE_1D_ARRAY_EXT
:
805 if (!ctx
->Extensions
.MESA_texture_array
) {
806 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
809 oldTexObj
= texUnit
->Current1DArray
;
811 case GL_TEXTURE_2D_ARRAY_EXT
:
812 if (!ctx
->Extensions
.MESA_texture_array
) {
813 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
816 oldTexObj
= texUnit
->Current2DArray
;
819 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
823 if (oldTexObj
->Name
== texName
) {
824 /* XXX this might be wrong. If the texobj is in use by another
825 * context and a texobj parameter was changed, this might be our
826 * only chance to update this context's hardware state.
827 * Note that some applications re-bind the same texture a lot so we
828 * want to handle that case quickly.
830 return; /* rebinding the same texture- no change */
834 * Get pointer to new texture object (newTexObj)
837 /* newTexObj = a default texture object */
840 newTexObj
= ctx
->Shared
->Default1D
;
843 newTexObj
= ctx
->Shared
->Default2D
;
846 newTexObj
= ctx
->Shared
->Default3D
;
848 case GL_TEXTURE_CUBE_MAP_ARB
:
849 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
851 case GL_TEXTURE_RECTANGLE_NV
:
852 newTexObj
= ctx
->Shared
->DefaultRect
;
854 case GL_TEXTURE_1D_ARRAY_EXT
:
855 newTexObj
= ctx
->Shared
->Default1DArray
;
857 case GL_TEXTURE_2D_ARRAY_EXT
:
858 newTexObj
= ctx
->Shared
->Default2DArray
;
861 ; /* Bad targets are caught above */
865 /* non-default texture object */
866 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
869 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
870 /* the named texture object's dimensions don't match the target */
871 _mesa_error( ctx
, GL_INVALID_OPERATION
,
872 "glBindTexture(wrong dimensionality)" );
875 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
876 /* have to init wrap and filter state here - kind of klunky */
877 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
878 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
879 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
880 newTexObj
->MinFilter
= GL_LINEAR
;
881 if (ctx
->Driver
.TexParameter
) {
882 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
883 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
884 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
885 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
886 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
887 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
892 /* if this is a new texture id, allocate a texture object now */
893 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
895 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
899 /* and insert it into hash table */
900 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
901 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
902 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
904 newTexObj
->Target
= target
;
907 /* XXX all RefCount accesses should be protected by a mutex. */
908 newTexObj
->RefCount
++;
910 /* do the actual binding, but first flush outstanding vertices:
912 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
916 texUnit
->Current1D
= newTexObj
;
919 texUnit
->Current2D
= newTexObj
;
922 texUnit
->Current3D
= newTexObj
;
924 case GL_TEXTURE_CUBE_MAP_ARB
:
925 texUnit
->CurrentCubeMap
= newTexObj
;
927 case GL_TEXTURE_RECTANGLE_NV
:
928 texUnit
->CurrentRect
= newTexObj
;
930 case GL_TEXTURE_1D_ARRAY_EXT
:
931 texUnit
->Current1DArray
= newTexObj
;
933 case GL_TEXTURE_2D_ARRAY_EXT
:
934 texUnit
->Current2DArray
= newTexObj
;
937 _mesa_problem(ctx
, "bad target in BindTexture");
941 /* Pass BindTexture call to device driver */
942 if (ctx
->Driver
.BindTexture
)
943 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
945 /* Decrement the reference count on the old texture and check if it's
948 /* XXX all RefCount accesses should be protected by a mutex. */
949 oldTexObj
->RefCount
--;
950 ASSERT(oldTexObj
->RefCount
>= 0);
951 if (oldTexObj
->RefCount
== 0) {
952 ASSERT(oldTexObj
->Name
!= 0);
953 ASSERT(ctx
->Driver
.DeleteTexture
);
954 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
960 * Set texture priorities.
962 * \param n number of textures.
963 * \param texName texture names.
964 * \param priorities corresponding texture priorities.
966 * \sa glPrioritizeTextures().
968 * Looks up each texture in the hash, clamps the corresponding priority between
969 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
972 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
973 const GLclampf
*priorities
)
975 GET_CURRENT_CONTEXT(ctx
);
977 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
980 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
987 for (i
= 0; i
< n
; i
++) {
988 if (texName
[i
] > 0) {
989 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
991 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
992 if (ctx
->Driver
.PrioritizeTexture
)
993 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
998 ctx
->NewState
|= _NEW_TEXTURE
;
1002 * See if textures are loaded in texture memory.
1004 * \param n number of textures to query.
1005 * \param texName array with the texture names.
1006 * \param residences array which will hold the residence status.
1008 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1010 * \sa glAreTexturesResident().
1012 * Looks up each texture in the hash and calls
1013 * dd_function_table::IsTextureResident.
1015 GLboolean GLAPIENTRY
1016 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1017 GLboolean
*residences
)
1019 GET_CURRENT_CONTEXT(ctx
);
1020 GLboolean allResident
= GL_TRUE
;
1022 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1025 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1029 if (!texName
|| !residences
)
1032 for (i
= 0; i
< n
; i
++) {
1033 struct gl_texture_object
*t
;
1034 if (texName
[i
] == 0) {
1035 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1038 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1040 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1043 if (!ctx
->Driver
.IsTextureResident
||
1044 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1045 /* The texture is resident */
1047 residences
[i
] = GL_TRUE
;
1050 /* The texture is not resident */
1052 allResident
= GL_FALSE
;
1053 for (j
= 0; j
< i
; j
++)
1054 residences
[j
] = GL_TRUE
;
1056 residences
[i
] = GL_FALSE
;
1064 * See if a name corresponds to a texture.
1066 * \param texture texture name.
1068 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1071 * \sa glIsTexture().
1073 * Calls _mesa_HashLookup().
1075 GLboolean GLAPIENTRY
1076 _mesa_IsTexture( GLuint texture
)
1078 struct gl_texture_object
*t
;
1079 GET_CURRENT_CONTEXT(ctx
);
1080 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1085 t
= _mesa_lookup_texture(ctx
, texture
);
1087 /* IsTexture is true only after object has been bound once. */
1088 return t
&& t
->Target
;
1093 * Simplest implementation of texture locking: Grab the a new mutex in
1094 * the shared context. Examine the shared context state timestamp and
1095 * if there has been a change, set the appropriate bits in
1098 * This is used to deal with synchronizing things when a texture object
1099 * is used/modified by different contexts (or threads) which are sharing
1102 * See also _mesa_lock/unlock_texture() in teximage.h
1105 _mesa_lock_context_textures( GLcontext
*ctx
)
1107 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1109 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1110 ctx
->NewState
|= _NEW_TEXTURE
;
1111 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1117 _mesa_unlock_context_textures( GLcontext
*ctx
)
1119 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1120 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);