68d9845bbf36147e13a8fd2e45fce23a09b84dd6
1 /* $Id: texobj.c,v 1.68 2003/04/01 18:10:10 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 * Allocate and initialize a new texture object
42 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
44 * \param ctx the rendering context
45 * \param name the integer name for the texture object
46 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
47 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV
48 * zero is ok for the sake of GenTextures()
49 * \return pointer to new texture object
51 struct gl_texture_object
*
52 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
54 struct gl_texture_object
*obj
;
55 obj
= CALLOC_STRUCT(gl_texture_object
);
56 _mesa_initialize_texture_object(obj
, name
, target
);
62 * Initialize a texture object to default values.
63 * \param obj the texture object
64 * \param name the texture name
65 * \param target the texture target
68 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
69 GLuint name
, GLenum target
)
72 target
== GL_TEXTURE_1D
||
73 target
== GL_TEXTURE_2D
||
74 target
== GL_TEXTURE_3D
||
75 target
== GL_TEXTURE_CUBE_MAP_ARB
||
76 target
== GL_TEXTURE_RECTANGLE_NV
);
78 /* init the non-zero fields */
79 _glthread_INIT_MUTEX(obj
->Mutex
);
84 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
85 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
86 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
87 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
88 obj
->MinFilter
= GL_LINEAR
;
91 obj
->WrapS
= GL_REPEAT
;
92 obj
->WrapT
= GL_REPEAT
;
93 obj
->WrapR
= GL_REPEAT
;
94 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
96 obj
->MagFilter
= GL_LINEAR
;
97 obj
->MinLod
= -1000.0;
100 obj
->MaxLevel
= 1000;
101 obj
->MaxAnisotropy
= 1.0;
102 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
103 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
104 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
105 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
106 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
107 obj
->ShadowAmbient
= 0.0F
; /* ARB/SGIX_shadow_ambient */
108 _mesa_init_colortable(&obj
->Palette
);
113 * Deallocate a texture object. It should have already been removed from
114 * the texture object pool.
115 * \param texObj the texture object to deallocate
118 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
126 _mesa_free_colortable_data(&texObj
->Palette
);
128 /* free the texture images */
129 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
130 if (texObj
->Image
[i
]) {
131 _mesa_delete_texture_image( texObj
->Image
[i
] );
135 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
136 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
138 /* free this object */
144 * Add the given texture object to the texture object pool.
147 _mesa_save_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
149 /* insert into linked list */
150 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
151 texObj
->Next
= ctx
->Shared
->TexObjectList
;
152 ctx
->Shared
->TexObjectList
= texObj
;
153 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
155 if (texObj
->Name
> 0) {
156 /* insert into hash table */
157 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
163 * Remove the given texture object from the texture object pool.
164 * Do not deallocate the texture object though.
167 _mesa_remove_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
169 struct gl_texture_object
*tprev
, *tcurr
;
171 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
173 /* unlink from the linked list */
175 tcurr
= ctx
->Shared
->TexObjectList
;
177 if (tcurr
== texObj
) {
179 tprev
->Next
= texObj
->Next
;
182 ctx
->Shared
->TexObjectList
= texObj
->Next
;
190 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
192 if (texObj
->Name
> 0) {
193 /* remove from hash table */
194 _mesa_HashRemove(ctx
->Shared
->TexObjects
, texObj
->Name
);
200 * Copy texture object state from one texture object to another.
203 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
204 const struct gl_texture_object
*src
)
206 dest
->Name
= src
->Name
;
207 dest
->Priority
= src
->Priority
;
208 dest
->BorderColor
[0] = src
->BorderColor
[0];
209 dest
->BorderColor
[1] = src
->BorderColor
[1];
210 dest
->BorderColor
[2] = src
->BorderColor
[2];
211 dest
->BorderColor
[3] = src
->BorderColor
[3];
212 dest
->WrapS
= src
->WrapS
;
213 dest
->WrapT
= src
->WrapT
;
214 dest
->WrapR
= src
->WrapR
;
215 dest
->MinFilter
= src
->MinFilter
;
216 dest
->MagFilter
= src
->MagFilter
;
217 dest
->MinLod
= src
->MinLod
;
218 dest
->MaxLod
= src
->MaxLod
;
219 dest
->BaseLevel
= src
->BaseLevel
;
220 dest
->MaxLevel
= src
->MaxLevel
;
221 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
222 dest
->CompareFlag
= src
->CompareFlag
;
223 dest
->CompareOperator
= src
->CompareOperator
;
224 dest
->ShadowAmbient
= src
->ShadowAmbient
;
225 dest
->CompareMode
= src
->CompareMode
;
226 dest
->CompareFunc
= src
->CompareFunc
;
227 dest
->DepthMode
= src
->DepthMode
;
228 dest
->_MaxLevel
= src
->_MaxLevel
;
229 dest
->_MaxLambda
= src
->_MaxLambda
;
230 dest
->GenerateMipmap
= src
->GenerateMipmap
;
231 dest
->Palette
= src
->Palette
;
232 dest
->Complete
= src
->Complete
;
237 * Report why a texture object is incomplete. (for debug only)
241 incomplete(const struct gl_texture_object
*t
, const char *why
)
243 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
246 #define incomplete(a, b)
251 * Examine a texture object to determine if it is complete.
252 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
255 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
256 struct gl_texture_object
*t
)
258 const GLint baseLevel
= t
->BaseLevel
;
259 GLint maxLog2
= 0, maxLevels
= 0;
261 t
->Complete
= GL_TRUE
; /* be optimistic */
263 /* Always need the base level image */
264 if (!t
->Image
[baseLevel
]) {
265 incomplete(t
, "Image[baseLevel] == NULL");
266 t
->Complete
= GL_FALSE
;
270 /* Compute _MaxLevel */
271 if (t
->Target
== GL_TEXTURE_1D
) {
272 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
273 maxLevels
= ctx
->Const
.MaxTextureLevels
;
275 else if (t
->Target
== GL_TEXTURE_2D
) {
276 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
277 t
->Image
[baseLevel
]->HeightLog2
);
278 maxLevels
= ctx
->Const
.MaxTextureLevels
;
280 else if (t
->Target
== GL_TEXTURE_3D
) {
281 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
282 t
->Image
[baseLevel
]->HeightLog2
);
283 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
284 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
286 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
287 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
288 t
->Image
[baseLevel
]->HeightLog2
);
289 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
291 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
292 maxLog2
= 0; /* not applicable */
293 maxLevels
= 1; /* no mipmapping */
296 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
300 ASSERT(maxLevels
> 0);
302 t
->_MaxLevel
= baseLevel
+ maxLog2
;
303 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
304 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
306 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
307 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
309 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
310 /* make sure that all six cube map level 0 images are the same size */
311 const GLuint w
= t
->Image
[baseLevel
]->Width2
;
312 const GLuint h
= t
->Image
[baseLevel
]->Height2
;
313 if (!t
->NegX
[baseLevel
] ||
314 t
->NegX
[baseLevel
]->Width2
!= w
||
315 t
->NegX
[baseLevel
]->Height2
!= h
||
316 !t
->PosY
[baseLevel
] ||
317 t
->PosY
[baseLevel
]->Width2
!= w
||
318 t
->PosY
[baseLevel
]->Height2
!= h
||
319 !t
->NegY
[baseLevel
] ||
320 t
->NegY
[baseLevel
]->Width2
!= w
||
321 t
->NegY
[baseLevel
]->Height2
!= h
||
322 !t
->PosZ
[baseLevel
] ||
323 t
->PosZ
[baseLevel
]->Width2
!= w
||
324 t
->PosZ
[baseLevel
]->Height2
!= h
||
325 !t
->NegZ
[baseLevel
] ||
326 t
->NegZ
[baseLevel
]->Width2
!= w
||
327 t
->NegZ
[baseLevel
]->Height2
!= h
) {
328 t
->Complete
= GL_FALSE
;
329 incomplete(t
, "Non-quare cubemap image");
334 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
336 * Mipmapping: determine if we have a complete set of mipmaps
339 GLint minLevel
= baseLevel
;
340 GLint maxLevel
= t
->_MaxLevel
;
342 if (minLevel
> maxLevel
) {
343 t
->Complete
= GL_FALSE
;
344 incomplete(t
, "minLevel > maxLevel");
348 /* Test dimension-independent attributes */
349 for (i
= minLevel
; i
<= maxLevel
; i
++) {
351 if (t
->Image
[i
]->TexFormat
!= t
->Image
[baseLevel
]->TexFormat
) {
352 t
->Complete
= GL_FALSE
;
353 incomplete(t
, "Format[i] != Format[baseLevel]");
356 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
357 t
->Complete
= GL_FALSE
;
358 incomplete(t
, "Border[i] != Border[baseLevel]");
364 /* Test things which depend on number of texture image dimensions */
365 if (t
->Target
== GL_TEXTURE_1D
) {
366 /* Test 1-D mipmaps */
367 GLuint width
= t
->Image
[baseLevel
]->Width2
;
368 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
372 if (i
>= minLevel
&& i
<= maxLevel
) {
374 t
->Complete
= GL_FALSE
;
375 incomplete(t
, "1D Image[i] == NULL");
378 if (t
->Image
[i
]->Width2
!= width
) {
379 t
->Complete
= GL_FALSE
;
380 incomplete(t
, "1D Image[i] bad width");
385 return; /* found smallest needed mipmap, all done! */
389 else if (t
->Target
== GL_TEXTURE_2D
) {
390 /* Test 2-D mipmaps */
391 GLuint width
= t
->Image
[baseLevel
]->Width2
;
392 GLuint height
= t
->Image
[baseLevel
]->Height2
;
393 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
400 if (i
>= minLevel
&& i
<= maxLevel
) {
402 t
->Complete
= GL_FALSE
;
403 incomplete(t
, "2D Image[i] == NULL");
406 if (t
->Image
[i
]->Width2
!= width
) {
407 t
->Complete
= GL_FALSE
;
408 incomplete(t
, "2D Image[i] bad width");
411 if (t
->Image
[i
]->Height2
!= height
) {
412 t
->Complete
= GL_FALSE
;
413 incomplete(t
, "2D Image[i] bad height");
416 if (width
==1 && height
==1) {
417 return; /* found smallest needed mipmap, all done! */
422 else if (t
->Target
== GL_TEXTURE_3D
) {
423 /* Test 3-D mipmaps */
424 GLuint width
= t
->Image
[baseLevel
]->Width2
;
425 GLuint height
= t
->Image
[baseLevel
]->Height2
;
426 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
427 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
437 if (i
>= minLevel
&& i
<= maxLevel
) {
439 incomplete(t
, "3D Image[i] == NULL");
440 t
->Complete
= GL_FALSE
;
443 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
444 t
->Complete
= GL_FALSE
;
445 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
448 if (t
->Image
[i
]->Width2
!= width
) {
449 t
->Complete
= GL_FALSE
;
450 incomplete(t
, "3D Image[i] bad width");
453 if (t
->Image
[i
]->Height2
!= height
) {
454 t
->Complete
= GL_FALSE
;
455 incomplete(t
, "3D Image[i] bad height");
458 if (t
->Image
[i
]->Depth2
!= depth
) {
459 t
->Complete
= GL_FALSE
;
460 incomplete(t
, "3D Image[i] bad depth");
464 if (width
== 1 && height
== 1 && depth
== 1) {
465 return; /* found smallest needed mipmap, all done! */
469 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
470 /* make sure 6 cube faces are consistant */
471 GLuint width
= t
->Image
[baseLevel
]->Width2
;
472 GLuint height
= t
->Image
[baseLevel
]->Height2
;
473 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
480 if (i
>= minLevel
&& i
<= maxLevel
) {
481 /* check that we have images defined */
482 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
483 !t
->PosY
[i
] || !t
->NegY
[i
] ||
484 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
485 t
->Complete
= GL_FALSE
;
486 incomplete(t
, "CubeMap Image[i] == NULL");
489 /* Don't support GL_DEPTH_COMPONENT for cube maps */
490 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
491 t
->Complete
= GL_FALSE
;
492 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
495 /* check that all six images have same size */
496 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
497 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
498 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
499 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
500 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
501 t
->Complete
= GL_FALSE
;
502 incomplete(t
, "CubeMap Image[i] bad size");
506 if (width
== 1 && height
== 1) {
507 return; /* found smallest needed mipmap, all done! */
511 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
512 /* XXX special checking? */
517 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
523 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
527 * Execute glGenTextures
530 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
532 GET_CURRENT_CONTEXT(ctx
);
535 ASSERT_OUTSIDE_BEGIN_END(ctx
);
538 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
546 * This must be atomic (generation and allocation of texture IDs)
548 _glthread_LOCK_MUTEX(GenTexturesLock
);
550 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
552 /* Return the texture names */
554 texName
[i
] = first
+ i
;
557 /* Allocate new, empty texture objects */
558 for (i
= 0; i
< n
; i
++) {
559 struct gl_texture_object
*texObj
;
560 GLuint name
= first
+ i
;
562 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
564 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
567 _mesa_save_texture_object(ctx
, texObj
);
570 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
576 * Execute glDeleteTextures
579 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
581 GET_CURRENT_CONTEXT(ctx
);
583 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
589 if (texName
[i
] > 0) {
590 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
591 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
593 /* First check if this texture is currently bound.
594 * If so, unbind it and decrement the reference count.
597 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
598 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
599 if (delObj
== unit
->Current1D
) {
600 unit
->Current1D
= ctx
->Shared
->Default1D
;
601 ctx
->Shared
->Default1D
->RefCount
++;
603 if (delObj
== unit
->_Current
)
604 unit
->_Current
= unit
->Current1D
;
606 else if (delObj
== unit
->Current2D
) {
607 unit
->Current2D
= ctx
->Shared
->Default2D
;
608 ctx
->Shared
->Default2D
->RefCount
++;
610 if (delObj
== unit
->_Current
)
611 unit
->_Current
= unit
->Current2D
;
613 else if (delObj
== unit
->Current3D
) {
614 unit
->Current3D
= ctx
->Shared
->Default3D
;
615 ctx
->Shared
->Default3D
->RefCount
++;
617 if (delObj
== unit
->_Current
)
618 unit
->_Current
= unit
->Current3D
;
620 else if (delObj
== unit
->CurrentCubeMap
) {
621 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
622 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
624 if (delObj
== unit
->_Current
)
625 unit
->_Current
= unit
->CurrentCubeMap
;
627 else if (delObj
== unit
->CurrentRect
) {
628 unit
->CurrentRect
= ctx
->Shared
->DefaultRect
;
629 ctx
->Shared
->DefaultRect
->RefCount
++;
631 if (delObj
== unit
->_Current
)
632 unit
->_Current
= unit
->CurrentRect
;
635 ctx
->NewState
|= _NEW_TEXTURE
;
637 /* Decrement reference count and delete if zero */
639 ASSERT(delObj
->RefCount
>= 0);
641 if (delObj
->RefCount
== 0) {
642 ASSERT(delObj
->Name
!= 0);
643 _mesa_remove_texture_object(ctx
, delObj
);
644 ASSERT(ctx
->Driver
.DeleteTexture
);
645 (*ctx
->Driver
.DeleteTexture
)(ctx
, delObj
);
655 * Execute glBindTexture
658 _mesa_BindTexture( GLenum target
, GLuint texName
)
660 GET_CURRENT_CONTEXT(ctx
);
661 GLuint unit
= ctx
->Texture
.CurrentUnit
;
662 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
663 struct gl_texture_object
*oldTexObj
;
664 struct gl_texture_object
*newTexObj
= 0;
665 ASSERT_OUTSIDE_BEGIN_END(ctx
);
667 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
668 _mesa_debug(ctx
, "glBindTexture %s %d\n",
669 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
673 oldTexObj
= texUnit
->Current1D
;
676 oldTexObj
= texUnit
->Current2D
;
679 oldTexObj
= texUnit
->Current3D
;
681 case GL_TEXTURE_CUBE_MAP_ARB
:
682 if (!ctx
->Extensions
.ARB_texture_cube_map
) {
683 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
686 oldTexObj
= texUnit
->CurrentCubeMap
;
688 case GL_TEXTURE_RECTANGLE_NV
:
689 if (!ctx
->Extensions
.NV_texture_rectangle
) {
690 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
693 oldTexObj
= texUnit
->CurrentRect
;
696 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
700 if (oldTexObj
->Name
== texName
)
701 return; /* rebinding the same texture- no change */
704 * Get pointer to new texture object (newTexObj)
707 /* newTexObj = a default texture object */
710 newTexObj
= ctx
->Shared
->Default1D
;
713 newTexObj
= ctx
->Shared
->Default2D
;
716 newTexObj
= ctx
->Shared
->Default3D
;
718 case GL_TEXTURE_CUBE_MAP_ARB
:
719 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
721 case GL_TEXTURE_RECTANGLE_NV
:
722 newTexObj
= ctx
->Shared
->DefaultRect
;
725 ; /* Bad targets are caught above */
729 /* non-default texture object */
730 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
731 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
734 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
735 /* the named texture object's dimensions don't match the target */
736 _mesa_error( ctx
, GL_INVALID_OPERATION
,
737 "glBindTexture(wrong dimensionality)" );
740 if (newTexObj
->Target
== 0 && target
== GL_TEXTURE_RECTANGLE_NV
) {
741 /* have to init wrap and filter state here - kind of klunky */
742 newTexObj
->WrapS
= GL_CLAMP_TO_EDGE
;
743 newTexObj
->WrapT
= GL_CLAMP_TO_EDGE
;
744 newTexObj
->WrapR
= GL_CLAMP_TO_EDGE
;
745 newTexObj
->MinFilter
= GL_LINEAR
;
749 /* if this is a new texture id, allocate a texture object now */
750 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
752 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
755 _mesa_save_texture_object(ctx
, newTexObj
);
757 newTexObj
->Target
= target
;
760 newTexObj
->RefCount
++;
762 /* do the actual binding, but first flush outstanding vertices:
764 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
768 texUnit
->Current1D
= newTexObj
;
771 texUnit
->Current2D
= newTexObj
;
774 texUnit
->Current3D
= newTexObj
;
776 case GL_TEXTURE_CUBE_MAP_ARB
:
777 texUnit
->CurrentCubeMap
= newTexObj
;
779 case GL_TEXTURE_RECTANGLE_NV
:
780 texUnit
->CurrentRect
= newTexObj
;
783 _mesa_problem(ctx
, "bad target in BindTexture");
787 /* Pass BindTexture call to device driver */
788 if (ctx
->Driver
.BindTexture
)
789 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
791 oldTexObj
->RefCount
--;
792 assert(oldTexObj
->RefCount
>= 0);
793 if (oldTexObj
->RefCount
== 0) {
794 assert(oldTexObj
->Name
!= 0);
795 _mesa_remove_texture_object(ctx
, oldTexObj
);
796 ASSERT(ctx
->Driver
.DeleteTexture
);
797 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
804 * Execute glPrioritizeTextures
807 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
808 const GLclampf
*priorities
)
810 GET_CURRENT_CONTEXT(ctx
);
812 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
815 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
822 for (i
= 0; i
< n
; i
++) {
823 if (texName
[i
] > 0) {
824 struct gl_texture_object
*t
= (struct gl_texture_object
*)
825 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
827 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
828 if (ctx
->Driver
.PrioritizeTexture
)
829 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
834 ctx
->NewState
|= _NEW_TEXTURE
;
840 * Execute glAreTexturesResident
843 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
844 GLboolean
*residences
)
846 GET_CURRENT_CONTEXT(ctx
);
847 GLboolean allResident
= GL_TRUE
;
849 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
852 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
856 if (!texName
|| !residences
)
859 for (i
= 0; i
< n
; i
++) {
860 struct gl_texture_object
*t
;
861 if (texName
[i
] == 0) {
862 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
865 t
= (struct gl_texture_object
*)
866 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
868 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
871 if (!ctx
->Driver
.IsTextureResident
||
872 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
873 /* The texture is resident */
875 residences
[i
] = GL_TRUE
;
878 /* The texture is not resident */
880 allResident
= GL_FALSE
;
881 for (j
= 0; j
< i
; j
++)
882 residences
[j
] = GL_TRUE
;
884 residences
[i
] = GL_FALSE
;
894 * Execute glIsTexture
897 _mesa_IsTexture( GLuint texture
)
899 GET_CURRENT_CONTEXT(ctx
);
900 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
901 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);