3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2007 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 */
143 * Deallocate a texture object struct. It should have already been
144 * removed from the texture object pool.
145 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
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 /* Set Target to an invalid value. With some assertions elsewhere
158 * we can try to detect possible use of deleted textures.
160 texObj
->Target
= 0x99;
162 _mesa_free_colortable_data(&texObj
->Palette
);
164 /* free the texture images */
165 for (face
= 0; face
< 6; face
++) {
166 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
167 if (texObj
->Image
[face
][i
]) {
168 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
173 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
174 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
176 /* free this object */
184 * Copy texture object state from one texture object to another.
185 * Use for glPush/PopAttrib.
187 * \param dest destination texture object.
188 * \param src source texture object.
191 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
192 const struct gl_texture_object
*src
)
194 dest
->Target
= src
->Target
;
195 dest
->Name
= src
->Name
;
196 dest
->Priority
= src
->Priority
;
197 dest
->BorderColor
[0] = src
->BorderColor
[0];
198 dest
->BorderColor
[1] = src
->BorderColor
[1];
199 dest
->BorderColor
[2] = src
->BorderColor
[2];
200 dest
->BorderColor
[3] = src
->BorderColor
[3];
201 dest
->WrapS
= src
->WrapS
;
202 dest
->WrapT
= src
->WrapT
;
203 dest
->WrapR
= src
->WrapR
;
204 dest
->MinFilter
= src
->MinFilter
;
205 dest
->MagFilter
= src
->MagFilter
;
206 dest
->MinLod
= src
->MinLod
;
207 dest
->MaxLod
= src
->MaxLod
;
208 dest
->LodBias
= src
->LodBias
;
209 dest
->BaseLevel
= src
->BaseLevel
;
210 dest
->MaxLevel
= src
->MaxLevel
;
211 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
212 dest
->CompareFlag
= src
->CompareFlag
;
213 dest
->CompareOperator
= src
->CompareOperator
;
214 dest
->ShadowAmbient
= src
->ShadowAmbient
;
215 dest
->CompareMode
= src
->CompareMode
;
216 dest
->CompareFunc
= src
->CompareFunc
;
217 dest
->DepthMode
= src
->DepthMode
;
218 dest
->_MaxLevel
= src
->_MaxLevel
;
219 dest
->_MaxLambda
= src
->_MaxLambda
;
220 dest
->GenerateMipmap
= src
->GenerateMipmap
;
221 dest
->Palette
= src
->Palette
;
222 dest
->_Complete
= src
->_Complete
;
227 * Check if the given texture object is valid by examining its Target field.
228 * For debugging only.
231 valid_texture_object(const struct gl_texture_object
*tex
)
233 switch (tex
->Target
) {
238 case GL_TEXTURE_CUBE_MAP_ARB
:
239 case GL_TEXTURE_RECTANGLE_NV
:
240 case GL_TEXTURE_1D_ARRAY_EXT
:
241 case GL_TEXTURE_2D_ARRAY_EXT
:
244 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
247 _mesa_problem(NULL
, "invalid texture object Target value");
254 * Reference (or unreference) a texture object.
255 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
256 * If 'tex' is non-null, increment its refcount.
259 _mesa_reference_texobj(struct gl_texture_object
**ptr
,
260 struct gl_texture_object
*tex
)
269 /* Unreference the old texture */
270 GLboolean deleteFlag
= GL_FALSE
;
271 struct gl_texture_object
*oldTex
= *ptr
;
273 assert(valid_texture_object(oldTex
));
275 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
276 ASSERT(oldTex
->RefCount
> 0);
279 deleteFlag
= (oldTex
->RefCount
== 0);
280 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
283 GET_CURRENT_CONTEXT(ctx
);
285 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
287 _mesa_problem(NULL
, "Unable to delete texture, no context");
295 /* reference new texture */
296 assert(valid_texture_object(tex
));
297 _glthread_LOCK_MUTEX(tex
->Mutex
);
298 if (tex
->RefCount
== 0) {
299 /* this texture's being deleted (look just above) */
300 /* Not sure this can every really happen. Warn if it does. */
301 _mesa_problem(NULL
, "referencing deleted texture object");
308 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
315 * Report why a texture object is incomplete.
317 * \param t texture object.
318 * \param why string describing why it's incomplete.
320 * \note For debug purposes only.
324 incomplete(const struct gl_texture_object
*t
, const char *why
)
326 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
329 #define incomplete(t, why)
334 * Examine a texture object to determine if it is complete.
336 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
339 * \param ctx GL context.
340 * \param t texture object.
342 * According to the texture target, verifies that each of the mipmaps is
343 * present and has the expected size.
346 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
347 struct gl_texture_object
*t
)
349 const GLint baseLevel
= t
->BaseLevel
;
350 GLint maxLog2
= 0, maxLevels
= 0;
352 t
->_Complete
= GL_TRUE
; /* be optimistic */
354 /* Always need the base level image */
355 if (!t
->Image
[0][baseLevel
]) {
357 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
358 (void *) t
, t
->Name
, baseLevel
);
360 t
->_Complete
= GL_FALSE
;
364 /* Check width/height/depth for zero */
365 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
366 t
->Image
[0][baseLevel
]->Height
== 0 ||
367 t
->Image
[0][baseLevel
]->Depth
== 0) {
368 incomplete(t
, "texture width = 0");
369 t
->_Complete
= GL_FALSE
;
373 /* Compute _MaxLevel */
374 if ((t
->Target
== GL_TEXTURE_1D
) ||
375 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
376 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
377 maxLevels
= ctx
->Const
.MaxTextureLevels
;
379 else if ((t
->Target
== GL_TEXTURE_2D
) ||
380 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
381 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
382 t
->Image
[0][baseLevel
]->HeightLog2
);
383 maxLevels
= ctx
->Const
.MaxTextureLevels
;
385 else if (t
->Target
== GL_TEXTURE_3D
) {
386 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
387 t
->Image
[0][baseLevel
]->HeightLog2
);
388 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
389 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
391 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
392 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
393 t
->Image
[0][baseLevel
]->HeightLog2
);
394 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
396 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
397 maxLog2
= 0; /* not applicable */
398 maxLevels
= 1; /* no mipmapping */
401 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
405 ASSERT(maxLevels
> 0);
407 t
->_MaxLevel
= baseLevel
+ maxLog2
;
408 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
409 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
411 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
412 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
414 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
415 /* make sure that all six cube map level 0 images are the same size */
416 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
417 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
419 for (face
= 1; face
< 6; face
++) {
420 if (t
->Image
[face
][baseLevel
] == NULL
||
421 t
->Image
[face
][baseLevel
]->Width2
!= w
||
422 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
423 t
->_Complete
= GL_FALSE
;
424 incomplete(t
, "Non-quare cubemap image");
430 /* extra checking for mipmaps */
431 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
433 * Mipmapping: determine if we have a complete set of mipmaps
436 GLint minLevel
= baseLevel
;
437 GLint maxLevel
= t
->_MaxLevel
;
439 if (minLevel
> maxLevel
) {
440 t
->_Complete
= GL_FALSE
;
441 incomplete(t
, "minLevel > maxLevel");
445 /* Test dimension-independent attributes */
446 for (i
= minLevel
; i
<= maxLevel
; i
++) {
447 if (t
->Image
[0][i
]) {
448 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
449 t
->_Complete
= GL_FALSE
;
450 incomplete(t
, "Format[i] != Format[baseLevel]");
453 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
454 t
->_Complete
= GL_FALSE
;
455 incomplete(t
, "Border[i] != Border[baseLevel]");
461 /* Test things which depend on number of texture image dimensions */
462 if ((t
->Target
== GL_TEXTURE_1D
) ||
463 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
464 /* Test 1-D mipmaps */
465 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
466 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
470 if (i
>= minLevel
&& i
<= maxLevel
) {
471 if (!t
->Image
[0][i
]) {
472 t
->_Complete
= GL_FALSE
;
473 incomplete(t
, "1D Image[0][i] == NULL");
476 if (t
->Image
[0][i
]->Width2
!= width
) {
477 t
->_Complete
= GL_FALSE
;
478 incomplete(t
, "1D Image[0][i] bad width");
483 return; /* found smallest needed mipmap, all done! */
487 else if ((t
->Target
== GL_TEXTURE_2D
) ||
488 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
489 /* Test 2-D mipmaps */
490 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
491 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
492 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
499 if (i
>= minLevel
&& i
<= maxLevel
) {
500 if (!t
->Image
[0][i
]) {
501 t
->_Complete
= GL_FALSE
;
502 incomplete(t
, "2D Image[0][i] == NULL");
505 if (t
->Image
[0][i
]->Width2
!= width
) {
506 t
->_Complete
= GL_FALSE
;
507 incomplete(t
, "2D Image[0][i] bad width");
510 if (t
->Image
[0][i
]->Height2
!= height
) {
511 t
->_Complete
= GL_FALSE
;
512 incomplete(t
, "2D Image[0][i] bad height");
515 if (width
==1 && height
==1) {
516 return; /* found smallest needed mipmap, all done! */
521 else if (t
->Target
== GL_TEXTURE_3D
) {
522 /* Test 3-D mipmaps */
523 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
524 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
525 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
526 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
536 if (i
>= minLevel
&& i
<= maxLevel
) {
537 if (!t
->Image
[0][i
]) {
538 incomplete(t
, "3D Image[0][i] == NULL");
539 t
->_Complete
= GL_FALSE
;
542 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
543 t
->_Complete
= GL_FALSE
;
544 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
547 if (t
->Image
[0][i
]->Width2
!= width
) {
548 t
->_Complete
= GL_FALSE
;
549 incomplete(t
, "3D Image[0][i] bad width");
552 if (t
->Image
[0][i
]->Height2
!= height
) {
553 t
->_Complete
= GL_FALSE
;
554 incomplete(t
, "3D Image[0][i] bad height");
557 if (t
->Image
[0][i
]->Depth2
!= depth
) {
558 t
->_Complete
= GL_FALSE
;
559 incomplete(t
, "3D Image[0][i] bad depth");
563 if (width
== 1 && height
== 1 && depth
== 1) {
564 return; /* found smallest needed mipmap, all done! */
568 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
569 /* make sure 6 cube faces are consistant */
570 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
571 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
572 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
579 if (i
>= minLevel
&& i
<= maxLevel
) {
581 for (face
= 0; face
< 6; face
++) {
582 /* check that we have images defined */
583 if (!t
->Image
[face
][i
]) {
584 t
->_Complete
= GL_FALSE
;
585 incomplete(t
, "CubeMap Image[n][i] == NULL");
588 /* Don't support GL_DEPTH_COMPONENT for cube maps */
589 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
590 t
->_Complete
= GL_FALSE
;
591 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
594 /* check that all six images have same size */
595 if (t
->Image
[face
][i
]->Width2
!=width
||
596 t
->Image
[face
][i
]->Height2
!=height
) {
597 t
->_Complete
= GL_FALSE
;
598 incomplete(t
, "CubeMap Image[n][i] bad size");
603 if (width
== 1 && height
== 1) {
604 return; /* found smallest needed mipmap, all done! */
608 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
609 /* XXX special checking? */
613 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
621 /***********************************************************************/
622 /** \name API functions */
627 * Generate texture names.
629 * \param n number of texture names to be generated.
630 * \param textures an array in which will hold the generated texture names.
632 * \sa glGenTextures().
634 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
635 * IDs which are stored in \p textures. Corresponding empty texture
636 * objects are also generated.
639 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
641 GET_CURRENT_CONTEXT(ctx
);
644 ASSERT_OUTSIDE_BEGIN_END(ctx
);
647 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
655 * This must be atomic (generation and allocation of texture IDs)
657 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
659 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
661 /* Allocate new, empty texture objects */
662 for (i
= 0; i
< n
; i
++) {
663 struct gl_texture_object
*texObj
;
664 GLuint name
= first
+ i
;
666 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
668 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
673 /* insert into hash table */
674 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
679 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
684 * Check if the given texture object is bound to the current draw or
685 * read framebuffer. If so, Unbind it.
688 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
690 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
693 for (i
= 0; i
< n
; i
++) {
694 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
697 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
698 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
699 fb
->Attachment
[j
].Texture
== texObj
) {
700 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
709 * Check if the given texture object is bound to any texture image units and
710 * unbind it if so (revert to default textures).
713 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
717 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
718 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
719 if (texObj
== unit
->Current1D
) {
720 _mesa_reference_texobj(&unit
->Current1D
, ctx
->Shared
->Default1D
);
722 else if (texObj
== unit
->Current2D
) {
723 _mesa_reference_texobj(&unit
->Current2D
, ctx
->Shared
->Default2D
);
725 else if (texObj
== unit
->Current3D
) {
726 _mesa_reference_texobj(&unit
->Current3D
, ctx
->Shared
->Default3D
);
728 else if (texObj
== unit
->CurrentCubeMap
) {
729 _mesa_reference_texobj(&unit
->CurrentCubeMap
, ctx
->Shared
->DefaultCubeMap
);
731 else if (texObj
== unit
->CurrentRect
) {
732 _mesa_reference_texobj(&unit
->CurrentRect
, ctx
->Shared
->DefaultRect
);
734 else if (texObj
== unit
->Current1DArray
) {
735 _mesa_reference_texobj(&unit
->Current1DArray
, ctx
->Shared
->Default1DArray
);
737 else if (texObj
== unit
->Current2DArray
) {
738 _mesa_reference_texobj(&unit
->Current2DArray
, ctx
->Shared
->Default2DArray
);
745 * Delete named textures.
747 * \param n number of textures to be deleted.
748 * \param textures array of texture IDs to be deleted.
750 * \sa glDeleteTextures().
752 * If we're about to delete a texture that's currently bound to any
753 * texture unit, unbind the texture first. Decrement the reference
754 * count on the texture object and delete it if it's zero.
755 * Recall that texture objects can be shared among several rendering
759 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
761 GET_CURRENT_CONTEXT(ctx
);
763 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
768 for (i
= 0; i
< n
; i
++) {
769 if (textures
[i
] > 0) {
770 struct gl_texture_object
*delObj
771 = _mesa_lookup_texture(ctx
, textures
[i
]);
774 _mesa_lock_texture(ctx
, delObj
);
776 /* Check if texture is bound to any framebuffer objects.
778 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
780 unbind_texobj_from_fbo(ctx
, delObj
);
782 /* Check if this texture is currently bound to any texture units.
785 unbind_texobj_from_texunits(ctx
, delObj
);
787 _mesa_unlock_texture(ctx
, delObj
);
789 ctx
->NewState
|= _NEW_TEXTURE
;
791 /* The texture _name_ is now free for re-use.
792 * Remove it from the hash table now.
794 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
795 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
796 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
798 /* Unreference the texobj. If refcount hits zero, the texture
801 _mesa_reference_texobj(&delObj
, NULL
);
809 * Bind a named texture to a texturing target.
811 * \param target texture target.
812 * \param texName texture name.
814 * \sa glBindTexture().
816 * Determines the old texture object bound and returns immediately if rebinding
817 * the same texture. Get the current texture which is either a default texture
818 * if name is null, a named texture from the hash, or a new texture if the
819 * given texture name is new. Increments its reference count, binds it, and
820 * calls dd_function_table::BindTexture. Decrements the old texture reference
821 * count and deletes it if it reaches zero.
824 _mesa_BindTexture( GLenum target
, GLuint texName
)
826 GET_CURRENT_CONTEXT(ctx
);
827 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
828 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
829 struct gl_texture_object
*newTexObj
= NULL
;
830 ASSERT_OUTSIDE_BEGIN_END(ctx
);
832 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
833 _mesa_debug(ctx
, "glBindTexture %s %d\n",
834 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
837 * Get pointer to new texture object (newTexObj)
840 /* newTexObj = a default texture object */
843 newTexObj
= ctx
->Shared
->Default1D
;
846 newTexObj
= ctx
->Shared
->Default2D
;
849 newTexObj
= ctx
->Shared
->Default3D
;
851 case GL_TEXTURE_CUBE_MAP_ARB
:
852 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
854 case GL_TEXTURE_RECTANGLE_NV
:
855 newTexObj
= ctx
->Shared
->DefaultRect
;
857 case GL_TEXTURE_1D_ARRAY_EXT
:
858 newTexObj
= ctx
->Shared
->Default1DArray
;
860 case GL_TEXTURE_2D_ARRAY_EXT
:
861 newTexObj
= ctx
->Shared
->Default2DArray
;
864 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
869 /* non-default texture object */
870 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
873 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
874 /* the named texture object's dimensions don't match the target */
875 _mesa_error( ctx
, GL_INVALID_OPERATION
,
876 "glBindTexture(wrong dimensionality)" );
879 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
880 /* have to init wrap and filter state here - kind of klunky */
881 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
882 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
883 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
884 newTexObj
->MinFilter
= GL_LINEAR
;
885 if (ctx
->Driver
.TexParameter
) {
886 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
887 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
888 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
889 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
890 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
891 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
896 /* if this is a new texture id, allocate a texture object now */
897 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
899 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
903 /* and insert it into hash table */
904 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
905 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
906 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
908 newTexObj
->Target
= target
;
911 assert(valid_texture_object(newTexObj
));
913 /* flush before changing binding */
914 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
916 /* Do the actual binding. The refcount on the previously bound
917 * texture object will be decremented. It'll be deleted if the
922 _mesa_reference_texobj(&texUnit
->Current1D
, newTexObj
);
925 _mesa_reference_texobj(&texUnit
->Current2D
, newTexObj
);
928 _mesa_reference_texobj(&texUnit
->Current3D
, newTexObj
);
930 case GL_TEXTURE_CUBE_MAP_ARB
:
931 _mesa_reference_texobj(&texUnit
->CurrentCubeMap
, newTexObj
);
933 case GL_TEXTURE_RECTANGLE_NV
:
934 _mesa_reference_texobj(&texUnit
->CurrentRect
, newTexObj
);
936 case GL_TEXTURE_1D_ARRAY_EXT
:
937 texUnit
->Current1DArray
= newTexObj
;
939 case GL_TEXTURE_2D_ARRAY_EXT
:
940 texUnit
->Current2DArray
= newTexObj
;
943 /* Bad target should be caught above */
944 _mesa_problem(ctx
, "bad target in BindTexture");
948 /* Pass BindTexture call to device driver */
949 if (ctx
->Driver
.BindTexture
)
950 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
955 * Set texture priorities.
957 * \param n number of textures.
958 * \param texName texture names.
959 * \param priorities corresponding texture priorities.
961 * \sa glPrioritizeTextures().
963 * Looks up each texture in the hash, clamps the corresponding priority between
964 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
967 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
968 const GLclampf
*priorities
)
970 GET_CURRENT_CONTEXT(ctx
);
972 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
975 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
982 for (i
= 0; i
< n
; i
++) {
983 if (texName
[i
] > 0) {
984 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
986 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
987 if (ctx
->Driver
.PrioritizeTexture
)
988 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
993 ctx
->NewState
|= _NEW_TEXTURE
;
997 * See if textures are loaded in texture memory.
999 * \param n number of textures to query.
1000 * \param texName array with the texture names.
1001 * \param residences array which will hold the residence status.
1003 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1005 * \sa glAreTexturesResident().
1007 * Looks up each texture in the hash and calls
1008 * dd_function_table::IsTextureResident.
1010 GLboolean GLAPIENTRY
1011 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1012 GLboolean
*residences
)
1014 GET_CURRENT_CONTEXT(ctx
);
1015 GLboolean allResident
= GL_TRUE
;
1017 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1020 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1024 if (!texName
|| !residences
)
1027 for (i
= 0; i
< n
; i
++) {
1028 struct gl_texture_object
*t
;
1029 if (texName
[i
] == 0) {
1030 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1033 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1035 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1038 if (!ctx
->Driver
.IsTextureResident
||
1039 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1040 /* The texture is resident */
1042 residences
[i
] = GL_TRUE
;
1045 /* The texture is not resident */
1047 allResident
= GL_FALSE
;
1048 for (j
= 0; j
< i
; j
++)
1049 residences
[j
] = GL_TRUE
;
1051 residences
[i
] = GL_FALSE
;
1059 * See if a name corresponds to a texture.
1061 * \param texture texture name.
1063 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1066 * \sa glIsTexture().
1068 * Calls _mesa_HashLookup().
1070 GLboolean GLAPIENTRY
1071 _mesa_IsTexture( GLuint texture
)
1073 struct gl_texture_object
*t
;
1074 GET_CURRENT_CONTEXT(ctx
);
1075 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1080 t
= _mesa_lookup_texture(ctx
, texture
);
1082 /* IsTexture is true only after object has been bound once. */
1083 return t
&& t
->Target
;
1088 * Simplest implementation of texture locking: Grab the a new mutex in
1089 * the shared context. Examine the shared context state timestamp and
1090 * if there has been a change, set the appropriate bits in
1093 * This is used to deal with synchronizing things when a texture object
1094 * is used/modified by different contexts (or threads) which are sharing
1097 * See also _mesa_lock/unlock_texture() in teximage.h
1100 _mesa_lock_context_textures( GLcontext
*ctx
)
1102 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1104 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1105 ctx
->NewState
|= _NEW_TEXTURE
;
1106 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1112 _mesa_unlock_context_textures( GLcontext
*ctx
)
1114 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1115 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);