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.
32 #if FEATURE_colortable
47 /**********************************************************************/
48 /** \name Internal functions */
53 * Return the gl_texture_object for a given ID.
55 struct gl_texture_object
*
56 _mesa_lookup_texture(GLcontext
*ctx
, GLuint id
)
58 return (struct gl_texture_object
*)
59 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
65 * Allocate and initialize a new texture object. But don't put it into the
66 * texture object hash table.
68 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * \param shared the shared GL state structure to contain the texture object
72 * \param name integer name for the texture object
73 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
74 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * \return pointer to new texture object.
79 struct gl_texture_object
*
80 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
82 struct gl_texture_object
*obj
;
84 obj
= MALLOC_STRUCT(gl_texture_object
);
85 _mesa_initialize_texture_object(obj
, name
, target
);
91 * Initialize a new texture object to default values.
92 * \param obj the texture object
93 * \param name the texture name
94 * \param target the texture target
97 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
98 GLuint name
, GLenum target
)
100 ASSERT(target
== 0 ||
101 target
== GL_TEXTURE_1D
||
102 target
== GL_TEXTURE_2D
||
103 target
== GL_TEXTURE_3D
||
104 target
== GL_TEXTURE_CUBE_MAP_ARB
||
105 target
== GL_TEXTURE_RECTANGLE_NV
||
106 target
== GL_TEXTURE_1D_ARRAY_EXT
||
107 target
== GL_TEXTURE_2D_ARRAY_EXT
);
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 */
145 * Some texture initialization can't be finished until we know which
146 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
149 finish_texture_init(GLcontext
*ctx
, GLenum target
,
150 struct gl_texture_object
*obj
)
152 assert(obj
->Target
== 0);
154 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
155 /* have to init wrap and filter state here - kind of klunky */
156 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
157 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
158 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
159 obj
->MinFilter
= GL_LINEAR
;
160 if (ctx
->Driver
.TexParameter
) {
161 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
162 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
163 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
164 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
165 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
166 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
173 * Deallocate a texture object struct. It should have already been
174 * removed from the texture object pool.
175 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
177 * \param shared the shared GL state to which the object belongs.
178 * \param texOjb the texture object to delete.
181 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
187 /* Set Target to an invalid value. With some assertions elsewhere
188 * we can try to detect possible use of deleted textures.
190 texObj
->Target
= 0x99;
192 #if FEATURE_colortable
193 _mesa_free_colortable_data(&texObj
->Palette
);
196 /* free the texture images */
197 for (face
= 0; face
< 6; face
++) {
198 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
199 if (texObj
->Image
[face
][i
]) {
200 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
205 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
206 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
208 /* free this object */
216 * Copy texture object state from one texture object to another.
217 * Use for glPush/PopAttrib.
219 * \param dest destination texture object.
220 * \param src source texture object.
223 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
224 const struct gl_texture_object
*src
)
226 dest
->Target
= src
->Target
;
227 dest
->Name
= src
->Name
;
228 dest
->Priority
= src
->Priority
;
229 dest
->BorderColor
[0] = src
->BorderColor
[0];
230 dest
->BorderColor
[1] = src
->BorderColor
[1];
231 dest
->BorderColor
[2] = src
->BorderColor
[2];
232 dest
->BorderColor
[3] = src
->BorderColor
[3];
233 dest
->WrapS
= src
->WrapS
;
234 dest
->WrapT
= src
->WrapT
;
235 dest
->WrapR
= src
->WrapR
;
236 dest
->MinFilter
= src
->MinFilter
;
237 dest
->MagFilter
= src
->MagFilter
;
238 dest
->MinLod
= src
->MinLod
;
239 dest
->MaxLod
= src
->MaxLod
;
240 dest
->LodBias
= src
->LodBias
;
241 dest
->BaseLevel
= src
->BaseLevel
;
242 dest
->MaxLevel
= src
->MaxLevel
;
243 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
244 dest
->CompareFlag
= src
->CompareFlag
;
245 dest
->CompareOperator
= src
->CompareOperator
;
246 dest
->ShadowAmbient
= src
->ShadowAmbient
;
247 dest
->CompareMode
= src
->CompareMode
;
248 dest
->CompareFunc
= src
->CompareFunc
;
249 dest
->DepthMode
= src
->DepthMode
;
250 dest
->_MaxLevel
= src
->_MaxLevel
;
251 dest
->_MaxLambda
= src
->_MaxLambda
;
252 dest
->GenerateMipmap
= src
->GenerateMipmap
;
253 dest
->Palette
= src
->Palette
;
254 dest
->_Complete
= src
->_Complete
;
259 * Check if the given texture object is valid by examining its Target field.
260 * For debugging only.
263 valid_texture_object(const struct gl_texture_object
*tex
)
265 switch (tex
->Target
) {
270 case GL_TEXTURE_CUBE_MAP_ARB
:
271 case GL_TEXTURE_RECTANGLE_NV
:
272 case GL_TEXTURE_1D_ARRAY_EXT
:
273 case GL_TEXTURE_2D_ARRAY_EXT
:
276 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
279 _mesa_problem(NULL
, "invalid texture object Target value");
286 * Reference (or unreference) a texture object.
287 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
288 * If 'tex' is non-null, increment its refcount.
291 _mesa_reference_texobj(struct gl_texture_object
**ptr
,
292 struct gl_texture_object
*tex
)
301 /* Unreference the old texture */
302 GLboolean deleteFlag
= GL_FALSE
;
303 struct gl_texture_object
*oldTex
= *ptr
;
305 assert(valid_texture_object(oldTex
));
307 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
308 ASSERT(oldTex
->RefCount
> 0);
311 deleteFlag
= (oldTex
->RefCount
== 0);
312 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
315 GET_CURRENT_CONTEXT(ctx
);
317 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
319 _mesa_problem(NULL
, "Unable to delete texture, no context");
327 /* reference new texture */
328 assert(valid_texture_object(tex
));
329 _glthread_LOCK_MUTEX(tex
->Mutex
);
330 if (tex
->RefCount
== 0) {
331 /* this texture's being deleted (look just above) */
332 /* Not sure this can every really happen. Warn if it does. */
333 _mesa_problem(NULL
, "referencing deleted texture object");
340 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
347 * Report why a texture object is incomplete.
349 * \param t texture object.
350 * \param why string describing why it's incomplete.
352 * \note For debug purposes only.
356 incomplete(const struct gl_texture_object
*t
, const char *why
)
358 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
361 #define incomplete(t, why)
366 * Examine a texture object to determine if it is complete.
368 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
371 * \param ctx GL context.
372 * \param t texture object.
374 * According to the texture target, verifies that each of the mipmaps is
375 * present and has the expected size.
378 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
379 struct gl_texture_object
*t
)
381 const GLint baseLevel
= t
->BaseLevel
;
382 GLint maxLog2
= 0, maxLevels
= 0;
384 t
->_Complete
= GL_TRUE
; /* be optimistic */
386 /* Always need the base level image */
387 if (!t
->Image
[0][baseLevel
]) {
389 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
390 (void *) t
, t
->Name
, baseLevel
);
392 t
->_Complete
= GL_FALSE
;
396 /* Check width/height/depth for zero */
397 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
398 t
->Image
[0][baseLevel
]->Height
== 0 ||
399 t
->Image
[0][baseLevel
]->Depth
== 0) {
400 incomplete(t
, "texture width = 0");
401 t
->_Complete
= GL_FALSE
;
405 /* Compute _MaxLevel */
406 if ((t
->Target
== GL_TEXTURE_1D
) ||
407 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
408 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
409 maxLevels
= ctx
->Const
.MaxTextureLevels
;
411 else if ((t
->Target
== GL_TEXTURE_2D
) ||
412 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
413 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
414 t
->Image
[0][baseLevel
]->HeightLog2
);
415 maxLevels
= ctx
->Const
.MaxTextureLevels
;
417 else if (t
->Target
== GL_TEXTURE_3D
) {
418 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
419 t
->Image
[0][baseLevel
]->HeightLog2
);
420 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
421 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
423 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
424 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
425 t
->Image
[0][baseLevel
]->HeightLog2
);
426 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
428 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
429 maxLog2
= 0; /* not applicable */
430 maxLevels
= 1; /* no mipmapping */
433 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
437 ASSERT(maxLevels
> 0);
439 t
->_MaxLevel
= baseLevel
+ maxLog2
;
440 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
441 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
443 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
444 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
446 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
447 /* make sure that all six cube map level 0 images are the same size */
448 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
449 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
451 for (face
= 1; face
< 6; face
++) {
452 if (t
->Image
[face
][baseLevel
] == NULL
||
453 t
->Image
[face
][baseLevel
]->Width2
!= w
||
454 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
455 t
->_Complete
= GL_FALSE
;
456 incomplete(t
, "Non-quare cubemap image");
462 /* extra checking for mipmaps */
463 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
465 * Mipmapping: determine if we have a complete set of mipmaps
468 GLint minLevel
= baseLevel
;
469 GLint maxLevel
= t
->_MaxLevel
;
471 if (minLevel
> maxLevel
) {
472 t
->_Complete
= GL_FALSE
;
473 incomplete(t
, "minLevel > maxLevel");
477 /* Test dimension-independent attributes */
478 for (i
= minLevel
; i
<= maxLevel
; i
++) {
479 if (t
->Image
[0][i
]) {
480 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
481 t
->_Complete
= GL_FALSE
;
482 incomplete(t
, "Format[i] != Format[baseLevel]");
485 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
486 t
->_Complete
= GL_FALSE
;
487 incomplete(t
, "Border[i] != Border[baseLevel]");
493 /* Test things which depend on number of texture image dimensions */
494 if ((t
->Target
== GL_TEXTURE_1D
) ||
495 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
496 /* Test 1-D mipmaps */
497 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
498 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
502 if (i
>= minLevel
&& i
<= maxLevel
) {
503 if (!t
->Image
[0][i
]) {
504 t
->_Complete
= GL_FALSE
;
505 incomplete(t
, "1D Image[0][i] == NULL");
508 if (t
->Image
[0][i
]->Width2
!= width
) {
509 t
->_Complete
= GL_FALSE
;
510 incomplete(t
, "1D Image[0][i] bad width");
515 return; /* found smallest needed mipmap, all done! */
519 else if ((t
->Target
== GL_TEXTURE_2D
) ||
520 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
521 /* Test 2-D mipmaps */
522 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
523 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
524 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
531 if (i
>= minLevel
&& i
<= maxLevel
) {
532 if (!t
->Image
[0][i
]) {
533 t
->_Complete
= GL_FALSE
;
534 incomplete(t
, "2D Image[0][i] == NULL");
537 if (t
->Image
[0][i
]->Width2
!= width
) {
538 t
->_Complete
= GL_FALSE
;
539 incomplete(t
, "2D Image[0][i] bad width");
542 if (t
->Image
[0][i
]->Height2
!= height
) {
543 t
->_Complete
= GL_FALSE
;
544 incomplete(t
, "2D Image[0][i] bad height");
547 if (width
==1 && height
==1) {
548 return; /* found smallest needed mipmap, all done! */
553 else if (t
->Target
== GL_TEXTURE_3D
) {
554 /* Test 3-D mipmaps */
555 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
556 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
557 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
558 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
568 if (i
>= minLevel
&& i
<= maxLevel
) {
569 if (!t
->Image
[0][i
]) {
570 incomplete(t
, "3D Image[0][i] == NULL");
571 t
->_Complete
= GL_FALSE
;
574 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
575 t
->_Complete
= GL_FALSE
;
576 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
579 if (t
->Image
[0][i
]->Width2
!= width
) {
580 t
->_Complete
= GL_FALSE
;
581 incomplete(t
, "3D Image[0][i] bad width");
584 if (t
->Image
[0][i
]->Height2
!= height
) {
585 t
->_Complete
= GL_FALSE
;
586 incomplete(t
, "3D Image[0][i] bad height");
589 if (t
->Image
[0][i
]->Depth2
!= depth
) {
590 t
->_Complete
= GL_FALSE
;
591 incomplete(t
, "3D Image[0][i] bad depth");
595 if (width
== 1 && height
== 1 && depth
== 1) {
596 return; /* found smallest needed mipmap, all done! */
600 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
601 /* make sure 6 cube faces are consistant */
602 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
603 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
604 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
611 if (i
>= minLevel
&& i
<= maxLevel
) {
613 for (face
= 0; face
< 6; face
++) {
614 /* check that we have images defined */
615 if (!t
->Image
[face
][i
]) {
616 t
->_Complete
= GL_FALSE
;
617 incomplete(t
, "CubeMap Image[n][i] == NULL");
620 /* Don't support GL_DEPTH_COMPONENT for cube maps */
621 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
622 t
->_Complete
= GL_FALSE
;
623 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
626 /* check that all six images have same size */
627 if (t
->Image
[face
][i
]->Width2
!=width
||
628 t
->Image
[face
][i
]->Height2
!=height
) {
629 t
->_Complete
= GL_FALSE
;
630 incomplete(t
, "CubeMap Image[n][i] bad size");
635 if (width
== 1 && height
== 1) {
636 return; /* found smallest needed mipmap, all done! */
640 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
641 /* XXX special checking? */
645 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
653 /***********************************************************************/
654 /** \name API functions */
659 * Generate texture names.
661 * \param n number of texture names to be generated.
662 * \param textures an array in which will hold the generated texture names.
664 * \sa glGenTextures().
666 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
667 * IDs which are stored in \p textures. Corresponding empty texture
668 * objects are also generated.
671 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
673 GET_CURRENT_CONTEXT(ctx
);
676 ASSERT_OUTSIDE_BEGIN_END(ctx
);
679 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
687 * This must be atomic (generation and allocation of texture IDs)
689 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
691 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
693 /* Allocate new, empty texture objects */
694 for (i
= 0; i
< n
; i
++) {
695 struct gl_texture_object
*texObj
;
696 GLuint name
= first
+ i
;
698 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
700 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
701 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
705 /* insert into hash table */
706 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
711 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
716 * Check if the given texture object is bound to the current draw or
717 * read framebuffer. If so, Unbind it.
720 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
722 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
725 for (i
= 0; i
< n
; i
++) {
726 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
729 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
730 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
731 fb
->Attachment
[j
].Texture
== texObj
) {
732 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
741 * Check if the given texture object is bound to any texture image units and
742 * unbind it if so (revert to default textures).
745 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
749 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
750 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
751 if (texObj
== unit
->Current1D
) {
752 _mesa_reference_texobj(&unit
->Current1D
, ctx
->Shared
->Default1D
);
754 else if (texObj
== unit
->Current2D
) {
755 _mesa_reference_texobj(&unit
->Current2D
, ctx
->Shared
->Default2D
);
757 else if (texObj
== unit
->Current3D
) {
758 _mesa_reference_texobj(&unit
->Current3D
, ctx
->Shared
->Default3D
);
760 else if (texObj
== unit
->CurrentCubeMap
) {
761 _mesa_reference_texobj(&unit
->CurrentCubeMap
, ctx
->Shared
->DefaultCubeMap
);
763 else if (texObj
== unit
->CurrentRect
) {
764 _mesa_reference_texobj(&unit
->CurrentRect
, ctx
->Shared
->DefaultRect
);
766 else if (texObj
== unit
->Current1DArray
) {
767 _mesa_reference_texobj(&unit
->Current1DArray
, ctx
->Shared
->Default1DArray
);
769 else if (texObj
== unit
->Current2DArray
) {
770 _mesa_reference_texobj(&unit
->Current2DArray
, ctx
->Shared
->Default2DArray
);
777 * Delete named textures.
779 * \param n number of textures to be deleted.
780 * \param textures array of texture IDs to be deleted.
782 * \sa glDeleteTextures().
784 * If we're about to delete a texture that's currently bound to any
785 * texture unit, unbind the texture first. Decrement the reference
786 * count on the texture object and delete it if it's zero.
787 * Recall that texture objects can be shared among several rendering
791 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
793 GET_CURRENT_CONTEXT(ctx
);
795 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
800 for (i
= 0; i
< n
; i
++) {
801 if (textures
[i
] > 0) {
802 struct gl_texture_object
*delObj
803 = _mesa_lookup_texture(ctx
, textures
[i
]);
806 _mesa_lock_texture(ctx
, delObj
);
808 /* Check if texture is bound to any framebuffer objects.
810 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
812 unbind_texobj_from_fbo(ctx
, delObj
);
814 /* Check if this texture is currently bound to any texture units.
817 unbind_texobj_from_texunits(ctx
, delObj
);
819 _mesa_unlock_texture(ctx
, delObj
);
821 ctx
->NewState
|= _NEW_TEXTURE
;
823 /* The texture _name_ is now free for re-use.
824 * Remove it from the hash table now.
826 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
827 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
828 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
830 /* Unreference the texobj. If refcount hits zero, the texture
833 _mesa_reference_texobj(&delObj
, NULL
);
841 * Bind a named texture to a texturing target.
843 * \param target texture target.
844 * \param texName texture name.
846 * \sa glBindTexture().
848 * Determines the old texture object bound and returns immediately if rebinding
849 * the same texture. Get the current texture which is either a default texture
850 * if name is null, a named texture from the hash, or a new texture if the
851 * given texture name is new. Increments its reference count, binds it, and
852 * calls dd_function_table::BindTexture. Decrements the old texture reference
853 * count and deletes it if it reaches zero.
856 _mesa_BindTexture( GLenum target
, GLuint texName
)
858 GET_CURRENT_CONTEXT(ctx
);
859 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
860 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
861 struct gl_texture_object
*newTexObj
= NULL
, *defaultTexObj
= NULL
;
862 ASSERT_OUTSIDE_BEGIN_END(ctx
);
864 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
865 _mesa_debug(ctx
, "glBindTexture %s %d\n",
866 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
870 defaultTexObj
= ctx
->Shared
->Default1D
;
873 defaultTexObj
= ctx
->Shared
->Default2D
;
876 defaultTexObj
= ctx
->Shared
->Default3D
;
878 case GL_TEXTURE_CUBE_MAP_ARB
:
879 defaultTexObj
= ctx
->Shared
->DefaultCubeMap
;
881 case GL_TEXTURE_RECTANGLE_NV
:
882 defaultTexObj
= ctx
->Shared
->DefaultRect
;
884 case GL_TEXTURE_1D_ARRAY_EXT
:
885 defaultTexObj
= ctx
->Shared
->Default1DArray
;
887 case GL_TEXTURE_2D_ARRAY_EXT
:
888 defaultTexObj
= ctx
->Shared
->Default2DArray
;
891 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
896 * Get pointer to new texture object (newTexObj)
899 newTexObj
= defaultTexObj
;
902 /* non-default texture object */
903 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
906 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
907 /* the named texture object's target doesn't match the given target */
908 _mesa_error( ctx
, GL_INVALID_OPERATION
,
909 "glBindTexture(target mismatch)" );
912 if (newTexObj
->Target
== 0) {
913 finish_texture_init(ctx
, target
, newTexObj
);
917 /* if this is a new texture id, allocate a texture object now */
918 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
924 /* and insert it into hash table */
925 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
926 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
927 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
929 newTexObj
->Target
= target
;
932 assert(valid_texture_object(newTexObj
));
934 /* flush before changing binding */
935 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
937 /* Do the actual binding. The refcount on the previously bound
938 * texture object will be decremented. It'll be deleted if the
943 _mesa_reference_texobj(&texUnit
->Current1D
, newTexObj
);
946 _mesa_reference_texobj(&texUnit
->Current2D
, newTexObj
);
949 _mesa_reference_texobj(&texUnit
->Current3D
, newTexObj
);
951 case GL_TEXTURE_CUBE_MAP_ARB
:
952 _mesa_reference_texobj(&texUnit
->CurrentCubeMap
, newTexObj
);
954 case GL_TEXTURE_RECTANGLE_NV
:
955 _mesa_reference_texobj(&texUnit
->CurrentRect
, newTexObj
);
957 case GL_TEXTURE_1D_ARRAY_EXT
:
958 texUnit
->Current1DArray
= newTexObj
;
960 case GL_TEXTURE_2D_ARRAY_EXT
:
961 texUnit
->Current2DArray
= newTexObj
;
964 /* Bad target should be caught above */
965 _mesa_problem(ctx
, "bad target in BindTexture");
969 /* Pass BindTexture call to device driver */
970 if (ctx
->Driver
.BindTexture
)
971 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
976 * Set texture priorities.
978 * \param n number of textures.
979 * \param texName texture names.
980 * \param priorities corresponding texture priorities.
982 * \sa glPrioritizeTextures().
984 * Looks up each texture in the hash, clamps the corresponding priority between
985 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
988 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
989 const GLclampf
*priorities
)
991 GET_CURRENT_CONTEXT(ctx
);
993 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
996 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1003 for (i
= 0; i
< n
; i
++) {
1004 if (texName
[i
] > 0) {
1005 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1007 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1008 if (ctx
->Driver
.PrioritizeTexture
)
1009 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
1014 ctx
->NewState
|= _NEW_TEXTURE
;
1018 * See if textures are loaded in texture memory.
1020 * \param n number of textures to query.
1021 * \param texName array with the texture names.
1022 * \param residences array which will hold the residence status.
1024 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1026 * \sa glAreTexturesResident().
1028 * Looks up each texture in the hash and calls
1029 * dd_function_table::IsTextureResident.
1031 GLboolean GLAPIENTRY
1032 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1033 GLboolean
*residences
)
1035 GET_CURRENT_CONTEXT(ctx
);
1036 GLboolean allResident
= GL_TRUE
;
1038 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1041 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1045 if (!texName
|| !residences
)
1048 for (i
= 0; i
< n
; i
++) {
1049 struct gl_texture_object
*t
;
1050 if (texName
[i
] == 0) {
1051 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1054 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1056 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1059 if (!ctx
->Driver
.IsTextureResident
||
1060 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1061 /* The texture is resident */
1063 residences
[i
] = GL_TRUE
;
1066 /* The texture is not resident */
1068 allResident
= GL_FALSE
;
1069 for (j
= 0; j
< i
; j
++)
1070 residences
[j
] = GL_TRUE
;
1072 residences
[i
] = GL_FALSE
;
1080 * See if a name corresponds to a texture.
1082 * \param texture texture name.
1084 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1087 * \sa glIsTexture().
1089 * Calls _mesa_HashLookup().
1091 GLboolean GLAPIENTRY
1092 _mesa_IsTexture( GLuint texture
)
1094 struct gl_texture_object
*t
;
1095 GET_CURRENT_CONTEXT(ctx
);
1096 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1101 t
= _mesa_lookup_texture(ctx
, texture
);
1103 /* IsTexture is true only after object has been bound once. */
1104 return t
&& t
->Target
;
1109 * Simplest implementation of texture locking: Grab the a new mutex in
1110 * the shared context. Examine the shared context state timestamp and
1111 * if there has been a change, set the appropriate bits in
1114 * This is used to deal with synchronizing things when a texture object
1115 * is used/modified by different contexts (or threads) which are sharing
1118 * See also _mesa_lock/unlock_texture() in teximage.h
1121 _mesa_lock_context_textures( GLcontext
*ctx
)
1123 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1125 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1126 ctx
->NewState
|= _NEW_TEXTURE
;
1127 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1133 _mesa_unlock_context_textures( GLcontext
*ctx
)
1135 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1136 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);