1 /* $Id: texobj.c,v 1.45 2001/03/18 08:53:49 gareth Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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
;
84 obj
->CompareOperator
= GL_TEXTURE_LEQUAL_R_SGIX
;
85 obj
->ShadowAmbient
= 0;
86 _mesa_init_colortable(&obj
->Palette
);
88 /* insert into linked list */
90 _glthread_LOCK_MUTEX(shared
->Mutex
);
91 obj
->Next
= shared
->TexObjectList
;
92 shared
->TexObjectList
= obj
;
93 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
97 /* insert into hash table */
98 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
106 * Deallocate a texture object struct and remove it from the given
108 * Input: shared - the shared GL state to which the object belongs
109 * t - the texture object to delete
111 void _mesa_free_texture_object( struct gl_shared_state
*shared
,
112 struct gl_texture_object
*t
)
114 struct gl_texture_object
*tprev
, *tcurr
;
118 /* unlink t from the linked list */
120 _glthread_LOCK_MUTEX(shared
->Mutex
);
122 tcurr
= shared
->TexObjectList
;
126 tprev
->Next
= t
->Next
;
129 shared
->TexObjectList
= t
->Next
;
136 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
140 /* remove from hash table */
141 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
144 _mesa_free_colortable_data(&t
->Palette
);
146 /* free the texture images */
149 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
151 _mesa_free_texture_image( t
->Image
[i
] );
156 /* free this object */
162 * Report why a texture object is incomplete. (for debug only)
166 incomplete(const struct gl_texture_object
*t
, const char *why
)
168 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
171 #define incomplete(a, b)
176 * Examine a texture object to determine if it is complete.
177 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
180 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
181 struct gl_texture_object
*t
)
183 const GLint baseLevel
= t
->BaseLevel
;
186 t
->Complete
= GL_TRUE
; /* be optimistic */
188 /* Always need the base level image */
189 if (!t
->Image
[baseLevel
]) {
190 incomplete(t
, "Image[baseLevel] == NULL");
191 t
->Complete
= GL_FALSE
;
195 /* Compute _MaxLevel */
196 if (t
->Dimensions
== 1) {
197 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
199 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
200 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
201 t
->Image
[baseLevel
]->HeightLog2
);
203 else if (t
->Dimensions
== 3) {
204 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
205 t
->Image
[baseLevel
]->HeightLog2
);
206 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
209 t
->_MaxLevel
= baseLevel
+ maxLog2
;
210 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
211 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, ctx
->Const
.MaxTextureLevels
- 1);
213 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
214 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
216 if (t
->Dimensions
== 6) {
217 /* make sure that all six cube map level 0 images are the same size */
218 const GLuint w
= t
->Image
[baseLevel
]->Width2
;
219 const GLuint h
= t
->Image
[baseLevel
]->Height2
;
220 if (!t
->NegX
[baseLevel
] ||
221 t
->NegX
[baseLevel
]->Width2
!= w
||
222 t
->NegX
[baseLevel
]->Height2
!= h
||
223 !t
->PosY
[baseLevel
] ||
224 t
->PosY
[baseLevel
]->Width2
!= w
||
225 t
->PosY
[baseLevel
]->Height2
!= h
||
226 !t
->NegY
[baseLevel
] ||
227 t
->NegY
[baseLevel
]->Width2
!= w
||
228 t
->NegY
[baseLevel
]->Height2
!= h
||
229 !t
->PosZ
[baseLevel
] ||
230 t
->PosZ
[baseLevel
]->Width2
!= w
||
231 t
->PosZ
[baseLevel
]->Height2
!= h
||
232 !t
->NegZ
[baseLevel
] ||
233 t
->NegZ
[baseLevel
]->Width2
!= w
||
234 t
->NegZ
[baseLevel
]->Height2
!= h
) {
235 t
->Complete
= GL_FALSE
;
236 incomplete(t
, "Non-quare cubemap image");
241 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
243 * Mipmapping: determine if we have a complete set of mipmaps
246 GLint minLevel
= baseLevel
;
247 GLint maxLevel
= t
->_MaxLevel
;
249 if (minLevel
> maxLevel
) {
250 t
->Complete
= GL_FALSE
;
251 incomplete(t
, "minLevel > maxLevel");
255 /* Test dimension-independent attributes */
256 for (i
= minLevel
; i
<= maxLevel
; i
++) {
258 if (t
->Image
[i
]->Format
!= t
->Image
[baseLevel
]->Format
) {
259 t
->Complete
= GL_FALSE
;
260 incomplete(t
, "Format[i] != Format[baseLevel]");
263 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
264 t
->Complete
= GL_FALSE
;
265 incomplete(t
, "Border[i] != Border[baseLevel]");
271 /* Test things which depend on number of texture image dimensions */
272 if (t
->Dimensions
== 1) {
273 /* Test 1-D mipmaps */
274 GLuint width
= t
->Image
[baseLevel
]->Width2
;
275 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
279 if (i
>= minLevel
&& i
<= maxLevel
) {
281 t
->Complete
= GL_FALSE
;
282 incomplete(t
, "1D Image[i] == NULL");
285 if (t
->Image
[i
]->Width2
!= width
) {
286 t
->Complete
= GL_FALSE
;
287 incomplete(t
, "1D Image[i] bad width");
292 return; /* found smallest needed mipmap, all done! */
296 else if (t
->Dimensions
== 2) {
297 /* Test 2-D mipmaps */
298 GLuint width
= t
->Image
[baseLevel
]->Width2
;
299 GLuint height
= t
->Image
[baseLevel
]->Height2
;
300 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
307 if (i
>= minLevel
&& i
<= maxLevel
) {
309 t
->Complete
= GL_FALSE
;
310 incomplete(t
, "2D Image[i] == NULL");
313 if (t
->Image
[i
]->Width2
!= width
) {
314 t
->Complete
= GL_FALSE
;
315 incomplete(t
, "2D Image[i] bad width");
318 if (t
->Image
[i
]->Height2
!= height
) {
319 t
->Complete
= GL_FALSE
;
320 incomplete(t
, "2D Image[i] bad height");
323 if (width
==1 && height
==1) {
324 return; /* found smallest needed mipmap, all done! */
329 else if (t
->Dimensions
== 3) {
330 /* Test 3-D mipmaps */
331 GLuint width
= t
->Image
[baseLevel
]->Width2
;
332 GLuint height
= t
->Image
[baseLevel
]->Height2
;
333 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
334 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
344 if (i
>= minLevel
&& i
<= maxLevel
) {
346 incomplete(t
, "3D Image[i] == NULL");
347 t
->Complete
= GL_FALSE
;
350 if (t
->Image
[i
]->Width2
!= width
) {
351 t
->Complete
= GL_FALSE
;
352 incomplete(t
, "3D Image[i] bad width");
355 if (t
->Image
[i
]->Height2
!= height
) {
356 t
->Complete
= GL_FALSE
;
357 incomplete(t
, "3D Image[i] bad height");
360 if (t
->Image
[i
]->Depth2
!= depth
) {
361 t
->Complete
= GL_FALSE
;
362 incomplete(t
, "3D Image[i] bad depth");
366 if (width
== 1 && height
== 1 && depth
== 1) {
367 return; /* found smallest needed mipmap, all done! */
371 else if (t
->Dimensions
== 6) {
372 /* make sure 6 cube faces are consistant */
373 GLuint width
= t
->Image
[baseLevel
]->Width2
;
374 GLuint height
= t
->Image
[baseLevel
]->Height2
;
375 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
382 if (i
>= minLevel
&& i
<= maxLevel
) {
383 /* check that we have images defined */
384 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
385 !t
->PosY
[i
] || !t
->NegY
[i
] ||
386 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
387 t
->Complete
= GL_FALSE
;
388 incomplete(t
, "CubeMap Image[i] == NULL");
391 /* check that all six images have same size */
392 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
393 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
394 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
395 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
396 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
397 t
->Complete
= GL_FALSE
;
398 incomplete(t
, "CubeMap Image[i] bad size");
402 if (width
== 1 && height
== 1) {
403 return; /* found smallest needed mipmap, all done! */
408 /* Dimensions = ??? */
409 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
415 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
419 * Execute glGenTextures
422 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
424 GET_CURRENT_CONTEXT(ctx
);
427 ASSERT_OUTSIDE_BEGIN_END(ctx
);
430 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
438 * This must be atomic (generation and allocation of texture IDs)
440 _glthread_LOCK_MUTEX(GenTexturesLock
);
442 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
444 /* Return the texture names */
446 texName
[i
] = first
+ i
;
449 /* Allocate new, empty texture objects */
451 GLuint name
= first
+ i
;
453 (void) _mesa_alloc_texture_object( ctx
->Shared
, name
, dims
);
456 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
462 * Execute glDeleteTextures
465 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
467 GET_CURRENT_CONTEXT(ctx
);
469 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
475 if (texName
[i
] > 0) {
476 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
477 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
479 /* First check if this texture is currently bound.
480 * If so, unbind it and decrement the reference count.
483 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
484 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
485 if (delObj
== unit
->Current1D
) {
486 unit
->Current1D
= ctx
->Shared
->Default1D
;
487 ctx
->Shared
->Default1D
->RefCount
++;
489 else if (delObj
== unit
->Current2D
) {
490 unit
->Current2D
= ctx
->Shared
->Default2D
;
491 ctx
->Shared
->Default2D
->RefCount
++;
493 else if (delObj
== unit
->Current3D
) {
494 unit
->Current3D
= ctx
->Shared
->Default3D
;
495 ctx
->Shared
->Default3D
->RefCount
++;
497 else if (delObj
== unit
->CurrentCubeMap
) {
498 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
499 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
502 ctx
->NewState
|= _NEW_TEXTURE
;
504 /* Decrement reference count and delete if zero */
506 ASSERT( delObj
->RefCount
>= 0 );
507 if (delObj
->RefCount
== 0) {
508 if (ctx
->Driver
.DeleteTexture
)
509 (*ctx
->Driver
.DeleteTexture
)( ctx
, delObj
);
510 _mesa_free_texture_object(ctx
->Shared
, delObj
);
520 * Execute glBindTexture
523 _mesa_BindTexture( GLenum target
, GLuint texName
)
525 GET_CURRENT_CONTEXT(ctx
);
526 GLuint unit
= ctx
->Texture
.CurrentUnit
;
527 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
528 struct gl_texture_object
*oldTexObj
;
529 struct gl_texture_object
*newTexObj
= 0;
531 ASSERT_OUTSIDE_BEGIN_END(ctx
);
533 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
534 fprintf(stderr
, "glBindTexture %s %d\n",
535 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
540 oldTexObj
= texUnit
->Current1D
;
544 oldTexObj
= texUnit
->Current2D
;
548 oldTexObj
= texUnit
->Current3D
;
550 case GL_TEXTURE_CUBE_MAP_ARB
:
551 if (ctx
->Extensions
.ARB_texture_cube_map
) {
553 oldTexObj
= texUnit
->CurrentCubeMap
;
558 _mesa_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
562 if (oldTexObj
->Name
== texName
)
563 return; /* rebinding the same texture- no change */
566 * Get pointer to new texture object (newTexObj)
569 /* newTexObj = a default texture object */
572 newTexObj
= ctx
->Shared
->Default1D
;
575 newTexObj
= ctx
->Shared
->Default2D
;
578 newTexObj
= ctx
->Shared
->Default3D
;
580 case GL_TEXTURE_CUBE_MAP_ARB
:
581 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
584 ; /* Bad targets are caught above */
588 /* non-default texture object */
589 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
590 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
593 if (newTexObj
->Dimensions
> 0 && newTexObj
->Dimensions
!= targetDim
) {
594 /* the named texture object's dimensions don't match the target */
595 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
600 /* if this is a new texture id, allocate a texture object now */
601 newTexObj
= _mesa_alloc_texture_object( ctx
->Shared
, texName
,
604 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
608 newTexObj
->Dimensions
= targetDim
;
611 newTexObj
->RefCount
++;
614 /* do the actual binding, but first flush outstanding vertices:
616 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
620 texUnit
->Current1D
= newTexObj
;
623 texUnit
->Current2D
= newTexObj
;
626 texUnit
->Current3D
= newTexObj
;
628 case GL_TEXTURE_CUBE_MAP_ARB
:
629 texUnit
->CurrentCubeMap
= newTexObj
;
632 _mesa_problem(ctx
, "bad target in BindTexture");
635 /* Pass BindTexture call to device driver */
636 if (ctx
->Driver
.BindTexture
)
637 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
639 if (oldTexObj
->Name
> 0) {
640 /* never delete default (id=0) texture objects */
641 oldTexObj
->RefCount
--;
642 if (oldTexObj
->RefCount
<= 0) {
643 if (ctx
->Driver
.DeleteTexture
) {
644 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
646 _mesa_free_texture_object(ctx
->Shared
, oldTexObj
);
654 * Execute glPrioritizeTextures
657 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
658 const GLclampf
*priorities
)
660 GET_CURRENT_CONTEXT(ctx
);
662 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
665 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
672 for (i
= 0; i
< n
; i
++) {
673 if (texName
[i
] > 0) {
674 struct gl_texture_object
*t
= (struct gl_texture_object
*)
675 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
677 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
678 if (ctx
->Driver
.PrioritizeTexture
)
679 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
684 ctx
->NewState
|= _NEW_TEXTURE
;
690 * Execute glAreTexturesResident
693 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
694 GLboolean
*residences
)
696 GET_CURRENT_CONTEXT(ctx
);
697 GLboolean allResident
= GL_TRUE
;
699 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
702 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
706 if (!texName
|| !residences
)
709 for (i
= 0; i
< n
; i
++) {
710 struct gl_texture_object
*t
;
711 if (texName
[i
] == 0) {
712 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
715 t
= (struct gl_texture_object
*)
716 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
718 if (ctx
->Driver
.IsTextureResident
) {
719 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
721 allResident
= GL_FALSE
;
724 residences
[i
] = GL_TRUE
;
728 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
738 * Execute glIsTexture
741 _mesa_IsTexture( GLuint texture
)
743 GET_CURRENT_CONTEXT(ctx
);
744 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
745 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);