1 /* $Id: texobj.c,v 1.41 2001/02/26 18:25:25 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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
->CompareFlag
= GL_FALSE
;
83 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
;
84 obj
->ShadowAmbient
= 0;
85 _mesa_init_colortable(&obj
->Palette
);
87 /* insert into linked list */
89 _glthread_LOCK_MUTEX(shared
->Mutex
);
90 obj
->Next
= shared
->TexObjectList
;
91 shared
->TexObjectList
= obj
;
92 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
96 /* insert into hash table */
97 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
105 * Deallocate a texture object struct and remove it from the given
107 * Input: shared - the shared GL state to which the object belongs
108 * t - the texture object to delete
110 void _mesa_free_texture_object( struct gl_shared_state
*shared
,
111 struct gl_texture_object
*t
)
113 struct gl_texture_object
*tprev
, *tcurr
;
117 /* unlink t from the linked list */
119 _glthread_LOCK_MUTEX(shared
->Mutex
);
121 tcurr
= shared
->TexObjectList
;
125 tprev
->Next
= t
->Next
;
128 shared
->TexObjectList
= t
->Next
;
135 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
139 /* remove from hash table */
140 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
143 _mesa_free_colortable_data(&t
->Palette
);
145 /* free the texture images */
148 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
150 _mesa_free_texture_image( t
->Image
[i
] );
155 /* free this object */
161 * Report why a texture object is incomplete. (for debug only)
165 incomplete(const struct gl_texture_object
*t
, const char *why
)
167 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
170 #define incomplete(a, b)
175 * Examine a texture object to determine if it is complete.
176 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
179 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
180 struct gl_texture_object
*t
)
182 const GLint baseLevel
= t
->BaseLevel
;
185 t
->Complete
= GL_TRUE
; /* be optimistic */
187 /* Always need the base level image */
188 if (!t
->Image
[baseLevel
]) {
189 incomplete(t
, "Image[baseLevel] == NULL");
190 t
->Complete
= GL_FALSE
;
194 /* Compute _MaxLevel */
195 if (t
->Dimensions
== 1) {
196 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
198 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
199 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
200 t
->Image
[baseLevel
]->HeightLog2
);
202 else if (t
->Dimensions
== 3) {
203 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
204 t
->Image
[baseLevel
]->HeightLog2
);
205 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
208 t
->_MaxLevel
= baseLevel
+ maxLog2
;
209 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
210 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, ctx
->Const
.MaxTextureLevels
- 1);
212 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
213 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
215 if (t
->Dimensions
== 6) {
216 /* make sure that all six cube map level 0 images are the same size */
217 const GLint w
= t
->Image
[baseLevel
]->Width2
;
218 const GLint h
= t
->Image
[baseLevel
]->Height2
;
219 if (!t
->NegX
[baseLevel
] ||
220 t
->NegX
[baseLevel
]->Width2
!= w
||
221 t
->NegX
[baseLevel
]->Height2
!= h
||
222 !t
->PosY
[baseLevel
] ||
223 t
->PosY
[baseLevel
]->Width2
!= w
||
224 t
->PosY
[baseLevel
]->Height2
!= h
||
225 !t
->NegY
[baseLevel
] ||
226 t
->NegY
[baseLevel
]->Width2
!= w
||
227 t
->NegY
[baseLevel
]->Height2
!= h
||
228 !t
->PosZ
[baseLevel
] ||
229 t
->PosZ
[baseLevel
]->Width2
!= w
||
230 t
->PosZ
[baseLevel
]->Height2
!= h
||
231 !t
->NegZ
[baseLevel
] ||
232 t
->NegZ
[baseLevel
]->Width2
!= w
||
233 t
->NegZ
[baseLevel
]->Height2
!= h
) {
234 t
->Complete
= GL_FALSE
;
235 incomplete(t
, "Non-quare cubemap image");
240 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
242 * Mipmapping: determine if we have a complete set of mipmaps
245 GLint minLevel
= baseLevel
;
246 GLint maxLevel
= t
->_MaxLevel
;
248 if (minLevel
> maxLevel
) {
249 t
->Complete
= GL_FALSE
;
250 incomplete(t
, "minLevel > maxLevel");
254 /* Test dimension-independent attributes */
255 for (i
= minLevel
; i
<= maxLevel
; i
++) {
257 if (t
->Image
[i
]->Format
!= t
->Image
[baseLevel
]->Format
) {
258 t
->Complete
= GL_FALSE
;
259 incomplete(t
, "Format[i] != Format[baseLevel]");
262 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
263 t
->Complete
= GL_FALSE
;
264 incomplete(t
, "Border[i] != Border[baseLevel]");
270 /* Test things which depend on number of texture image dimensions */
271 if (t
->Dimensions
== 1) {
272 /* Test 1-D mipmaps */
273 GLuint width
= t
->Image
[baseLevel
]->Width2
;
274 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
278 if (i
>= minLevel
&& i
<= maxLevel
) {
280 t
->Complete
= GL_FALSE
;
281 incomplete(t
, "1D Image[i] == NULL");
284 if (t
->Image
[i
]->Width2
!= width
) {
285 t
->Complete
= GL_FALSE
;
286 incomplete(t
, "1D Image[i] bad width");
291 return; /* found smallest needed mipmap, all done! */
295 else if (t
->Dimensions
== 2) {
296 /* Test 2-D mipmaps */
297 GLuint width
= t
->Image
[baseLevel
]->Width2
;
298 GLuint height
= t
->Image
[baseLevel
]->Height2
;
299 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
306 if (i
>= minLevel
&& i
<= maxLevel
) {
308 t
->Complete
= GL_FALSE
;
309 incomplete(t
, "2D Image[i] == NULL");
312 if (t
->Image
[i
]->Width2
!= width
) {
313 t
->Complete
= GL_FALSE
;
314 incomplete(t
, "2D Image[i] bad width");
317 if (t
->Image
[i
]->Height2
!= height
) {
318 t
->Complete
= GL_FALSE
;
319 incomplete(t
, "2D Image[i] bad height");
322 if (width
==1 && height
==1) {
323 return; /* found smallest needed mipmap, all done! */
328 else if (t
->Dimensions
== 3) {
329 /* Test 3-D mipmaps */
330 GLuint width
= t
->Image
[baseLevel
]->Width2
;
331 GLuint height
= t
->Image
[baseLevel
]->Height2
;
332 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
333 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
343 if (i
>= minLevel
&& i
<= maxLevel
) {
345 incomplete(t
, "3D Image[i] == NULL");
346 t
->Complete
= GL_FALSE
;
349 if (t
->Image
[i
]->Width2
!= width
) {
350 t
->Complete
= GL_FALSE
;
351 incomplete(t
, "3D Image[i] bad width");
354 if (t
->Image
[i
]->Height2
!= height
) {
355 t
->Complete
= GL_FALSE
;
356 incomplete(t
, "3D Image[i] bad height");
359 if (t
->Image
[i
]->Depth2
!= depth
) {
360 t
->Complete
= GL_FALSE
;
361 incomplete(t
, "3D Image[i] bad depth");
365 if (width
== 1 && height
== 1 && depth
== 1) {
366 return; /* found smallest needed mipmap, all done! */
370 else if (t
->Dimensions
== 6) {
371 /* make sure 6 cube faces are consistant */
372 GLuint width
= t
->Image
[baseLevel
]->Width2
;
373 GLuint height
= t
->Image
[baseLevel
]->Height2
;
374 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
381 if (i
>= minLevel
&& i
<= maxLevel
) {
382 /* check that we have images defined */
383 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
384 !t
->PosY
[i
] || !t
->NegY
[i
] ||
385 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
386 t
->Complete
= GL_FALSE
;
387 incomplete(t
, "CubeMap Image[i] == NULL");
390 /* check that all six images have same size */
391 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
392 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
393 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
394 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
395 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
396 t
->Complete
= GL_FALSE
;
397 incomplete(t
, "CubeMap Image[i] bad size");
401 if (width
== 1 && height
== 1) {
402 return; /* found smallest needed mipmap, all done! */
407 /* Dimensions = ??? */
408 gl_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
414 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
418 * Execute glGenTextures
421 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
423 GET_CURRENT_CONTEXT(ctx
);
426 ASSERT_OUTSIDE_BEGIN_END(ctx
);
429 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
437 * This must be atomic (generation and allocation of texture IDs)
439 _glthread_LOCK_MUTEX(GenTexturesLock
);
441 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
443 /* Return the texture names */
445 texName
[i
] = first
+ i
;
448 /* Allocate new, empty texture objects */
450 GLuint name
= first
+ i
;
452 (void) _mesa_alloc_texture_object( ctx
->Shared
, name
, dims
);
455 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
461 * Execute glDeleteTextures
464 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
466 GET_CURRENT_CONTEXT(ctx
);
468 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
474 if (texName
[i
] > 0) {
475 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
476 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
478 /* First check if this texture is currently bound.
479 * If so, unbind it and decrement the reference count.
482 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
483 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
484 if (delObj
== unit
->Current1D
) {
485 unit
->Current1D
= ctx
->Shared
->Default1D
;
486 ctx
->Shared
->Default1D
->RefCount
++;
488 else if (delObj
== unit
->Current2D
) {
489 unit
->Current2D
= ctx
->Shared
->Default2D
;
490 ctx
->Shared
->Default2D
->RefCount
++;
492 else if (delObj
== unit
->Current3D
) {
493 unit
->Current3D
= ctx
->Shared
->Default3D
;
494 ctx
->Shared
->Default3D
->RefCount
++;
496 else if (delObj
== unit
->CurrentCubeMap
) {
497 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
498 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
501 ctx
->NewState
|= _NEW_TEXTURE
;
503 /* Decrement reference count and delete if zero */
505 ASSERT( delObj
->RefCount
>= 0 );
506 if (delObj
->RefCount
== 0) {
507 if (ctx
->Driver
.DeleteTexture
)
508 (*ctx
->Driver
.DeleteTexture
)( ctx
, delObj
);
509 _mesa_free_texture_object(ctx
->Shared
, delObj
);
519 * Execute glBindTexture
522 _mesa_BindTexture( GLenum target
, GLuint texName
)
524 GET_CURRENT_CONTEXT(ctx
);
525 GLuint unit
= ctx
->Texture
.CurrentUnit
;
526 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
527 struct gl_texture_object
*oldTexObj
;
528 struct gl_texture_object
*newTexObj
= 0;
530 ASSERT_OUTSIDE_BEGIN_END(ctx
);
532 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
533 fprintf(stderr
, "glBindTexture %s %d\n",
534 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
539 oldTexObj
= texUnit
->Current1D
;
543 oldTexObj
= texUnit
->Current2D
;
547 oldTexObj
= texUnit
->Current3D
;
549 case GL_TEXTURE_CUBE_MAP_ARB
:
550 if (ctx
->Extensions
.ARB_texture_cube_map
) {
552 oldTexObj
= texUnit
->CurrentCubeMap
;
557 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
561 if (oldTexObj
->Name
== texName
)
562 return; /* rebinding the same texture- no change */
565 * Get pointer to new texture object (newTexObj)
568 /* newTexObj = a default texture object */
571 newTexObj
= ctx
->Shared
->Default1D
;
574 newTexObj
= ctx
->Shared
->Default2D
;
577 newTexObj
= ctx
->Shared
->Default3D
;
579 case GL_TEXTURE_CUBE_MAP_ARB
:
580 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
583 ; /* Bad targets are caught above */
587 /* non-default texture object */
588 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
589 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
592 if (newTexObj
->Dimensions
> 0 && newTexObj
->Dimensions
!= targetDim
) {
593 /* the named texture object's dimensions don't match the target */
594 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
599 /* if this is a new texture id, allocate a texture object now */
600 newTexObj
= _mesa_alloc_texture_object( ctx
->Shared
, texName
,
603 gl_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
607 newTexObj
->Dimensions
= targetDim
;
610 newTexObj
->RefCount
++;
613 /* do the actual binding, but first flush outstanding vertices:
615 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
619 texUnit
->Current1D
= newTexObj
;
622 texUnit
->Current2D
= newTexObj
;
625 texUnit
->Current3D
= newTexObj
;
627 case GL_TEXTURE_CUBE_MAP_ARB
:
628 texUnit
->CurrentCubeMap
= newTexObj
;
631 gl_problem(ctx
, "bad target in BindTexture");
634 /* Pass BindTexture call to device driver */
635 if (ctx
->Driver
.BindTexture
)
636 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
638 if (oldTexObj
->Name
> 0) {
639 /* never delete default (id=0) texture objects */
640 oldTexObj
->RefCount
--;
641 if (oldTexObj
->RefCount
<= 0) {
642 if (ctx
->Driver
.DeleteTexture
) {
643 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
645 _mesa_free_texture_object(ctx
->Shared
, oldTexObj
);
653 * Execute glPrioritizeTextures
656 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
657 const GLclampf
*priorities
)
659 GET_CURRENT_CONTEXT(ctx
);
661 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
664 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
671 for (i
= 0; i
< n
; i
++) {
672 if (texName
[i
] > 0) {
673 struct gl_texture_object
*t
= (struct gl_texture_object
*)
674 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
676 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
677 if (ctx
->Driver
.PrioritizeTexture
)
678 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
683 ctx
->NewState
|= _NEW_TEXTURE
;
689 * Execute glAreTexturesResident
692 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
693 GLboolean
*residences
)
695 GET_CURRENT_CONTEXT(ctx
);
696 GLboolean allResident
= GL_TRUE
;
698 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
701 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
705 if (!texName
|| !residences
)
708 for (i
= 0; i
< n
; i
++) {
709 struct gl_texture_object
*t
;
710 if (texName
[i
] == 0) {
711 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
714 t
= (struct gl_texture_object
*)
715 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
717 if (ctx
->Driver
.IsTextureResident
) {
718 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
720 allResident
= GL_FALSE
;
723 residences
[i
] = GL_TRUE
;
727 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
737 * Execute glIsTexture
740 _mesa_IsTexture( GLuint texture
)
742 GET_CURRENT_CONTEXT(ctx
);
743 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
744 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);