21f37e49bc7751fa528259e84ec72eefec6c63c4
1 /* $Id: texobj.c,v 1.32 2000/11/05 18:40:58 keithw 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 * Return: pointer to new texture object
55 struct gl_texture_object
*
56 gl_alloc_texture_object( struct gl_shared_state
*shared
, GLuint name
,
59 struct gl_texture_object
*obj
;
61 ASSERT(dimensions
<= 3 || dimensions
== 6);
63 obj
= CALLOC_STRUCT(gl_texture_object
);
66 /* init the non-zero fields */
67 _glthread_INIT_MUTEX(obj
->Mutex
);
70 obj
->Dimensions
= dimensions
;
72 obj
->WrapS
= GL_REPEAT
;
73 obj
->WrapT
= GL_REPEAT
;
74 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
75 obj
->MagFilter
= GL_LINEAR
;
76 obj
->MinLod
= -1000.0;
80 _mesa_init_colortable(&obj
->Palette
);
82 /* insert into linked list */
84 _glthread_LOCK_MUTEX(shared
->Mutex
);
85 obj
->Next
= shared
->TexObjectList
;
86 shared
->TexObjectList
= obj
;
87 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
91 /* insert into hash table */
92 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
100 * Deallocate a texture object struct and remove it from the given
102 * Input: shared - the shared GL state to which the object belongs
103 * t - the texture object to delete
105 void gl_free_texture_object( struct gl_shared_state
*shared
,
106 struct gl_texture_object
*t
)
108 struct gl_texture_object
*tprev
, *tcurr
;
112 /* Remove t from dirty list so we don't touch free'd memory later.
113 * Test for shared since Proxy texture aren't in global linked list.
116 gl_remove_texobj_from_dirty_list( shared
, t
);
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 texture images */
149 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
151 _mesa_free_texture_image( t
->Image
[i
] );
155 /* free this object */
161 incomplete(const struct gl_texture_object
*t
, const char *why
)
163 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
166 #define incomplete(a, b)
171 * Examine a texture object to determine if it is complete or not.
172 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
175 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
176 struct gl_texture_object
*t
)
178 const GLint baseLevel
= t
->BaseLevel
;
180 t
->Complete
= GL_TRUE
; /* be optimistic */
182 /* Always need level zero image */
183 if (!t
->Image
[baseLevel
]) {
184 incomplete(t
, "Image[baseLevel] == NULL");
185 t
->Complete
= GL_FALSE
;
189 /* Compute number of mipmap levels */
190 if (t
->Dimensions
== 1) {
191 t
->_P
= t
->Image
[baseLevel
]->WidthLog2
;
193 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
194 t
->_P
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
195 t
->Image
[baseLevel
]->HeightLog2
);
197 else if (t
->Dimensions
== 3) {
198 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
199 t
->Image
[baseLevel
]->HeightLog2
);
200 max
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
204 /* Compute M (see the 1.2 spec) used during mipmapping */
205 t
->_M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->_P
) - t
->BaseLevel
);
208 if (t
->Dimensions
== 6) {
209 /* make sure all six level 0 images are same size */
210 const GLint w
= t
->Image
[baseLevel
]->Width2
;
211 const GLint h
= t
->Image
[baseLevel
]->Height2
;
212 if (!t
->NegX
[baseLevel
] ||
213 t
->NegX
[baseLevel
]->Width2
!= w
||
214 t
->NegX
[baseLevel
]->Height2
!= h
||
215 !t
->PosY
[baseLevel
] ||
216 t
->PosY
[baseLevel
]->Width2
!= w
||
217 t
->PosY
[baseLevel
]->Height2
!= h
||
218 !t
->NegY
[baseLevel
] ||
219 t
->NegY
[baseLevel
]->Width2
!= w
||
220 t
->NegY
[baseLevel
]->Height2
!= h
||
221 !t
->PosZ
[baseLevel
] ||
222 t
->PosZ
[baseLevel
]->Width2
!= w
||
223 t
->PosZ
[baseLevel
]->Height2
!= h
||
224 !t
->NegZ
[baseLevel
] ||
225 t
->NegZ
[baseLevel
]->Width2
!= w
||
226 t
->NegZ
[baseLevel
]->Height2
!= h
) {
227 t
->Complete
= GL_FALSE
;
228 incomplete(t
, "Non-quare cubemap image");
233 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
235 * Mipmapping: determine if we have a complete set of mipmaps
238 GLint minLevel
= baseLevel
;
239 GLint maxLevel
= MIN2(t
->_P
, ctx
->Const
.MaxTextureLevels
-1);
240 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
242 if (minLevel
> maxLevel
) {
243 t
->Complete
= GL_FALSE
;
244 incomplete(t
, "minLevel > maxLevel");
248 /* Test dimension-independent attributes */
249 for (i
= minLevel
; i
<= maxLevel
; i
++) {
251 if (t
->Image
[i
]->Format
!= t
->Image
[baseLevel
]->Format
) {
252 t
->Complete
= GL_FALSE
;
253 incomplete(t
, "Format[i] != Format[baseLevel]");
256 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
257 t
->Complete
= GL_FALSE
;
258 incomplete(t
, "Border[i] != Border[baseLevel]");
264 /* Test things which depend on number of texture image dimensions */
265 if (t
->Dimensions
== 1) {
266 /* Test 1-D mipmaps */
267 GLuint width
= t
->Image
[baseLevel
]->Width2
;
268 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
272 if (i
>= minLevel
&& i
<= maxLevel
) {
274 t
->Complete
= GL_FALSE
;
275 incomplete(t
, "1D Image[i] == NULL");
278 if (t
->Image
[i
]->Width2
!= width
) {
279 t
->Complete
= GL_FALSE
;
280 incomplete(t
, "1D Image[i] bad width");
285 return; /* found smallest needed mipmap, all done! */
289 else if (t
->Dimensions
== 2) {
290 /* Test 2-D mipmaps */
291 GLuint width
= t
->Image
[baseLevel
]->Width2
;
292 GLuint height
= t
->Image
[baseLevel
]->Height2
;
293 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
300 if (i
>= minLevel
&& i
<= maxLevel
) {
302 t
->Complete
= GL_FALSE
;
303 incomplete(t
, "2D Image[i] == NULL");
306 if (t
->Image
[i
]->Width2
!= width
) {
307 t
->Complete
= GL_FALSE
;
308 incomplete(t
, "2D Image[i] bad width");
311 if (t
->Image
[i
]->Height2
!= height
) {
312 t
->Complete
= GL_FALSE
;
313 incomplete(t
, "2D Image[i] bad height");
316 if (width
==1 && height
==1) {
317 return; /* found smallest needed mipmap, all done! */
322 else if (t
->Dimensions
== 3) {
323 /* Test 3-D mipmaps */
324 GLuint width
= t
->Image
[baseLevel
]->Width2
;
325 GLuint height
= t
->Image
[baseLevel
]->Height2
;
326 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
327 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
337 if (i
>= minLevel
&& i
<= maxLevel
) {
339 incomplete(t
, "3D Image[i] == NULL");
340 t
->Complete
= GL_FALSE
;
343 if (t
->Image
[i
]->Width2
!= width
) {
344 t
->Complete
= GL_FALSE
;
345 incomplete(t
, "3D Image[i] bad width");
348 if (t
->Image
[i
]->Height2
!= height
) {
349 t
->Complete
= GL_FALSE
;
350 incomplete(t
, "3D Image[i] bad height");
353 if (t
->Image
[i
]->Depth2
!= depth
) {
354 t
->Complete
= GL_FALSE
;
355 incomplete(t
, "3D Image[i] bad depth");
359 if (width
== 1 && height
== 1 && depth
== 1) {
360 return; /* found smallest needed mipmap, all done! */
364 else if (t
->Dimensions
== 6) {
365 /* make sure 6 cube faces are consistant */
366 GLuint width
= t
->Image
[baseLevel
]->Width2
;
367 GLuint height
= t
->Image
[baseLevel
]->Height2
;
368 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
375 if (i
>= minLevel
&& i
<= maxLevel
) {
376 /* check that we have images defined */
377 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
378 !t
->PosY
[i
] || !t
->NegY
[i
] ||
379 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
380 t
->Complete
= GL_FALSE
;
381 incomplete(t
, "CubeMap Image[i] == NULL");
384 /* check that all six images have same size */
385 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
386 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
387 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
388 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
389 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
390 t
->Complete
= GL_FALSE
;
391 incomplete(t
, "CubeMap Image[i] bad size");
395 if (width
== 1 && height
== 1) {
396 return; /* found smallest needed mipmap, all done! */
401 /* Dimensions = ??? */
402 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
408 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
412 * Execute glGenTextures
415 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
417 GET_CURRENT_CONTEXT(ctx
);
421 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
423 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
431 * This must be atomic (generation and allocation of texture IDs)
433 _glthread_LOCK_MUTEX(GenTexturesLock
);
435 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
437 /* Return the texture names */
439 texName
[i
] = first
+ i
;
442 /* Allocate new, empty texture objects */
444 GLuint name
= first
+ i
;
446 (void) gl_alloc_texture_object(ctx
->Shared
, name
, dims
);
449 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
455 * Execute glDeleteTextures
458 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
460 GET_CURRENT_CONTEXT(ctx
);
463 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
469 struct gl_texture_object
*t
;
471 t
= (struct gl_texture_object
*)
472 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
474 /* First check if this texture is currently bound.
475 * If so, unbind it and decrement the reference count.
478 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
479 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
481 for (d
= 1 ; d
<= 3 ; d
++) {
482 if (unit
->CurrentD
[d
] == t
) {
483 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
484 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
486 ASSERT( t
->RefCount
>= 0 );
487 ctx
->NewState
|= _NEW_TEXTURE
;
492 /* Decrement reference count and delete if zero */
494 ASSERT( t
->RefCount
>= 0 );
495 if (t
->RefCount
== 0) {
496 if (ctx
->Driver
.DeleteTexture
)
497 (*ctx
->Driver
.DeleteTexture
)( ctx
, t
);
498 gl_free_texture_object(ctx
->Shared
, t
);
508 * Execute glBindTexture
511 _mesa_BindTexture( GLenum target
, GLuint texName
)
513 GET_CURRENT_CONTEXT(ctx
);
514 GLuint unit
= ctx
->Texture
.CurrentUnit
;
515 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
516 struct gl_texture_object
*oldTexObj
;
517 struct gl_texture_object
*newTexObj
;
520 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
521 fprintf(stderr
, "glBindTexture %s %d\n",
522 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
524 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
529 oldTexObj
= texUnit
->CurrentD
[1];
533 oldTexObj
= texUnit
->CurrentD
[2];
537 oldTexObj
= texUnit
->CurrentD
[3];
539 case GL_TEXTURE_CUBE_MAP_ARB
:
540 if (ctx
->Extensions
.ARB_texture_cube_map
) {
542 oldTexObj
= texUnit
->CurrentCubeMap
;
547 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
551 if (oldTexObj
->Name
== texName
)
555 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
556 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
558 newTexObj
= ctx
->Shared
->DefaultD
[dim
];
561 struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
562 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
565 newTexObj
= gl_alloc_texture_object(ctx
->Shared
, texName
, dim
);
567 if (newTexObj
->Dimensions
!= dim
) {
568 if (newTexObj
->Dimensions
) {
569 /* the named texture object's dimensions don't match the target */
570 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
573 newTexObj
->Dimensions
= dim
;
577 newTexObj
->RefCount
++;
581 texUnit
->CurrentD
[1] = newTexObj
;
584 texUnit
->CurrentD
[2] = newTexObj
;
587 texUnit
->CurrentD
[3] = newTexObj
;
589 case GL_TEXTURE_CUBE_MAP_ARB
:
590 texUnit
->CurrentCubeMap
= newTexObj
;
593 gl_problem(ctx
, "bad target in BindTexture");
596 /* If we've changed the CurrentD[123] texture object then update the
597 * ctx->Texture.Current pointer to point to the new texture object.
599 texUnit
->_Current
= texUnit
->CurrentD
[texUnit
->_CurrentDimension
];
601 ctx
->NewState
|= _NEW_TEXTURE
;
603 /* Pass BindTexture call to device driver */
604 if (ctx
->Driver
.BindTexture
)
605 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
607 if (oldTexObj
->Name
> 0) {
608 /* never delete default (id=0) texture objects */
609 oldTexObj
->RefCount
--;
610 if (oldTexObj
->RefCount
<= 0) {
611 if (ctx
->Driver
.DeleteTexture
) {
612 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
614 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
622 * Execute glPrioritizeTextures
625 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
626 const GLclampf
*priorities
)
628 GET_CURRENT_CONTEXT(ctx
);
631 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
633 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
640 for (i
= 0; i
< n
; i
++) {
641 if (texName
[i
] > 0) {
642 struct gl_texture_object
*t
= (struct gl_texture_object
*)
643 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
645 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
646 if (ctx
->Driver
.PrioritizeTexture
)
647 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
652 ctx
->NewState
|= _NEW_TEXTURE
;
658 * Execute glAreTexturesResident
661 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
662 GLboolean
*residences
)
664 GET_CURRENT_CONTEXT(ctx
);
665 GLboolean allResident
= GL_TRUE
;
668 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
669 "glAreTexturesResident", GL_FALSE
);
671 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
675 if (!texName
|| !residences
)
678 for (i
= 0; i
< n
; i
++) {
679 struct gl_texture_object
*t
;
680 if (texName
[i
] == 0) {
681 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
684 t
= (struct gl_texture_object
*)
685 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
687 if (ctx
->Driver
.IsTextureResident
) {
688 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
690 allResident
= GL_FALSE
;
693 residences
[i
] = GL_TRUE
;
697 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
707 * Execute glIsTexture
710 _mesa_IsTexture( GLuint texture
)
712 GET_CURRENT_CONTEXT(ctx
);
713 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
715 if (texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {