3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2003 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.
44 /**********************************************************************/
45 /** \name Internal functions */
49 * Allocate and initialize a new texture object. But don't put it into the
50 * texture object hash table.
52 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
55 * \param shared the shared GL state structure to contain the texture object
56 * \param name integer name for the texture object
57 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
58 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
61 * \return pointer to new texture object.
63 * Allocate and initialize a gl_texture_object structure, and insert in the
64 * shared state texture list while holding its mutex.
65 * If <tt>name > 0</tt> then also insert the new texture object into the hash
69 struct gl_texture_object
*
70 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
72 struct gl_texture_object
*obj
;
73 obj
= CALLOC_STRUCT(gl_texture_object
);
74 _mesa_initialize_texture_object(obj
, name
, target
);
80 * Initialize a texture object to default values.
81 * \param obj the texture object
82 * \param name the texture name
83 * \param target the texture target
86 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
87 GLuint name
, GLenum target
)
90 target
== GL_TEXTURE_1D
||
91 target
== GL_TEXTURE_2D
||
92 target
== GL_TEXTURE_3D
||
93 target
== GL_TEXTURE_CUBE_MAP_ARB
||
94 target
== GL_TEXTURE_RECTANGLE_NV
);
96 /* init the non-zero fields */
97 _glthread_INIT_MUTEX(obj
->Mutex
);
100 obj
->Target
= target
;
101 obj
->Priority
= 1.0F
;
102 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
103 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
104 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
105 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
106 obj
->MinFilter
= GL_LINEAR
;
109 obj
->WrapS
= GL_REPEAT
;
110 obj
->WrapT
= GL_REPEAT
;
111 obj
->WrapR
= GL_REPEAT
;
112 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
114 obj
->MagFilter
= GL_LINEAR
;
115 obj
->MinLod
= -1000.0;
116 obj
->MaxLod
= 1000.0;
119 obj
->MaxLevel
= 1000;
120 obj
->MaxAnisotropy
= 1.0;
121 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
122 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
123 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
124 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
125 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
126 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
127 _mesa_init_colortable(&obj
->Palette
);
132 * Deallocate a texture object struct. It should have already been
133 * removed from the texture object pool.
135 * \param shared the shared GL state to which the object belongs.
136 * \param texOjb the texture object to delete.
138 * Unlink the texture object from the shared state texture linked list while
139 * holding its lock. If the texture is a name number it's also removed from the
140 * hash table. Finally frees the texture images and the object itself.
143 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
151 _mesa_free_colortable_data(&texObj
->Palette
);
153 /* free the texture images */
154 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
155 if (texObj
->Image
[i
]) {
156 _mesa_delete_texture_image( texObj
->Image
[i
] );
160 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
161 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
163 /* free this object */
169 * Add the given texture object to the texture object pool.
172 _mesa_save_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
174 /* insert into linked list */
175 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
176 texObj
->Next
= ctx
->Shared
->TexObjectList
;
177 ctx
->Shared
->TexObjectList
= texObj
;
178 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
180 if (texObj
->Name
> 0) {
181 /* insert into hash table */
182 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
188 * Remove the given texture object from the texture object pool.
189 * Do not deallocate the texture object though.
192 _mesa_remove_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
194 struct gl_texture_object
*tprev
, *tcurr
;
196 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
198 /* unlink from the linked list */
200 tcurr
= ctx
->Shared
->TexObjectList
;
202 if (tcurr
== texObj
) {
204 tprev
->Next
= texObj
->Next
;
207 ctx
->Shared
->TexObjectList
= texObj
->Next
;
215 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
217 if (texObj
->Name
> 0) {
218 /* remove from hash table */
219 _mesa_HashRemove(ctx
->Shared
->TexObjects
, texObj
->Name
);
224 * Copy texture object state from one texture object to another.
226 * \param dest destination texture object.
227 * \param src source texture object.
230 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
231 const struct gl_texture_object
*src
)
233 dest
->Name
= src
->Name
;
234 dest
->Priority
= src
->Priority
;
235 dest
->BorderColor
[0] = src
->BorderColor
[0];
236 dest
->BorderColor
[1] = src
->BorderColor
[1];
237 dest
->BorderColor
[2] = src
->BorderColor
[2];
238 dest
->BorderColor
[3] = src
->BorderColor
[3];
239 dest
->WrapS
= src
->WrapS
;
240 dest
->WrapT
= src
->WrapT
;
241 dest
->WrapR
= src
->WrapR
;
242 dest
->MinFilter
= src
->MinFilter
;
243 dest
->MagFilter
= src
->MagFilter
;
244 dest
->MinLod
= src
->MinLod
;
245 dest
->MaxLod
= src
->MaxLod
;
246 dest
->LodBias
= src
->LodBias
;
247 dest
->BaseLevel
= src
->BaseLevel
;
248 dest
->MaxLevel
= src
->MaxLevel
;
249 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
250 dest
->CompareFlag
= src
->CompareFlag
;
251 dest
->CompareOperator
= src
->CompareOperator
;
252 dest
->ShadowAmbient
= src
->ShadowAmbient
;
253 dest
->CompareMode
= src
->CompareMode
;
254 dest
->CompareFunc
= src
->CompareFunc
;
255 dest
->DepthMode
= src
->DepthMode
;
256 dest
->_MaxLevel
= src
->_MaxLevel
;
257 dest
->_MaxLambda
= src
->_MaxLambda
;
258 dest
->GenerateMipmap
= src
->GenerateMipmap
;
259 dest
->Palette
= src
->Palette
;
260 dest
->Complete
= src
->Complete
;
261 dest
->_IsPowerOfTwo
= src
->_IsPowerOfTwo
;
266 * Report why a texture object is incomplete.
268 * \param t texture object.
269 * \param why string describing why it's incomplete.
271 * \note For debug purposes only.
275 incomplete(const struct gl_texture_object
*t
, const char *why
)
277 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
280 #define incomplete(t, why)
285 * Examine a texture object to determine if it is complete.
287 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
290 * \param ctx GL context.
291 * \param t texture object.
293 * According to the texture target, verifies that each of the mipmaps is
294 * present and has the expected size.
297 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
298 struct gl_texture_object
*t
)
300 const GLint baseLevel
= t
->BaseLevel
;
301 GLint maxLog2
= 0, maxLevels
= 0;
303 t
->Complete
= GL_TRUE
; /* be optimistic */
304 t
->_IsPowerOfTwo
= GL_TRUE
; /* may be set FALSE below */
306 /* Always need the base level image */
307 if (!t
->Image
[baseLevel
]) {
309 sprintf(s
, "obj %p (%d) Image[baseLevel=%d] == NULL",
310 (void *) t
, t
->Name
, baseLevel
);
312 t
->Complete
= GL_FALSE
;
316 /* Check width/height/depth for zero */
317 if (t
->Image
[baseLevel
]->Width
== 0 ||
318 t
->Image
[baseLevel
]->Height
== 0 ||
319 t
->Image
[baseLevel
]->Depth
== 0) {
320 incomplete(t
, "texture width = 0");
321 t
->Complete
= GL_FALSE
;
325 /* Compute _MaxLevel */
326 if (t
->Target
== GL_TEXTURE_1D
) {
327 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
328 maxLevels
= ctx
->Const
.MaxTextureLevels
;
330 else if (t
->Target
== GL_TEXTURE_2D
) {
331 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
332 t
->Image
[baseLevel
]->HeightLog2
);
333 maxLevels
= ctx
->Const
.MaxTextureLevels
;
335 else if (t
->Target
== GL_TEXTURE_3D
) {
336 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
337 t
->Image
[baseLevel
]->HeightLog2
);
338 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
339 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
341 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
342 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
343 t
->Image
[baseLevel
]->HeightLog2
);
344 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
346 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
347 maxLog2
= 0; /* not applicable */
348 maxLevels
= 1; /* no mipmapping */
351 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
355 ASSERT(maxLevels
> 0);
357 t
->_MaxLevel
= baseLevel
+ maxLog2
;
358 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
359 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
361 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
362 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
364 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
365 /* make sure that all six cube map level 0 images are the same size */
366 const GLuint w
= t
->Image
[baseLevel
]->Width2
;
367 const GLuint h
= t
->Image
[baseLevel
]->Height2
;
368 if (!t
->NegX
[baseLevel
] ||
369 t
->NegX
[baseLevel
]->Width2
!= w
||
370 t
->NegX
[baseLevel
]->Height2
!= h
||
371 !t
->PosY
[baseLevel
] ||
372 t
->PosY
[baseLevel
]->Width2
!= w
||
373 t
->PosY
[baseLevel
]->Height2
!= h
||
374 !t
->NegY
[baseLevel
] ||
375 t
->NegY
[baseLevel
]->Width2
!= w
||
376 t
->NegY
[baseLevel
]->Height2
!= h
||
377 !t
->PosZ
[baseLevel
] ||
378 t
->PosZ
[baseLevel
]->Width2
!= w
||
379 t
->PosZ
[baseLevel
]->Height2
!= h
||
380 !t
->NegZ
[baseLevel
] ||
381 t
->NegZ
[baseLevel
]->Width2
!= w
||
382 t
->NegZ
[baseLevel
]->Height2
!= h
) {
383 t
->Complete
= GL_FALSE
;
384 incomplete(t
, "Non-quare cubemap image");
389 /* check for non power of two */
390 if (!t
->Image
[baseLevel
]->_IsPowerOfTwo
) {
391 t
->_IsPowerOfTwo
= GL_FALSE
;
394 /* extra checking for mipmaps */
395 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
397 * Mipmapping: determine if we have a complete set of mipmaps
400 GLint minLevel
= baseLevel
;
401 GLint maxLevel
= t
->_MaxLevel
;
403 if (minLevel
> maxLevel
) {
404 t
->Complete
= GL_FALSE
;
405 incomplete(t
, "minLevel > maxLevel");
409 /* Test dimension-independent attributes */
410 for (i
= minLevel
; i
<= maxLevel
; i
++) {
412 if (t
->Image
[i
]->TexFormat
!= t
->Image
[baseLevel
]->TexFormat
) {
413 t
->Complete
= GL_FALSE
;
414 incomplete(t
, "Format[i] != Format[baseLevel]");
417 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
418 t
->Complete
= GL_FALSE
;
419 incomplete(t
, "Border[i] != Border[baseLevel]");
425 /* Test things which depend on number of texture image dimensions */
426 if (t
->Target
== GL_TEXTURE_1D
) {
427 /* Test 1-D mipmaps */
428 GLuint width
= t
->Image
[baseLevel
]->Width2
;
429 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
433 if (i
>= minLevel
&& i
<= maxLevel
) {
435 t
->Complete
= GL_FALSE
;
436 incomplete(t
, "1D Image[i] == NULL");
439 if (t
->Image
[i
]->Width2
!= width
) {
440 t
->Complete
= GL_FALSE
;
441 incomplete(t
, "1D Image[i] bad width");
446 return; /* found smallest needed mipmap, all done! */
450 else if (t
->Target
== GL_TEXTURE_2D
) {
451 /* Test 2-D mipmaps */
452 GLuint width
= t
->Image
[baseLevel
]->Width2
;
453 GLuint height
= t
->Image
[baseLevel
]->Height2
;
454 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
461 if (i
>= minLevel
&& i
<= maxLevel
) {
463 t
->Complete
= GL_FALSE
;
464 incomplete(t
, "2D Image[i] == NULL");
467 if (t
->Image
[i
]->Width2
!= width
) {
468 t
->Complete
= GL_FALSE
;
469 incomplete(t
, "2D Image[i] bad width");
472 if (t
->Image
[i
]->Height2
!= height
) {
473 t
->Complete
= GL_FALSE
;
474 incomplete(t
, "2D Image[i] bad height");
477 if (width
==1 && height
==1) {
478 return; /* found smallest needed mipmap, all done! */
483 else if (t
->Target
== GL_TEXTURE_3D
) {
484 /* Test 3-D mipmaps */
485 GLuint width
= t
->Image
[baseLevel
]->Width2
;
486 GLuint height
= t
->Image
[baseLevel
]->Height2
;
487 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
488 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
498 if (i
>= minLevel
&& i
<= maxLevel
) {
500 incomplete(t
, "3D Image[i] == NULL");
501 t
->Complete
= GL_FALSE
;
504 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
505 t
->Complete
= GL_FALSE
;
506 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
509 if (t
->Image
[i
]->Width2
!= width
) {
510 t
->Complete
= GL_FALSE
;
511 incomplete(t
, "3D Image[i] bad width");
514 if (t
->Image
[i
]->Height2
!= height
) {
515 t
->Complete
= GL_FALSE
;
516 incomplete(t
, "3D Image[i] bad height");
519 if (t
->Image
[i
]->Depth2
!= depth
) {
520 t
->Complete
= GL_FALSE
;
521 incomplete(t
, "3D Image[i] bad depth");
525 if (width
== 1 && height
== 1 && depth
== 1) {
526 return; /* found smallest needed mipmap, all done! */
530 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
531 /* make sure 6 cube faces are consistant */
532 GLuint width
= t
->Image
[baseLevel
]->Width2
;
533 GLuint height
= t
->Image
[baseLevel
]->Height2
;
534 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
541 if (i
>= minLevel
&& i
<= maxLevel
) {
542 /* check that we have images defined */
543 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
544 !t
->PosY
[i
] || !t
->NegY
[i
] ||
545 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
546 t
->Complete
= GL_FALSE
;
547 incomplete(t
, "CubeMap Image[i] == NULL");
550 /* Don't support GL_DEPTH_COMPONENT for cube maps */
551 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
552 t
->Complete
= GL_FALSE
;
553 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
556 /* check that all six images have same size */
557 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
558 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
559 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
560 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
561 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
562 t
->Complete
= GL_FALSE
;
563 incomplete(t
, "CubeMap Image[i] bad size");
567 if (width
== 1 && height
== 1) {
568 return; /* found smallest needed mipmap, all done! */
572 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
573 /* XXX special checking? */
577 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
585 /***********************************************************************/
586 /** \name API functions */
590 * Texture name generation lock.
592 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
593 * of texture IDs is atomic.
595 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
598 * Generate texture names.
600 * \param n number of texture names to be generated.
601 * \param texName an array in which will hold the generated texture names.
603 * \sa glGenTextures().
605 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
606 * to find a block of free texture IDs which are stored in \p texName.
607 * Corresponding empty texture objects are also generated.
610 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
612 GET_CURRENT_CONTEXT(ctx
);
615 ASSERT_OUTSIDE_BEGIN_END(ctx
);
618 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
626 * This must be atomic (generation and allocation of texture IDs)
628 _glthread_LOCK_MUTEX(GenTexturesLock
);
630 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
632 /* Allocate new, empty texture objects */
633 for (i
= 0; i
< n
; i
++) {
634 struct gl_texture_object
*texObj
;
635 GLuint name
= first
+ i
;
637 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
639 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
642 _mesa_save_texture_object(ctx
, texObj
);
646 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
650 * Delete named textures.
652 * \param n number of textures to be deleted.
653 * \param texName array of textures names to be deleted.
655 * \sa glDeleteTextures().
657 * For each texture checks if its bound to any of the texture units, unbinding
658 * it and decrementing the reference count if so. If the texture reference
659 * count is zero, delete its object.
662 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
664 GET_CURRENT_CONTEXT(ctx
);
666 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
672 if (texName
[i
] > 0) {
673 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
674 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
676 /* First check if this texture is currently bound.
677 * If so, unbind it and decrement the reference count.
680 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
681 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
682 if (delObj
== unit
->Current1D
) {
683 unit
->Current1D
= ctx
->Shared
->Default1D
;
684 ctx
->Shared
->Default1D
->RefCount
++;
686 if (delObj
== unit
->_Current
)
687 unit
->_Current
= unit
->Current1D
;
689 else if (delObj
== unit
->Current2D
) {
690 unit
->Current2D
= ctx
->Shared
->Default2D
;
691 ctx
->Shared
->Default2D
->RefCount
++;
693 if (delObj
== unit
->_Current
)
694 unit
->_Current
= unit
->Current2D
;
696 else if (delObj
== unit
->Current3D
) {
697 unit
->Current3D
= ctx
->Shared
->Default3D
;
698 ctx
->Shared
->Default3D
->RefCount
++;
700 if (delObj
== unit
->_Current
)
701 unit
->_Current
= unit
->Current3D
;
703 else if (delObj
== unit
->CurrentCubeMap
) {
704 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
705 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
707 if (delObj
== unit
->_Current
)
708 unit
->_Current
= unit
->CurrentCubeMap
;
710 else if (delObj
== unit
->CurrentRect
) {
711 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
712 ctx
->Shared
->DefaultRect
->RefCount
++;
714 if (delObj
== unit
->_Current
)
715 unit
->_Current
= unit
->CurrentRect
;
718 ctx
->NewState
|= _NEW_TEXTURE
;
720 /* Decrement reference count and delete if zero */
722 ASSERT(delObj
->RefCount
>= 0);
724 if (delObj
->RefCount
== 0) {
725 ASSERT(delObj
->Name
!= 0);
726 _mesa_remove_texture_object(ctx
, delObj
);
727 ASSERT(ctx
->Driver
.DeleteTexture
);
728 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
736 * Bind a named texture to a texturing target.
738 * \param target texture target.
739 * \param texName texture name.
741 * \sa glBindTexture().
743 * Determines the old texture object bound and returns immediately if rebinding
744 * the same texture. Get the current texture which is either a default texture
745 * if name is null, a named texture from the hash, or a new texture if the
746 * given texture name is new. Increments its reference count, binds it, and
747 * calls dd_function_table::BindTexture. Decrements the old texture reference
748 * count and deletes it if it reaches zero.
751 _mesa_BindTexture( GLenum target
, GLuint texName
)
753 GET_CURRENT_CONTEXT(ctx
);
754 GLuint unit
= ctx
->Texture
.CurrentUnit
;
755 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
756 struct gl_texture_object
*oldTexObj
;
757 struct gl_texture_object
*newTexObj
= 0;
758 ASSERT_OUTSIDE_BEGIN_END(ctx
);
760 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
761 _mesa_debug(ctx
, "glBindTexture %s %d\n",
762 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
766 oldTexObj
= texUnit
->Current1D
;
769 oldTexObj
= texUnit
->Current2D
;
772 oldTexObj
= texUnit
->Current3D
;
774 case GL_TEXTURE_CUBE_MAP_ARB
:
775 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
776 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
779 oldTexObj
= texUnit
->CurrentCubeMap
;
781 case GL_TEXTURE_RECTANGLE_NV
:
782 if (!ctx
->Extensions
.NV_texture_rectangle
) {
783 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
786 oldTexObj
= texUnit
->CurrentRect
;
789 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
793 if (oldTexObj
->Name
== texName
)
794 return; /* rebinding the same texture- no change */
797 * Get pointer to new texture object (newTexObj)
800 /* newTexObj = a default texture object */
803 newTexObj
= ctx
->Shared
->Default1D
;
806 newTexObj
= ctx
->Shared
->Default2D
;
809 newTexObj
= ctx
->Shared
->Default3D
;
811 case GL_TEXTURE_CUBE_MAP_ARB
:
812 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
814 case GL_TEXTURE_RECTANGLE_NV
:
815 newTexObj
= ctx
->Shared
->DefaultRect
;
818 ; /* Bad targets are caught above */
822 /* non-default texture object */
823 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
824 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
827 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
828 /* the named texture object's dimensions don't match the target */
829 _mesa_error( ctx
, GL_INVALID_OPERATION
,
830 "glBindTexture(wrong dimensionality)" );
833 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
834 /* have to init wrap and filter state here - kind of klunky */
835 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
836 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
837 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
838 newTexObj
->MinFilter
= GL_LINEAR
;
842 /* if this is a new texture id, allocate a texture object now */
843 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
845 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
848 _mesa_save_texture_object(ctx
, newTexObj
);
850 newTexObj
->Target
= target
;
853 newTexObj
->RefCount
++;
855 /* do the actual binding, but first flush outstanding vertices:
857 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
861 texUnit
->Current1D
= newTexObj
;
864 texUnit
->Current2D
= newTexObj
;
867 texUnit
->Current3D
= newTexObj
;
869 case GL_TEXTURE_CUBE_MAP_ARB
:
870 texUnit
->CurrentCubeMap
= newTexObj
;
872 case GL_TEXTURE_RECTANGLE_NV
:
873 texUnit
->CurrentRect
= newTexObj
;
876 _mesa_problem(ctx
, "bad target in BindTexture");
880 /* Pass BindTexture call to device driver */
881 if (ctx
->Driver
.BindTexture
)
882 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
884 oldTexObj
->RefCount
--;
885 assert(oldTexObj
->RefCount
>= 0);
886 if (oldTexObj
->RefCount
== 0) {
887 assert(oldTexObj
->Name
!= 0);
888 _mesa_remove_texture_object(ctx
, oldTexObj
);
889 ASSERT(ctx
->Driver
.DeleteTexture
);
890 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
895 * Set texture priorities.
897 * \param n number of textures.
898 * \param texName texture names.
899 * \param priorities corresponding texture priorities.
901 * \sa glPrioritizeTextures().
903 * Looks up each texture in the hash, clamps the corresponding priority between
904 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
907 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
908 const GLclampf
*priorities
)
910 GET_CURRENT_CONTEXT(ctx
);
912 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
915 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
922 for (i
= 0; i
< n
; i
++) {
923 if (texName
[i
] > 0) {
924 struct gl_texture_object
*t
= (struct gl_texture_object
*)
925 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
927 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
928 if (ctx
->Driver
.PrioritizeTexture
)
929 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
934 ctx
->NewState
|= _NEW_TEXTURE
;
938 * See if textures are loaded in texture memory.
940 * \param n number of textures to query.
941 * \param texName array with the texture names.
942 * \param residences array which will hold the residence status.
944 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
946 * \sa glAreTexturesResident().
948 * Looks up each texture in the hash and calls
949 * dd_function_table::IsTextureResident.
952 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
953 GLboolean
*residences
)
955 GET_CURRENT_CONTEXT(ctx
);
956 GLboolean allResident
= GL_TRUE
;
958 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
961 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
965 if (!texName
|| !residences
)
968 for (i
= 0; i
< n
; i
++) {
969 struct gl_texture_object
*t
;
970 if (texName
[i
] == 0) {
971 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
974 t
= (struct gl_texture_object
*)
975 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
977 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
980 if (!ctx
->Driver
.IsTextureResident
||
981 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
982 /* The texture is resident */
984 residences
[i
] = GL_TRUE
;
987 /* The texture is not resident */
989 allResident
= GL_FALSE
;
990 for (j
= 0; j
< i
; j
++)
991 residences
[j
] = GL_TRUE
;
993 residences
[i
] = GL_FALSE
;
1001 * See if a name corresponds to a texture.
1003 * \param texture texture name.
1005 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1008 * \sa glIsTexture().
1010 * Calls _mesa_HashLookup().
1012 GLboolean GLAPIENTRY
1013 _mesa_IsTexture( GLuint texture
)
1015 GET_CURRENT_CONTEXT(ctx
);
1016 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1017 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);