ac70e5a22ec0e1bfc105ce1c4e7763da03137f5c
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 */
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
)
156 printf("TEX DELETE %p (%u)\n", (void*) texObj, texObj->Name);
161 /* Set Target to an invalid value. With some assertions elsewhere
162 * we can try to detect possible use of deleted textures.
164 texObj
->Target
= 0x99;
166 _mesa_free_colortable_data(&texObj
->Palette
);
168 /* free the texture images */
169 for (face
= 0; face
< 6; face
++) {
170 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
171 if (texObj
->Image
[face
][i
]) {
172 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
177 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
178 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
180 /* free this object */
188 * Copy texture object state from one texture object to another.
189 * Use for glPush/PopAttrib.
191 * \param dest destination texture object.
192 * \param src source texture object.
195 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
196 const struct gl_texture_object
*src
)
198 dest
->Name
= src
->Name
;
199 dest
->Priority
= src
->Priority
;
200 dest
->BorderColor
[0] = src
->BorderColor
[0];
201 dest
->BorderColor
[1] = src
->BorderColor
[1];
202 dest
->BorderColor
[2] = src
->BorderColor
[2];
203 dest
->BorderColor
[3] = src
->BorderColor
[3];
204 dest
->WrapS
= src
->WrapS
;
205 dest
->WrapT
= src
->WrapT
;
206 dest
->WrapR
= src
->WrapR
;
207 dest
->MinFilter
= src
->MinFilter
;
208 dest
->MagFilter
= src
->MagFilter
;
209 dest
->MinLod
= src
->MinLod
;
210 dest
->MaxLod
= src
->MaxLod
;
211 dest
->LodBias
= src
->LodBias
;
212 dest
->BaseLevel
= src
->BaseLevel
;
213 dest
->MaxLevel
= src
->MaxLevel
;
214 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
215 dest
->CompareFlag
= src
->CompareFlag
;
216 dest
->CompareOperator
= src
->CompareOperator
;
217 dest
->ShadowAmbient
= src
->ShadowAmbient
;
218 dest
->CompareMode
= src
->CompareMode
;
219 dest
->CompareFunc
= src
->CompareFunc
;
220 dest
->DepthMode
= src
->DepthMode
;
221 dest
->_MaxLevel
= src
->_MaxLevel
;
222 dest
->_MaxLambda
= src
->_MaxLambda
;
223 dest
->GenerateMipmap
= src
->GenerateMipmap
;
224 dest
->Palette
= src
->Palette
;
225 dest
->_Complete
= src
->_Complete
;
230 * Check if the given texture object is valid by examining its Target field.
231 * For debugging only.
234 valid_texture_object(const struct gl_texture_object
*tex
)
236 switch (tex
->Target
) {
241 case GL_TEXTURE_CUBE_MAP_ARB
:
242 case GL_TEXTURE_RECTANGLE_NV
:
243 case GL_TEXTURE_1D_ARRAY_EXT
:
244 case GL_TEXTURE_2D_ARRAY_EXT
:
247 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
250 _mesa_problem(NULL
, "invalid texture object Target value");
257 * Reference (or unreference) a texture object.
258 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
259 * If 'tex' is non-null, increment its refcount.
262 _mesa_reference_texobj(struct gl_texture_object
**ptr
,
263 struct gl_texture_object
*tex
)
272 /* Unreference the old texture */
273 GLboolean deleteFlag
= GL_FALSE
;
274 struct gl_texture_object
*oldTex
= *ptr
;
276 assert(valid_texture_object(oldTex
));
278 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
279 ASSERT(oldTex
->RefCount
> 0);
282 printf("TEX DECR %p (%u) to %d\n",
283 (void*) oldTex, oldTex->Name, oldTex->RefCount);
285 deleteFlag
= (oldTex
->RefCount
== 0);
286 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
289 GET_CURRENT_CONTEXT(ctx
);
290 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
298 /* reference new texture */
299 assert(valid_texture_object(tex
));
300 _glthread_LOCK_MUTEX(tex
->Mutex
);
301 if (tex
->RefCount
== 0) {
302 /* this texture's being deleted (look just above) */
303 /* Not sure this can every really happen. Warn if it does. */
304 _mesa_problem(NULL
, "referencing deleted texture object");
310 printf("TEX INCR %p (%u) to %d\n",
311 (void*) tex, tex->Name, tex->RefCount);
315 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
322 * Report why a texture object is incomplete.
324 * \param t texture object.
325 * \param why string describing why it's incomplete.
327 * \note For debug purposes only.
331 incomplete(const struct gl_texture_object
*t
, const char *why
)
333 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
336 #define incomplete(t, why)
341 * Examine a texture object to determine if it is complete.
343 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
346 * \param ctx GL context.
347 * \param t texture object.
349 * According to the texture target, verifies that each of the mipmaps is
350 * present and has the expected size.
353 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
354 struct gl_texture_object
*t
)
356 const GLint baseLevel
= t
->BaseLevel
;
357 GLint maxLog2
= 0, maxLevels
= 0;
359 t
->_Complete
= GL_TRUE
; /* be optimistic */
361 /* Always need the base level image */
362 if (!t
->Image
[0][baseLevel
]) {
364 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
365 (void *) t
, t
->Name
, baseLevel
);
367 t
->_Complete
= GL_FALSE
;
371 /* Check width/height/depth for zero */
372 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
373 t
->Image
[0][baseLevel
]->Height
== 0 ||
374 t
->Image
[0][baseLevel
]->Depth
== 0) {
375 incomplete(t
, "texture width = 0");
376 t
->_Complete
= GL_FALSE
;
380 /* Compute _MaxLevel */
381 if ((t
->Target
== GL_TEXTURE_1D
) ||
382 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
383 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
384 maxLevels
= ctx
->Const
.MaxTextureLevels
;
386 else if ((t
->Target
== GL_TEXTURE_2D
) ||
387 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
388 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
389 t
->Image
[0][baseLevel
]->HeightLog2
);
390 maxLevels
= ctx
->Const
.MaxTextureLevels
;
392 else if (t
->Target
== GL_TEXTURE_3D
) {
393 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
394 t
->Image
[0][baseLevel
]->HeightLog2
);
395 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
396 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
398 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
399 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
400 t
->Image
[0][baseLevel
]->HeightLog2
);
401 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
403 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
404 maxLog2
= 0; /* not applicable */
405 maxLevels
= 1; /* no mipmapping */
408 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
412 ASSERT(maxLevels
> 0);
414 t
->_MaxLevel
= baseLevel
+ maxLog2
;
415 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
416 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
418 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
419 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
421 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
422 /* make sure that all six cube map level 0 images are the same size */
423 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
424 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
426 for (face
= 1; face
< 6; face
++) {
427 if (t
->Image
[face
][baseLevel
] == NULL
||
428 t
->Image
[face
][baseLevel
]->Width2
!= w
||
429 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
430 t
->_Complete
= GL_FALSE
;
431 incomplete(t
, "Non-quare cubemap image");
437 /* extra checking for mipmaps */
438 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
440 * Mipmapping: determine if we have a complete set of mipmaps
443 GLint minLevel
= baseLevel
;
444 GLint maxLevel
= t
->_MaxLevel
;
446 if (minLevel
> maxLevel
) {
447 t
->_Complete
= GL_FALSE
;
448 incomplete(t
, "minLevel > maxLevel");
452 /* Test dimension-independent attributes */
453 for (i
= minLevel
; i
<= maxLevel
; i
++) {
454 if (t
->Image
[0][i
]) {
455 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
456 t
->_Complete
= GL_FALSE
;
457 incomplete(t
, "Format[i] != Format[baseLevel]");
460 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
461 t
->_Complete
= GL_FALSE
;
462 incomplete(t
, "Border[i] != Border[baseLevel]");
468 /* Test things which depend on number of texture image dimensions */
469 if ((t
->Target
== GL_TEXTURE_1D
) ||
470 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
471 /* Test 1-D mipmaps */
472 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
473 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
477 if (i
>= minLevel
&& i
<= maxLevel
) {
478 if (!t
->Image
[0][i
]) {
479 t
->_Complete
= GL_FALSE
;
480 incomplete(t
, "1D Image[0][i] == NULL");
483 if (t
->Image
[0][i
]->Width2
!= width
) {
484 t
->_Complete
= GL_FALSE
;
485 incomplete(t
, "1D Image[0][i] bad width");
490 return; /* found smallest needed mipmap, all done! */
494 else if ((t
->Target
== GL_TEXTURE_2D
) ||
495 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
496 /* Test 2-D mipmaps */
497 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
498 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
499 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
506 if (i
>= minLevel
&& i
<= maxLevel
) {
507 if (!t
->Image
[0][i
]) {
508 t
->_Complete
= GL_FALSE
;
509 incomplete(t
, "2D Image[0][i] == NULL");
512 if (t
->Image
[0][i
]->Width2
!= width
) {
513 t
->_Complete
= GL_FALSE
;
514 incomplete(t
, "2D Image[0][i] bad width");
517 if (t
->Image
[0][i
]->Height2
!= height
) {
518 t
->_Complete
= GL_FALSE
;
519 incomplete(t
, "2D Image[0][i] bad height");
522 if (width
==1 && height
==1) {
523 return; /* found smallest needed mipmap, all done! */
528 else if (t
->Target
== GL_TEXTURE_3D
) {
529 /* Test 3-D mipmaps */
530 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
531 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
532 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
533 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
543 if (i
>= minLevel
&& i
<= maxLevel
) {
544 if (!t
->Image
[0][i
]) {
545 incomplete(t
, "3D Image[0][i] == NULL");
546 t
->_Complete
= GL_FALSE
;
549 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
550 t
->_Complete
= GL_FALSE
;
551 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
554 if (t
->Image
[0][i
]->Width2
!= width
) {
555 t
->_Complete
= GL_FALSE
;
556 incomplete(t
, "3D Image[0][i] bad width");
559 if (t
->Image
[0][i
]->Height2
!= height
) {
560 t
->_Complete
= GL_FALSE
;
561 incomplete(t
, "3D Image[0][i] bad height");
564 if (t
->Image
[0][i
]->Depth2
!= depth
) {
565 t
->_Complete
= GL_FALSE
;
566 incomplete(t
, "3D Image[0][i] bad depth");
570 if (width
== 1 && height
== 1 && depth
== 1) {
571 return; /* found smallest needed mipmap, all done! */
575 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
576 /* make sure 6 cube faces are consistant */
577 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
578 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
579 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
586 if (i
>= minLevel
&& i
<= maxLevel
) {
588 for (face
= 0; face
< 6; face
++) {
589 /* check that we have images defined */
590 if (!t
->Image
[face
][i
]) {
591 t
->_Complete
= GL_FALSE
;
592 incomplete(t
, "CubeMap Image[n][i] == NULL");
595 /* Don't support GL_DEPTH_COMPONENT for cube maps */
596 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
597 t
->_Complete
= GL_FALSE
;
598 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
601 /* check that all six images have same size */
602 if (t
->Image
[face
][i
]->Width2
!=width
||
603 t
->Image
[face
][i
]->Height2
!=height
) {
604 t
->_Complete
= GL_FALSE
;
605 incomplete(t
, "CubeMap Image[n][i] bad size");
610 if (width
== 1 && height
== 1) {
611 return; /* found smallest needed mipmap, all done! */
615 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
616 /* XXX special checking? */
620 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
628 /***********************************************************************/
629 /** \name API functions */
634 * Generate texture names.
636 * \param n number of texture names to be generated.
637 * \param textures an array in which will hold the generated texture names.
639 * \sa glGenTextures().
641 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
642 * IDs which are stored in \p textures. Corresponding empty texture
643 * objects are also generated.
646 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
648 GET_CURRENT_CONTEXT(ctx
);
651 ASSERT_OUTSIDE_BEGIN_END(ctx
);
654 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
662 * This must be atomic (generation and allocation of texture IDs)
664 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
666 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
668 /* Allocate new, empty texture objects */
669 for (i
= 0; i
< n
; i
++) {
670 struct gl_texture_object
*texObj
;
671 GLuint name
= first
+ i
;
673 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
675 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
676 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
680 /* insert into hash table */
681 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
686 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
691 * Check if the given texture object is bound to the current draw or
692 * read framebuffer. If so, Unbind it.
695 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
697 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
700 for (i
= 0; i
< n
; i
++) {
701 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
704 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
705 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
706 fb
->Attachment
[j
].Texture
== texObj
) {
707 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
716 * Check if the given texture object is bound to any texture image units and
717 * unbind it if so (revert to default textures).
720 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
724 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
725 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
726 if (texObj
== unit
->Current1D
) {
727 _mesa_reference_texobj(&unit
->Current1D
, ctx
->Shared
->Default1D
);
729 else if (texObj
== unit
->Current2D
) {
730 _mesa_reference_texobj(&unit
->Current2D
, ctx
->Shared
->Default2D
);
732 else if (texObj
== unit
->Current3D
) {
733 _mesa_reference_texobj(&unit
->Current3D
, ctx
->Shared
->Default3D
);
735 else if (texObj
== unit
->CurrentCubeMap
) {
736 _mesa_reference_texobj(&unit
->CurrentCubeMap
, ctx
->Shared
->DefaultCubeMap
);
738 else if (texObj
== unit
->CurrentRect
) {
739 _mesa_reference_texobj(&unit
->CurrentRect
, ctx
->Shared
->DefaultRect
);
741 else if (texObj
== unit
->Current1DArray
) {
742 _mesa_reference_texobj(&unit
->Current1DArray
, ctx
->Shared
->Default1DArray
);
744 else if (texObj
== unit
->Current2DArray
) {
745 _mesa_reference_texobj(&unit
->Current2DArray
, ctx
->Shared
->Default2DArray
);
752 * Delete named textures.
754 * \param n number of textures to be deleted.
755 * \param textures array of texture IDs to be deleted.
757 * \sa glDeleteTextures().
759 * If we're about to delete a texture that's currently bound to any
760 * texture unit, unbind the texture first. Decrement the reference
761 * count on the texture object and delete it if it's zero.
762 * Recall that texture objects can be shared among several rendering
766 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
768 GET_CURRENT_CONTEXT(ctx
);
770 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
775 for (i
= 0; i
< n
; i
++) {
776 if (textures
[i
] > 0) {
777 struct gl_texture_object
*delObj
778 = _mesa_lookup_texture(ctx
, textures
[i
]);
781 _mesa_lock_texture(ctx
, delObj
);
783 /* Check if texture is bound to any framebuffer objects.
785 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
787 unbind_texobj_from_fbo(ctx
, delObj
);
789 /* Check if this texture is currently bound to any texture units.
792 unbind_texobj_from_texunits(ctx
, delObj
);
794 _mesa_unlock_texture(ctx
, delObj
);
796 ctx
->NewState
|= _NEW_TEXTURE
;
798 /* The texture _name_ is now free for re-use.
799 * Remove it from the hash table now.
801 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
802 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
803 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
805 /* Unrefernce the texobj. If refcount hits zero, the texture
808 _mesa_reference_texobj(&delObj
, NULL
);
816 * Bind a named texture to a texturing target.
818 * \param target texture target.
819 * \param texName texture name.
821 * \sa glBindTexture().
823 * Determines the old texture object bound and returns immediately if rebinding
824 * the same texture. Get the current texture which is either a default texture
825 * if name is null, a named texture from the hash, or a new texture if the
826 * given texture name is new. Increments its reference count, binds it, and
827 * calls dd_function_table::BindTexture. Decrements the old texture reference
828 * count and deletes it if it reaches zero.
831 _mesa_BindTexture( GLenum target
, GLuint texName
)
833 GET_CURRENT_CONTEXT(ctx
);
834 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
835 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
836 struct gl_texture_object
*newTexObj
= NULL
;
837 ASSERT_OUTSIDE_BEGIN_END(ctx
);
839 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
840 _mesa_debug(ctx
, "glBindTexture %s %d\n",
841 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
844 * Get pointer to new texture object (newTexObj)
847 /* newTexObj = a default texture object */
850 newTexObj
= ctx
->Shared
->Default1D
;
853 newTexObj
= ctx
->Shared
->Default2D
;
856 newTexObj
= ctx
->Shared
->Default3D
;
858 case GL_TEXTURE_CUBE_MAP_ARB
:
859 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
861 case GL_TEXTURE_RECTANGLE_NV
:
862 newTexObj
= ctx
->Shared
->DefaultRect
;
864 case GL_TEXTURE_1D_ARRAY_EXT
:
865 newTexObj
= ctx
->Shared
->Default1DArray
;
867 case GL_TEXTURE_2D_ARRAY_EXT
:
868 newTexObj
= ctx
->Shared
->Default2DArray
;
871 ; /* Bad targets are caught above */
875 /* non-default texture object */
876 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
879 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
880 /* the named texture object's dimensions don't match the target */
881 _mesa_error( ctx
, GL_INVALID_OPERATION
,
882 "glBindTexture(wrong dimensionality)" );
885 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
886 /* have to init wrap and filter state here - kind of klunky */
887 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
888 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
889 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
890 newTexObj
->MinFilter
= GL_LINEAR
;
891 if (ctx
->Driver
.TexParameter
) {
892 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
893 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
894 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
895 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
896 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
897 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
902 /* if this is a new texture id, allocate a texture object now */
903 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
905 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
909 /* and insert it into hash table */
910 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
911 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
912 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
914 newTexObj
->Target
= target
;
917 assert(valid_texture_object(newTexObj
));
919 /* flush before changing binding */
920 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
922 /* Do the actual binding. The refcount on the previously bound
923 * texture object will be decremented. It'll be deleted if the
928 _mesa_reference_texobj(&texUnit
->Current1D
, newTexObj
);
931 _mesa_reference_texobj(&texUnit
->Current2D
, newTexObj
);
934 _mesa_reference_texobj(&texUnit
->Current3D
, newTexObj
);
936 case GL_TEXTURE_CUBE_MAP_ARB
:
937 _mesa_reference_texobj(&texUnit
->CurrentCubeMap
, newTexObj
);
939 case GL_TEXTURE_RECTANGLE_NV
:
940 _mesa_reference_texobj(&texUnit
->CurrentRect
, newTexObj
);
942 case GL_TEXTURE_1D_ARRAY_EXT
:
943 texUnit
->Current1DArray
= newTexObj
;
945 case GL_TEXTURE_2D_ARRAY_EXT
:
946 texUnit
->Current2DArray
= newTexObj
;
949 _mesa_problem(ctx
, "bad target in BindTexture");
953 /* Pass BindTexture call to device driver */
954 if (ctx
->Driver
.BindTexture
)
955 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
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
);