1 /* $Id: texobj.c,v 1.52 2002/02/15 16:32:06 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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.
47 * Allocate a new texture object and add it to the linked list of texture
48 * objects. If name>0 then also insert the new texture object into the hash
50 * Input: shared - the shared GL state structure to contain the texture object
51 * name - integer name for the texture object
52 * dimensions - either 1, 2, 3 or 6 (cube map)
53 * zero is ok for the sake of GenTextures()
54 * Return: pointer to new texture object
56 struct gl_texture_object
*
57 _mesa_alloc_texture_object( struct gl_shared_state
*shared
,
58 GLuint name
, GLuint dimensions
)
60 struct gl_texture_object
*obj
;
62 ASSERT(dimensions
<= 3 || dimensions
== 6);
64 obj
= CALLOC_STRUCT(gl_texture_object
);
67 /* init the non-zero fields */
68 _glthread_INIT_MUTEX(obj
->Mutex
);
71 obj
->Dimensions
= dimensions
;
73 obj
->WrapS
= GL_REPEAT
;
74 obj
->WrapT
= GL_REPEAT
;
75 obj
->WrapR
= GL_REPEAT
;
76 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
77 obj
->MagFilter
= GL_LINEAR
;
78 obj
->MinLod
= -1000.0;
82 obj
->MaxAnisotropy
= 1.0;
83 obj
->CompareFlag
= GL_FALSE
; /* SGIX_shadow */
84 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
; /* SGIX_shadow */
85 obj
->CompareMode
= GL_LUMINANCE
; /* ARB_shadow */
86 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
87 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
88 obj
->ShadowAmbient
= 0; /* ARB/SGIX_shadow_ambient */
89 _mesa_init_colortable(&obj
->Palette
);
91 /* insert into linked list */
93 _glthread_LOCK_MUTEX(shared
->Mutex
);
94 obj
->Next
= shared
->TexObjectList
;
95 shared
->TexObjectList
= obj
;
96 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
100 /* insert into hash table */
101 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
109 * Deallocate a texture object struct and remove it from the given
111 * Input: shared - the shared GL state to which the object belongs
112 * t - the texture object to delete
114 void _mesa_free_texture_object( struct gl_shared_state
*shared
,
115 struct gl_texture_object
*t
)
117 struct gl_texture_object
*tprev
, *tcurr
;
121 /* unlink t from the linked list */
123 _glthread_LOCK_MUTEX(shared
->Mutex
);
125 tcurr
= shared
->TexObjectList
;
129 tprev
->Next
= t
->Next
;
132 shared
->TexObjectList
= t
->Next
;
139 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
143 /* remove from hash table */
144 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
147 _mesa_free_colortable_data(&t
->Palette
);
149 /* free the texture images */
152 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
154 _mesa_free_texture_image( t
->Image
[i
] );
159 /* free this object */
165 * Report why a texture object is incomplete. (for debug only)
169 incomplete(const struct gl_texture_object
*t
, const char *why
)
171 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
174 #define incomplete(a, b)
179 * Examine a texture object to determine if it is complete.
180 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
183 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
184 struct gl_texture_object
*t
)
186 const GLint baseLevel
= t
->BaseLevel
;
187 GLint maxLog2
= 0, maxLevels
= 0;
189 t
->Complete
= GL_TRUE
; /* be optimistic */
191 /* Always need the base level image */
192 if (!t
->Image
[baseLevel
]) {
193 incomplete(t
, "Image[baseLevel] == NULL");
194 t
->Complete
= GL_FALSE
;
198 /* Compute _MaxLevel */
199 if (t
->Dimensions
== 1) {
200 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
201 maxLevels
= ctx
->Const
.MaxTextureLevels
;
203 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
204 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
205 t
->Image
[baseLevel
]->HeightLog2
);
206 maxLevels
= (t
->Dimensions
== 2) ?
207 ctx
->Const
.MaxTextureLevels
: ctx
->Const
.MaxCubeTextureLevels
;
209 else if (t
->Dimensions
== 3) {
210 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
211 t
->Image
[baseLevel
]->HeightLog2
);
212 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
213 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
216 _mesa_problem(ctx
, "Bad t->Dimension in _mesa_test_texobj_completeness");
220 ASSERT(maxLevels
> 0);
222 t
->_MaxLevel
= baseLevel
+ maxLog2
;
223 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
224 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
226 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
227 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
229 if (t
->Dimensions
== 6) {
230 /* make sure that all six cube map level 0 images are the same size */
231 const GLuint w
= t
->Image
[baseLevel
]->Width2
;
232 const GLuint h
= t
->Image
[baseLevel
]->Height2
;
233 if (!t
->NegX
[baseLevel
] ||
234 t
->NegX
[baseLevel
]->Width2
!= w
||
235 t
->NegX
[baseLevel
]->Height2
!= h
||
236 !t
->PosY
[baseLevel
] ||
237 t
->PosY
[baseLevel
]->Width2
!= w
||
238 t
->PosY
[baseLevel
]->Height2
!= h
||
239 !t
->NegY
[baseLevel
] ||
240 t
->NegY
[baseLevel
]->Width2
!= w
||
241 t
->NegY
[baseLevel
]->Height2
!= h
||
242 !t
->PosZ
[baseLevel
] ||
243 t
->PosZ
[baseLevel
]->Width2
!= w
||
244 t
->PosZ
[baseLevel
]->Height2
!= h
||
245 !t
->NegZ
[baseLevel
] ||
246 t
->NegZ
[baseLevel
]->Width2
!= w
||
247 t
->NegZ
[baseLevel
]->Height2
!= h
) {
248 t
->Complete
= GL_FALSE
;
249 incomplete(t
, "Non-quare cubemap image");
254 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
256 * Mipmapping: determine if we have a complete set of mipmaps
259 GLint minLevel
= baseLevel
;
260 GLint maxLevel
= t
->_MaxLevel
;
262 if (minLevel
> maxLevel
) {
263 t
->Complete
= GL_FALSE
;
264 incomplete(t
, "minLevel > maxLevel");
268 /* Test dimension-independent attributes */
269 for (i
= minLevel
; i
<= maxLevel
; i
++) {
271 if (t
->Image
[i
]->TexFormat
!= t
->Image
[baseLevel
]->TexFormat
) {
272 t
->Complete
= GL_FALSE
;
273 incomplete(t
, "Format[i] != Format[baseLevel]");
276 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
277 t
->Complete
= GL_FALSE
;
278 incomplete(t
, "Border[i] != Border[baseLevel]");
284 /* Test things which depend on number of texture image dimensions */
285 if (t
->Dimensions
== 1) {
286 /* Test 1-D mipmaps */
287 GLuint width
= t
->Image
[baseLevel
]->Width2
;
288 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
292 if (i
>= minLevel
&& i
<= maxLevel
) {
294 t
->Complete
= GL_FALSE
;
295 incomplete(t
, "1D Image[i] == NULL");
298 if (t
->Image
[i
]->Width2
!= width
) {
299 t
->Complete
= GL_FALSE
;
300 incomplete(t
, "1D Image[i] bad width");
305 return; /* found smallest needed mipmap, all done! */
309 else if (t
->Dimensions
== 2) {
310 /* Test 2-D mipmaps */
311 GLuint width
= t
->Image
[baseLevel
]->Width2
;
312 GLuint height
= t
->Image
[baseLevel
]->Height2
;
313 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
320 if (i
>= minLevel
&& i
<= maxLevel
) {
322 t
->Complete
= GL_FALSE
;
323 incomplete(t
, "2D Image[i] == NULL");
326 if (t
->Image
[i
]->Width2
!= width
) {
327 t
->Complete
= GL_FALSE
;
328 incomplete(t
, "2D Image[i] bad width");
331 if (t
->Image
[i
]->Height2
!= height
) {
332 t
->Complete
= GL_FALSE
;
333 incomplete(t
, "2D Image[i] bad height");
336 if (width
==1 && height
==1) {
337 return; /* found smallest needed mipmap, all done! */
342 else if (t
->Dimensions
== 3) {
343 /* Test 3-D mipmaps */
344 GLuint width
= t
->Image
[baseLevel
]->Width2
;
345 GLuint height
= t
->Image
[baseLevel
]->Height2
;
346 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
347 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
357 if (i
>= minLevel
&& i
<= maxLevel
) {
359 incomplete(t
, "3D Image[i] == NULL");
360 t
->Complete
= GL_FALSE
;
363 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
364 t
->Complete
= GL_FALSE
;
365 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
368 if (t
->Image
[i
]->Width2
!= width
) {
369 t
->Complete
= GL_FALSE
;
370 incomplete(t
, "3D Image[i] bad width");
373 if (t
->Image
[i
]->Height2
!= height
) {
374 t
->Complete
= GL_FALSE
;
375 incomplete(t
, "3D Image[i] bad height");
378 if (t
->Image
[i
]->Depth2
!= depth
) {
379 t
->Complete
= GL_FALSE
;
380 incomplete(t
, "3D Image[i] bad depth");
384 if (width
== 1 && height
== 1 && depth
== 1) {
385 return; /* found smallest needed mipmap, all done! */
389 else if (t
->Dimensions
== 6) {
390 /* make sure 6 cube faces are consistant */
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
) {
401 /* Don't support GL_DEPTH_COMPONENT for cube maps */
402 if (t
->Image
[i
]->Format
== GL_DEPTH_COMPONENT
) {
403 t
->Complete
= GL_FALSE
;
404 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
407 /* check that we have images defined */
408 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
409 !t
->PosY
[i
] || !t
->NegY
[i
] ||
410 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
411 t
->Complete
= GL_FALSE
;
412 incomplete(t
, "CubeMap Image[i] == NULL");
415 /* check that all six images have same size */
416 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
417 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
418 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
419 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
420 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
421 t
->Complete
= GL_FALSE
;
422 incomplete(t
, "CubeMap Image[i] bad size");
426 if (width
== 1 && height
== 1) {
427 return; /* found smallest needed mipmap, all done! */
432 /* Dimensions = ??? */
433 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
439 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
443 * Execute glGenTextures
446 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
448 GET_CURRENT_CONTEXT(ctx
);
451 ASSERT_OUTSIDE_BEGIN_END(ctx
);
454 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
462 * This must be atomic (generation and allocation of texture IDs)
464 _glthread_LOCK_MUTEX(GenTexturesLock
);
466 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
468 /* Return the texture names */
470 texName
[i
] = first
+ i
;
473 /* Allocate new, empty texture objects */
475 GLuint name
= first
+ i
;
477 (void) _mesa_alloc_texture_object( ctx
->Shared
, name
, dims
);
480 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
486 * Execute glDeleteTextures
489 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
491 GET_CURRENT_CONTEXT(ctx
);
493 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
499 if (texName
[i
] > 0) {
500 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
501 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
503 /* First check if this texture is currently bound.
504 * If so, unbind it and decrement the reference count.
507 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
508 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
509 if (delObj
== unit
->Current1D
) {
510 unit
->Current1D
= ctx
->Shared
->Default1D
;
511 ctx
->Shared
->Default1D
->RefCount
++;
512 if (delObj
== unit
->_Current
)
513 unit
->_Current
= unit
->Current1D
;
515 else if (delObj
== unit
->Current2D
) {
516 unit
->Current2D
= ctx
->Shared
->Default2D
;
517 ctx
->Shared
->Default2D
->RefCount
++;
518 if (delObj
== unit
->_Current
)
519 unit
->_Current
= unit
->Current2D
;
521 else if (delObj
== unit
->Current3D
) {
522 unit
->Current3D
= ctx
->Shared
->Default3D
;
523 ctx
->Shared
->Default3D
->RefCount
++;
524 if (delObj
== unit
->_Current
)
525 unit
->_Current
= unit
->Current3D
;
527 else if (delObj
== unit
->CurrentCubeMap
) {
528 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
529 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
530 if (delObj
== unit
->_Current
)
531 unit
->_Current
= unit
->CurrentCubeMap
;
534 ctx
->NewState
|= _NEW_TEXTURE
;
536 /* Decrement reference count and delete if zero */
538 ASSERT(delObj
->RefCount
>= 0);
540 if (delObj
->RefCount
== 0) {
541 ASSERT(delObj
->Name
!= 0);
542 if (ctx
->Driver
.DeleteTexture
)
543 (*ctx
->Driver
.DeleteTexture
)( ctx
, delObj
);
544 _mesa_free_texture_object(ctx
->Shared
, delObj
);
554 * Execute glBindTexture
557 _mesa_BindTexture( GLenum target
, GLuint texName
)
559 GET_CURRENT_CONTEXT(ctx
);
560 GLuint unit
= ctx
->Texture
.CurrentUnit
;
561 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
562 struct gl_texture_object
*oldTexObj
;
563 struct gl_texture_object
*newTexObj
= 0;
565 ASSERT_OUTSIDE_BEGIN_END(ctx
);
567 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
568 fprintf(stderr
, "glBindTexture %s %d\n",
569 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
574 oldTexObj
= texUnit
->Current1D
;
578 oldTexObj
= texUnit
->Current2D
;
582 oldTexObj
= texUnit
->Current3D
;
584 case GL_TEXTURE_CUBE_MAP_ARB
:
585 if (ctx
->Extensions
.ARB_texture_cube_map
) {
587 oldTexObj
= texUnit
->CurrentCubeMap
;
592 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
596 if (oldTexObj
->Name
== texName
)
597 return; /* rebinding the same texture- no change */
600 * Get pointer to new texture object (newTexObj)
603 /* newTexObj = a default texture object */
606 newTexObj
= ctx
->Shared
->Default1D
;
609 newTexObj
= ctx
->Shared
->Default2D
;
612 newTexObj
= ctx
->Shared
->Default3D
;
614 case GL_TEXTURE_CUBE_MAP_ARB
:
615 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
618 ; /* Bad targets are caught above */
622 /* non-default texture object */
623 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
624 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
627 if (newTexObj
->Dimensions
> 0 && newTexObj
->Dimensions
!= targetDim
) {
628 /* the named texture object's dimensions don't match the target */
629 _mesa_error( ctx
, GL_INVALID_OPERATION
,
630 "glBindTexture(wrong dimensionality)" );
635 /* if this is a new texture id, allocate a texture object now */
636 newTexObj
= _mesa_alloc_texture_object( ctx
->Shared
, texName
,
639 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
643 newTexObj
->Dimensions
= targetDim
;
646 newTexObj
->RefCount
++;
648 /* do the actual binding, but first flush outstanding vertices:
650 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
654 texUnit
->Current1D
= newTexObj
;
657 texUnit
->Current2D
= newTexObj
;
660 texUnit
->Current3D
= newTexObj
;
662 case GL_TEXTURE_CUBE_MAP_ARB
:
663 texUnit
->CurrentCubeMap
= newTexObj
;
666 _mesa_problem(ctx
, "bad target in BindTexture");
669 /* Pass BindTexture call to device driver */
670 if (ctx
->Driver
.BindTexture
)
671 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
673 oldTexObj
->RefCount
--;
674 assert(oldTexObj
->RefCount
>= 0);
675 if (oldTexObj
->RefCount
== 0) {
676 assert(oldTexObj
->Name
!= 0);
677 if (ctx
->Driver
.DeleteTexture
) {
678 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
680 _mesa_free_texture_object(ctx
->Shared
, oldTexObj
);
687 * Execute glPrioritizeTextures
690 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
691 const GLclampf
*priorities
)
693 GET_CURRENT_CONTEXT(ctx
);
695 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
698 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
705 for (i
= 0; i
< n
; i
++) {
706 if (texName
[i
] > 0) {
707 struct gl_texture_object
*t
= (struct gl_texture_object
*)
708 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
710 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
711 if (ctx
->Driver
.PrioritizeTexture
)
712 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
717 ctx
->NewState
|= _NEW_TEXTURE
;
723 * Execute glAreTexturesResident
726 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
727 GLboolean
*residences
)
729 GET_CURRENT_CONTEXT(ctx
);
730 GLboolean allResident
= GL_TRUE
;
732 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
735 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
739 if (!texName
|| !residences
)
742 for (i
= 0; i
< n
; i
++) {
743 struct gl_texture_object
*t
;
744 if (texName
[i
] == 0) {
745 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
748 t
= (struct gl_texture_object
*)
749 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
751 if (ctx
->Driver
.IsTextureResident
) {
752 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
754 allResident
= GL_FALSE
;
757 residences
[i
] = GL_TRUE
;
761 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
771 * Execute glIsTexture
774 _mesa_IsTexture( GLuint texture
)
776 GET_CURRENT_CONTEXT(ctx
);
777 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
778 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);