0d2946e95a735854d3118c4cdeba16fcdc6df5b2
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.
144 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
146 * \param shared the shared GL state to which the object belongs.
147 * \param texOjb the texture object to delete.
150 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
156 _mesa_free_colortable_data(&texObj
->Palette
);
158 /* free the texture images */
159 for (face
= 0; face
< 6; face
++) {
160 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
161 if (texObj
->Image
[face
][i
]) {
162 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
167 /* free this object */
175 * Copy texture object state from one texture object to another.
176 * Use for glPush/PopAttrib.
178 * \param dest destination texture object.
179 * \param src source texture object.
182 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
183 const struct gl_texture_object
*src
)
185 dest
->Name
= src
->Name
;
186 dest
->Priority
= src
->Priority
;
187 dest
->BorderColor
[0] = src
->BorderColor
[0];
188 dest
->BorderColor
[1] = src
->BorderColor
[1];
189 dest
->BorderColor
[2] = src
->BorderColor
[2];
190 dest
->BorderColor
[3] = src
->BorderColor
[3];
191 dest
->WrapS
= src
->WrapS
;
192 dest
->WrapT
= src
->WrapT
;
193 dest
->WrapR
= src
->WrapR
;
194 dest
->MinFilter
= src
->MinFilter
;
195 dest
->MagFilter
= src
->MagFilter
;
196 dest
->MinLod
= src
->MinLod
;
197 dest
->MaxLod
= src
->MaxLod
;
198 dest
->LodBias
= src
->LodBias
;
199 dest
->BaseLevel
= src
->BaseLevel
;
200 dest
->MaxLevel
= src
->MaxLevel
;
201 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
202 dest
->CompareFlag
= src
->CompareFlag
;
203 dest
->CompareOperator
= src
->CompareOperator
;
204 dest
->ShadowAmbient
= src
->ShadowAmbient
;
205 dest
->CompareMode
= src
->CompareMode
;
206 dest
->CompareFunc
= src
->CompareFunc
;
207 dest
->DepthMode
= src
->DepthMode
;
208 dest
->_MaxLevel
= src
->_MaxLevel
;
209 dest
->_MaxLambda
= src
->_MaxLambda
;
210 dest
->GenerateMipmap
= src
->GenerateMipmap
;
211 dest
->Palette
= src
->Palette
;
212 dest
->Complete
= src
->Complete
;
217 * Report why a texture object is incomplete.
219 * \param t texture object.
220 * \param why string describing why it's incomplete.
222 * \note For debug purposes only.
226 incomplete(const struct gl_texture_object
*t
, const char *why
)
228 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
231 #define incomplete(t, why)
236 * Examine a texture object to determine if it is complete.
238 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
241 * \param ctx GL context.
242 * \param t texture object.
244 * According to the texture target, verifies that each of the mipmaps is
245 * present and has the expected size.
248 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
249 struct gl_texture_object
*t
)
251 const GLint baseLevel
= t
->BaseLevel
;
252 GLint maxLog2
= 0, maxLevels
= 0;
254 t
->Complete
= GL_TRUE
; /* be optimistic */
256 /* Always need the base level image */
257 if (!t
->Image
[0][baseLevel
]) {
259 _mesa_sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
260 (void *) t
, t
->Name
, baseLevel
);
262 t
->Complete
= GL_FALSE
;
266 /* Check width/height/depth for zero */
267 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
268 t
->Image
[0][baseLevel
]->Height
== 0 ||
269 t
->Image
[0][baseLevel
]->Depth
== 0) {
270 incomplete(t
, "texture width = 0");
271 t
->Complete
= GL_FALSE
;
275 /* Compute _MaxLevel */
276 if ((t
->Target
== GL_TEXTURE_1D
) ||
277 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
278 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
279 maxLevels
= ctx
->Const
.MaxTextureLevels
;
281 else if ((t
->Target
== GL_TEXTURE_2D
) ||
282 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
283 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
284 t
->Image
[0][baseLevel
]->HeightLog2
);
285 maxLevels
= ctx
->Const
.MaxTextureLevels
;
287 else if (t
->Target
== GL_TEXTURE_3D
) {
288 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
289 t
->Image
[0][baseLevel
]->HeightLog2
);
290 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
291 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
293 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
294 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
295 t
->Image
[0][baseLevel
]->HeightLog2
);
296 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
298 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
299 maxLog2
= 0; /* not applicable */
300 maxLevels
= 1; /* no mipmapping */
303 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
307 ASSERT(maxLevels
> 0);
309 t
->_MaxLevel
= baseLevel
+ maxLog2
;
310 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
311 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
313 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
314 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
316 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
317 /* make sure that all six cube map level 0 images are the same size */
318 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
319 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
321 for (face
= 1; face
< 6; face
++) {
322 if (t
->Image
[face
][baseLevel
] == NULL
||
323 t
->Image
[face
][baseLevel
]->Width2
!= w
||
324 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
325 t
->Complete
= GL_FALSE
;
326 incomplete(t
, "Non-quare cubemap image");
332 /* extra checking for mipmaps */
333 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
335 * Mipmapping: determine if we have a complete set of mipmaps
338 GLint minLevel
= baseLevel
;
339 GLint maxLevel
= t
->_MaxLevel
;
341 if (minLevel
> maxLevel
) {
342 t
->Complete
= GL_FALSE
;
343 incomplete(t
, "minLevel > maxLevel");
347 /* Test dimension-independent attributes */
348 for (i
= minLevel
; i
<= maxLevel
; i
++) {
349 if (t
->Image
[0][i
]) {
350 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
351 t
->Complete
= GL_FALSE
;
352 incomplete(t
, "Format[i] != Format[baseLevel]");
355 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
356 t
->Complete
= GL_FALSE
;
357 incomplete(t
, "Border[i] != Border[baseLevel]");
363 /* Test things which depend on number of texture image dimensions */
364 if ((t
->Target
== GL_TEXTURE_1D
) ||
365 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
366 /* Test 1-D mipmaps */
367 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
368 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
372 if (i
>= minLevel
&& i
<= maxLevel
) {
373 if (!t
->Image
[0][i
]) {
374 t
->Complete
= GL_FALSE
;
375 incomplete(t
, "1D Image[0][i] == NULL");
378 if (t
->Image
[0][i
]->Width2
!= width
) {
379 t
->Complete
= GL_FALSE
;
380 incomplete(t
, "1D Image[0][i] bad width");
385 return; /* found smallest needed mipmap, all done! */
389 else if ((t
->Target
== GL_TEXTURE_2D
) ||
390 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
391 /* Test 2-D mipmaps */
392 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
393 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
394 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
401 if (i
>= minLevel
&& i
<= maxLevel
) {
402 if (!t
->Image
[0][i
]) {
403 t
->Complete
= GL_FALSE
;
404 incomplete(t
, "2D Image[0][i] == NULL");
407 if (t
->Image
[0][i
]->Width2
!= width
) {
408 t
->Complete
= GL_FALSE
;
409 incomplete(t
, "2D Image[0][i] bad width");
412 if (t
->Image
[0][i
]->Height2
!= height
) {
413 t
->Complete
= GL_FALSE
;
414 incomplete(t
, "2D Image[0][i] bad height");
417 if (width
==1 && height
==1) {
418 return; /* found smallest needed mipmap, all done! */
423 else if (t
->Target
== GL_TEXTURE_3D
) {
424 /* Test 3-D mipmaps */
425 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
426 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
427 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
428 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
438 if (i
>= minLevel
&& i
<= maxLevel
) {
439 if (!t
->Image
[0][i
]) {
440 incomplete(t
, "3D Image[0][i] == NULL");
441 t
->Complete
= GL_FALSE
;
444 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
445 t
->Complete
= GL_FALSE
;
446 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
449 if (t
->Image
[0][i
]->Width2
!= width
) {
450 t
->Complete
= GL_FALSE
;
451 incomplete(t
, "3D Image[0][i] bad width");
454 if (t
->Image
[0][i
]->Height2
!= height
) {
455 t
->Complete
= GL_FALSE
;
456 incomplete(t
, "3D Image[0][i] bad height");
459 if (t
->Image
[0][i
]->Depth2
!= depth
) {
460 t
->Complete
= GL_FALSE
;
461 incomplete(t
, "3D Image[0][i] bad depth");
465 if (width
== 1 && height
== 1 && depth
== 1) {
466 return; /* found smallest needed mipmap, all done! */
470 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
471 /* make sure 6 cube faces are consistant */
472 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
473 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
474 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
481 if (i
>= minLevel
&& i
<= maxLevel
) {
483 for (face
= 0; face
< 6; face
++) {
484 /* check that we have images defined */
485 if (!t
->Image
[face
][i
]) {
486 t
->Complete
= GL_FALSE
;
487 incomplete(t
, "CubeMap Image[n][i] == NULL");
490 /* Don't support GL_DEPTH_COMPONENT for cube maps */
491 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
492 t
->Complete
= GL_FALSE
;
493 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
496 /* check that all six images have same size */
497 if (t
->Image
[face
][i
]->Width2
!=width
||
498 t
->Image
[face
][i
]->Height2
!=height
) {
499 t
->Complete
= GL_FALSE
;
500 incomplete(t
, "CubeMap Image[n][i] bad size");
505 if (width
== 1 && height
== 1) {
506 return; /* found smallest needed mipmap, all done! */
510 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
511 /* XXX special checking? */
515 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
523 /***********************************************************************/
524 /** \name API functions */
529 * Generate texture names.
531 * \param n number of texture names to be generated.
532 * \param textures an array in which will hold the generated texture names.
534 * \sa glGenTextures().
536 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
537 * IDs which are stored in \p textures. Corresponding empty texture
538 * objects are also generated.
541 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
543 GET_CURRENT_CONTEXT(ctx
);
546 ASSERT_OUTSIDE_BEGIN_END(ctx
);
549 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
557 * This must be atomic (generation and allocation of texture IDs)
559 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
561 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
563 /* Allocate new, empty texture objects */
564 for (i
= 0; i
< n
; i
++) {
565 struct gl_texture_object
*texObj
;
566 GLuint name
= first
+ i
;
568 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
570 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
575 /* insert into hash table */
576 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
581 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
586 * Check if the given texture object is bound to the current draw or
587 * read framebuffer. If so, Unbind it.
590 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
592 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
595 for (i
= 0; i
< n
; i
++) {
596 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
599 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
600 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
601 fb
->Attachment
[j
].Texture
== texObj
) {
602 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
611 * Check if the given texture object is bound to any texture image units and
613 * XXX all RefCount accesses should be protected by a mutex.
616 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
620 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
621 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
622 struct gl_texture_object
**curr
= NULL
;
624 if (texObj
== unit
->Current1D
) {
625 curr
= &unit
->Current1D
;
626 unit
->Current1D
= ctx
->Shared
->Default1D
;
628 else if (texObj
== unit
->Current2D
) {
629 curr
= &unit
->Current2D
;
630 unit
->Current2D
= ctx
->Shared
->Default2D
;
632 else if (texObj
== unit
->Current3D
) {
633 curr
= &unit
->Current3D
;
634 unit
->Current3D
= ctx
->Shared
->Default3D
;
636 else if (texObj
== unit
->CurrentCubeMap
) {
637 curr
= &unit
->CurrentCubeMap
;
638 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
640 else if (texObj
== unit
->CurrentRect
) {
641 curr
= &unit
->CurrentRect
;
642 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
644 else if (texObj
== unit
->Current1DArray
) {
645 curr
= &unit
->Current1DArray
;
646 unit
->CurrentRect
= ctx
->Shared
->Default1DArray
;
648 else if (texObj
== unit
->Current2DArray
) {
649 curr
= &unit
->Current1DArray
;
650 unit
->CurrentRect
= ctx
->Shared
->Default2DArray
;
656 if (texObj
== unit
->_Current
)
657 unit
->_Current
= *curr
;
664 * Delete named textures.
666 * \param n number of textures to be deleted.
667 * \param textures array of texture IDs to be deleted.
669 * \sa glDeleteTextures().
671 * If we're about to delete a texture that's currently bound to any
672 * texture unit, unbind the texture first. Decrement the reference
673 * count on the texture object and delete it if it's zero.
674 * Recall that texture objects can be shared among several rendering
678 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
680 GET_CURRENT_CONTEXT(ctx
);
682 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
687 for (i
= 0; i
< n
; i
++) {
688 if (textures
[i
] > 0) {
689 struct gl_texture_object
*delObj
690 = _mesa_lookup_texture(ctx
, textures
[i
]);
695 _mesa_lock_texture(ctx
, delObj
);
697 /* Check if texture is bound to any framebuffer objects.
699 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
701 unbind_texobj_from_fbo(ctx
, delObj
);
703 /* Check if this texture is currently bound to any texture units.
704 * If so, unbind it and decrement the reference count.
706 unbind_texobj_from_texunits(ctx
, delObj
);
708 ctx
->NewState
|= _NEW_TEXTURE
;
710 /* The texture _name_ is now free for re-use.
711 * Remove it from the hash table now.
713 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
714 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
715 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
717 /* The actual texture object will not be freed until it's no
718 * longer bound in any context.
719 * XXX all RefCount accesses should be protected by a mutex.
722 deleted
= (delObj
->RefCount
== 0);
723 _mesa_unlock_texture(ctx
, delObj
);
725 /* We know that refcount went to zero above, so this is
726 * the only pointer left to delObj, so we don't have to
727 * worry about locking any more:
730 ASSERT(delObj
->Name
!= 0); /* Never delete default tex objs */
731 ASSERT(ctx
->Driver
.DeleteTexture
);
732 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
741 * Bind a named texture to a texturing target.
743 * \param target texture target.
744 * \param texName texture name.
746 * \sa glBindTexture().
748 * Determines the old texture object bound and returns immediately if rebinding
749 * the same texture. Get the current texture which is either a default texture
750 * if name is null, a named texture from the hash, or a new texture if the
751 * given texture name is new. Increments its reference count, binds it, and
752 * calls dd_function_table::BindTexture. Decrements the old texture reference
753 * count and deletes it if it reaches zero.
756 _mesa_BindTexture( GLenum target
, GLuint texName
)
758 GET_CURRENT_CONTEXT(ctx
);
759 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
760 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
761 struct gl_texture_object
*oldTexObj
;
762 struct gl_texture_object
*newTexObj
= NULL
;
763 ASSERT_OUTSIDE_BEGIN_END(ctx
);
765 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
766 _mesa_debug(ctx
, "glBindTexture %s %d\n",
767 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
770 * Get pointer to currently bound texture object (oldTexObj)
774 oldTexObj
= texUnit
->Current1D
;
777 oldTexObj
= texUnit
->Current2D
;
780 oldTexObj
= texUnit
->Current3D
;
782 case GL_TEXTURE_CUBE_MAP_ARB
:
783 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
784 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
787 oldTexObj
= texUnit
->CurrentCubeMap
;
789 case GL_TEXTURE_RECTANGLE_NV
:
790 if (!ctx
->Extensions
.NV_texture_rectangle
) {
791 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
794 oldTexObj
= texUnit
->CurrentRect
;
796 case GL_TEXTURE_1D_ARRAY_EXT
:
797 if (!ctx
->Extensions
.MESA_texture_array
) {
798 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
801 oldTexObj
= texUnit
->Current1DArray
;
803 case GL_TEXTURE_2D_ARRAY_EXT
:
804 if (!ctx
->Extensions
.MESA_texture_array
) {
805 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
808 oldTexObj
= texUnit
->Current2DArray
;
811 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
815 if (oldTexObj
->Name
== texName
) {
816 /* XXX this might be wrong. If the texobj is in use by another
817 * context and a texobj parameter was changed, this might be our
818 * only chance to update this context's hardware state.
819 * Note that some applications re-bind the same texture a lot so we
820 * want to handle that case quickly.
822 return; /* rebinding the same texture- no change */
826 * Get pointer to new texture object (newTexObj)
829 /* newTexObj = a default texture object */
832 newTexObj
= ctx
->Shared
->Default1D
;
835 newTexObj
= ctx
->Shared
->Default2D
;
838 newTexObj
= ctx
->Shared
->Default3D
;
840 case GL_TEXTURE_CUBE_MAP_ARB
:
841 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
843 case GL_TEXTURE_RECTANGLE_NV
:
844 newTexObj
= ctx
->Shared
->DefaultRect
;
846 case GL_TEXTURE_1D_ARRAY_EXT
:
847 newTexObj
= ctx
->Shared
->Default1DArray
;
849 case GL_TEXTURE_2D_ARRAY_EXT
:
850 newTexObj
= ctx
->Shared
->Default2DArray
;
853 ; /* Bad targets are caught above */
857 /* non-default texture object */
858 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
861 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
862 /* the named texture object's dimensions don't match the target */
863 _mesa_error( ctx
, GL_INVALID_OPERATION
,
864 "glBindTexture(wrong dimensionality)" );
867 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
868 /* have to init wrap and filter state here - kind of klunky */
869 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
870 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
871 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
872 newTexObj
->MinFilter
= GL_LINEAR
;
873 if (ctx
->Driver
.TexParameter
) {
874 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
875 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
876 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
877 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
878 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
879 (*ctx
->Driver
.TexParameter
)( ctx
, target
, newTexObj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
884 /* if this is a new texture id, allocate a texture object now */
885 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
887 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
891 /* and insert it into hash table */
892 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
893 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
894 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
896 newTexObj
->Target
= target
;
899 /* XXX all RefCount accesses should be protected by a mutex. */
900 newTexObj
->RefCount
++;
902 /* do the actual binding, but first flush outstanding vertices:
904 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
908 texUnit
->Current1D
= newTexObj
;
911 texUnit
->Current2D
= newTexObj
;
914 texUnit
->Current3D
= newTexObj
;
916 case GL_TEXTURE_CUBE_MAP_ARB
:
917 texUnit
->CurrentCubeMap
= newTexObj
;
919 case GL_TEXTURE_RECTANGLE_NV
:
920 texUnit
->CurrentRect
= newTexObj
;
922 case GL_TEXTURE_1D_ARRAY_EXT
:
923 texUnit
->Current1DArray
= newTexObj
;
925 case GL_TEXTURE_2D_ARRAY_EXT
:
926 texUnit
->Current2DArray
= newTexObj
;
929 _mesa_problem(ctx
, "bad target in BindTexture");
933 /* Pass BindTexture call to device driver */
934 if (ctx
->Driver
.BindTexture
)
935 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
937 /* Decrement the reference count on the old texture and check if it's
940 /* XXX all RefCount accesses should be protected by a mutex. */
941 oldTexObj
->RefCount
--;
942 ASSERT(oldTexObj
->RefCount
>= 0);
943 if (oldTexObj
->RefCount
== 0) {
944 ASSERT(oldTexObj
->Name
!= 0);
945 ASSERT(ctx
->Driver
.DeleteTexture
);
946 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
952 * Set texture priorities.
954 * \param n number of textures.
955 * \param texName texture names.
956 * \param priorities corresponding texture priorities.
958 * \sa glPrioritizeTextures().
960 * Looks up each texture in the hash, clamps the corresponding priority between
961 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
964 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
965 const GLclampf
*priorities
)
967 GET_CURRENT_CONTEXT(ctx
);
969 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
972 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
979 for (i
= 0; i
< n
; i
++) {
980 if (texName
[i
] > 0) {
981 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
983 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
984 if (ctx
->Driver
.PrioritizeTexture
)
985 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
990 ctx
->NewState
|= _NEW_TEXTURE
;
994 * See if textures are loaded in texture memory.
996 * \param n number of textures to query.
997 * \param texName array with the texture names.
998 * \param residences array which will hold the residence status.
1000 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1002 * \sa glAreTexturesResident().
1004 * Looks up each texture in the hash and calls
1005 * dd_function_table::IsTextureResident.
1007 GLboolean GLAPIENTRY
1008 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1009 GLboolean
*residences
)
1011 GET_CURRENT_CONTEXT(ctx
);
1012 GLboolean allResident
= GL_TRUE
;
1014 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1017 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1021 if (!texName
|| !residences
)
1024 for (i
= 0; i
< n
; i
++) {
1025 struct gl_texture_object
*t
;
1026 if (texName
[i
] == 0) {
1027 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1030 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1032 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1035 if (!ctx
->Driver
.IsTextureResident
||
1036 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1037 /* The texture is resident */
1039 residences
[i
] = GL_TRUE
;
1042 /* The texture is not resident */
1044 allResident
= GL_FALSE
;
1045 for (j
= 0; j
< i
; j
++)
1046 residences
[j
] = GL_TRUE
;
1048 residences
[i
] = GL_FALSE
;
1056 * See if a name corresponds to a texture.
1058 * \param texture texture name.
1060 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1063 * \sa glIsTexture().
1065 * Calls _mesa_HashLookup().
1067 GLboolean GLAPIENTRY
1068 _mesa_IsTexture( GLuint texture
)
1070 struct gl_texture_object
*t
;
1071 GET_CURRENT_CONTEXT(ctx
);
1072 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1077 t
= _mesa_lookup_texture(ctx
, texture
);
1079 /* IsTexture is true only after object has been bound once. */
1080 return t
&& t
->Target
;
1085 * Simplest implementation of texture locking: Grab the a new mutex in
1086 * the shared context. Examine the shared context state timestamp and
1087 * if there has been a change, set the appropriate bits in
1090 * This is used to deal with synchronizing things when a texture object
1091 * is used/modified by different contexts (or threads) which are sharing
1094 * See also _mesa_lock/unlock_texture() in teximage.h
1097 _mesa_lock_context_textures( GLcontext
*ctx
)
1099 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1101 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1102 ctx
->NewState
|= _NEW_TEXTURE
;
1103 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1109 _mesa_unlock_context_textures( GLcontext
*ctx
)
1111 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1112 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);