f090fcd734c9c5651900888b9e6459f03a24c243
1 /* $Id: texobj.c,v 1.35 2000/11/22 07:32:17 joukj 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
, GLuint name
,
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 _mesa_init_colortable(&obj
->Palette
);
84 /* insert into linked list */
86 _glthread_LOCK_MUTEX(shared
->Mutex
);
87 obj
->Next
= shared
->TexObjectList
;
88 shared
->TexObjectList
= obj
;
89 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
93 /* insert into hash table */
94 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
102 * Deallocate a texture object struct and remove it from the given
104 * Input: shared - the shared GL state to which the object belongs
105 * t - the texture object to delete
107 void _mesa_free_texture_object( struct gl_shared_state
*shared
,
108 struct gl_texture_object
*t
)
110 struct gl_texture_object
*tprev
, *tcurr
;
114 /* unlink t from the linked list */
116 _glthread_LOCK_MUTEX(shared
->Mutex
);
118 tcurr
= shared
->TexObjectList
;
122 tprev
->Next
= t
->Next
;
125 shared
->TexObjectList
= t
->Next
;
132 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
136 /* remove from hash table */
137 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
140 _mesa_free_colortable_data(&t
->Palette
);
142 /* free the texture images */
145 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
147 _mesa_free_texture_image( t
->Image
[i
] );
152 /* free this object */
158 * Report why a texture object is incomplete. (for debug only)
162 incomplete(const struct gl_texture_object
*t
, const char *why
)
164 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
167 #define incomplete(a, b)
172 * Examine a texture object to determine if it is complete.
173 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
176 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
177 struct gl_texture_object
*t
)
179 const GLint baseLevel
= t
->BaseLevel
;
181 t
->Complete
= GL_TRUE
; /* be optimistic */
183 /* Always need the base level image */
184 if (!t
->Image
[baseLevel
]) {
185 incomplete(t
, "Image[baseLevel] == NULL");
186 t
->Complete
= GL_FALSE
;
190 /* Compute number of mipmap levels */
191 if (t
->Dimensions
== 1) {
192 t
->_P
= t
->Image
[baseLevel
]->WidthLog2
;
194 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
195 t
->_P
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
196 t
->Image
[baseLevel
]->HeightLog2
);
198 else if (t
->Dimensions
== 3) {
199 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
200 t
->Image
[baseLevel
]->HeightLog2
);
201 max
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
205 /* Compute M (see the 1.2 spec) used during mipmapping */
206 t
->_M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->_P
) - t
->BaseLevel
);
209 if (t
->Dimensions
== 6) {
210 /* make sure that all six cube map level 0 images are the same size */
211 const GLint w
= t
->Image
[baseLevel
]->Width2
;
212 const GLint h
= t
->Image
[baseLevel
]->Height2
;
213 if (!t
->NegX
[baseLevel
] ||
214 t
->NegX
[baseLevel
]->Width2
!= w
||
215 t
->NegX
[baseLevel
]->Height2
!= h
||
216 !t
->PosY
[baseLevel
] ||
217 t
->PosY
[baseLevel
]->Width2
!= w
||
218 t
->PosY
[baseLevel
]->Height2
!= h
||
219 !t
->NegY
[baseLevel
] ||
220 t
->NegY
[baseLevel
]->Width2
!= w
||
221 t
->NegY
[baseLevel
]->Height2
!= h
||
222 !t
->PosZ
[baseLevel
] ||
223 t
->PosZ
[baseLevel
]->Width2
!= w
||
224 t
->PosZ
[baseLevel
]->Height2
!= h
||
225 !t
->NegZ
[baseLevel
] ||
226 t
->NegZ
[baseLevel
]->Width2
!= w
||
227 t
->NegZ
[baseLevel
]->Height2
!= h
) {
228 t
->Complete
= GL_FALSE
;
229 incomplete(t
, "Non-quare cubemap image");
234 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
236 * Mipmapping: determine if we have a complete set of mipmaps
239 GLint minLevel
= baseLevel
;
240 GLint maxLevel
= MIN2(t
->_P
, ctx
->Const
.MaxTextureLevels
-1);
241 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
243 if (minLevel
> maxLevel
) {
244 t
->Complete
= GL_FALSE
;
245 incomplete(t
, "minLevel > maxLevel");
249 /* Test dimension-independent attributes */
250 for (i
= minLevel
; i
<= maxLevel
; i
++) {
252 if (t
->Image
[i
]->Format
!= t
->Image
[baseLevel
]->Format
) {
253 t
->Complete
= GL_FALSE
;
254 incomplete(t
, "Format[i] != Format[baseLevel]");
257 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
258 t
->Complete
= GL_FALSE
;
259 incomplete(t
, "Border[i] != Border[baseLevel]");
265 /* Test things which depend on number of texture image dimensions */
266 if (t
->Dimensions
== 1) {
267 /* Test 1-D mipmaps */
268 GLuint width
= t
->Image
[baseLevel
]->Width2
;
269 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
273 if (i
>= minLevel
&& i
<= maxLevel
) {
275 t
->Complete
= GL_FALSE
;
276 incomplete(t
, "1D Image[i] == NULL");
279 if (t
->Image
[i
]->Width2
!= width
) {
280 t
->Complete
= GL_FALSE
;
281 incomplete(t
, "1D Image[i] bad width");
286 return; /* found smallest needed mipmap, all done! */
290 else if (t
->Dimensions
== 2) {
291 /* Test 2-D mipmaps */
292 GLuint width
= t
->Image
[baseLevel
]->Width2
;
293 GLuint height
= t
->Image
[baseLevel
]->Height2
;
294 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
301 if (i
>= minLevel
&& i
<= maxLevel
) {
303 t
->Complete
= GL_FALSE
;
304 incomplete(t
, "2D Image[i] == NULL");
307 if (t
->Image
[i
]->Width2
!= width
) {
308 t
->Complete
= GL_FALSE
;
309 incomplete(t
, "2D Image[i] bad width");
312 if (t
->Image
[i
]->Height2
!= height
) {
313 t
->Complete
= GL_FALSE
;
314 incomplete(t
, "2D Image[i] bad height");
317 if (width
==1 && height
==1) {
318 return; /* found smallest needed mipmap, all done! */
323 else if (t
->Dimensions
== 3) {
324 /* Test 3-D mipmaps */
325 GLuint width
= t
->Image
[baseLevel
]->Width2
;
326 GLuint height
= t
->Image
[baseLevel
]->Height2
;
327 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
328 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
338 if (i
>= minLevel
&& i
<= maxLevel
) {
340 incomplete(t
, "3D Image[i] == NULL");
341 t
->Complete
= GL_FALSE
;
344 if (t
->Image
[i
]->Width2
!= width
) {
345 t
->Complete
= GL_FALSE
;
346 incomplete(t
, "3D Image[i] bad width");
349 if (t
->Image
[i
]->Height2
!= height
) {
350 t
->Complete
= GL_FALSE
;
351 incomplete(t
, "3D Image[i] bad height");
354 if (t
->Image
[i
]->Depth2
!= depth
) {
355 t
->Complete
= GL_FALSE
;
356 incomplete(t
, "3D Image[i] bad depth");
360 if (width
== 1 && height
== 1 && depth
== 1) {
361 return; /* found smallest needed mipmap, all done! */
365 else if (t
->Dimensions
== 6) {
366 /* make sure 6 cube faces are consistant */
367 GLuint width
= t
->Image
[baseLevel
]->Width2
;
368 GLuint height
= t
->Image
[baseLevel
]->Height2
;
369 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
376 if (i
>= minLevel
&& i
<= maxLevel
) {
377 /* check that we have images defined */
378 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
379 !t
->PosY
[i
] || !t
->NegY
[i
] ||
380 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
381 t
->Complete
= GL_FALSE
;
382 incomplete(t
, "CubeMap Image[i] == NULL");
385 /* check that all six images have same size */
386 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
387 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
388 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
389 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
390 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
391 t
->Complete
= GL_FALSE
;
392 incomplete(t
, "CubeMap Image[i] bad size");
396 if (width
== 1 && height
== 1) {
397 return; /* found smallest needed mipmap, all done! */
402 /* Dimensions = ??? */
403 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
409 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
413 * Execute glGenTextures
416 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
418 GET_CURRENT_CONTEXT(ctx
);
422 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
424 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
432 * This must be atomic (generation and allocation of texture IDs)
434 _glthread_LOCK_MUTEX(GenTexturesLock
);
436 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
438 /* Return the texture names */
440 texName
[i
] = first
+ i
;
443 /* Allocate new, empty texture objects */
445 GLuint name
= first
+ i
;
447 (void) _mesa_alloc_texture_object(ctx
->Shared
, name
, dims
);
450 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
456 * Execute glDeleteTextures
459 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
461 GET_CURRENT_CONTEXT(ctx
);
464 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
470 if (texName
[i
] > 0) {
471 struct gl_texture_object
*delObj
= (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
];
480 if (delObj
== unit
->Current1D
) {
481 unit
->Current1D
= ctx
->Shared
->Default1D
;
482 ctx
->Shared
->Default1D
->RefCount
++;
484 else if (delObj
== unit
->Current2D
) {
485 unit
->Current2D
= ctx
->Shared
->Default2D
;
486 ctx
->Shared
->Default2D
->RefCount
++;
488 else if (delObj
== unit
->Current3D
) {
489 unit
->Current3D
= ctx
->Shared
->Default3D
;
490 ctx
->Shared
->Default3D
->RefCount
++;
492 else if (delObj
== unit
->CurrentCubeMap
) {
493 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
494 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
497 ctx
->NewState
|= _NEW_TEXTURE
;
499 /* Decrement reference count and delete if zero */
501 ASSERT( delObj
->RefCount
>= 0 );
502 if (delObj
->RefCount
== 0) {
503 if (ctx
->Driver
.DeleteTexture
)
504 (*ctx
->Driver
.DeleteTexture
)( ctx
, delObj
);
505 _mesa_free_texture_object(ctx
->Shared
, delObj
);
515 * Execute glBindTexture
518 _mesa_BindTexture( GLenum target
, GLuint texName
)
520 GET_CURRENT_CONTEXT(ctx
);
521 GLuint unit
= ctx
->Texture
.CurrentUnit
;
522 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
523 struct gl_texture_object
*oldTexObj
;
524 struct gl_texture_object
*newTexObj
;
527 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
528 fprintf(stderr
, "glBindTexture %s %d\n",
529 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
531 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
536 oldTexObj
= texUnit
->Current1D
;
540 oldTexObj
= texUnit
->Current2D
;
544 oldTexObj
= texUnit
->Current3D
;
546 case GL_TEXTURE_CUBE_MAP_ARB
:
547 if (ctx
->Extensions
.ARB_texture_cube_map
) {
549 oldTexObj
= texUnit
->CurrentCubeMap
;
554 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
558 if (oldTexObj
->Name
== texName
)
559 return; /* rebinding the same texture- no change */
562 * Get pointer to new texture object (newTexObj)
565 /* newTexObj = a default texture object */
568 newTexObj
= ctx
->Shared
->Default1D
;
571 newTexObj
= ctx
->Shared
->Default2D
;
574 newTexObj
= ctx
->Shared
->Default3D
;
576 case GL_TEXTURE_CUBE_MAP_ARB
:
577 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
580 ; /* Bad targets are caught above */
584 /* non-default texture object */
585 const struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
586 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
589 if (newTexObj
->Dimensions
> 0 && newTexObj
->Dimensions
!= targetDim
) {
590 /* the named texture object's dimensions don't match the target */
591 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
596 /* if this is a new texture id, allocate a texture object now */
597 newTexObj
= _mesa_alloc_texture_object(ctx
->Shared
, texName
, targetDim
);
599 gl_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
603 newTexObj
->Dimensions
= targetDim
;
606 newTexObj
->RefCount
++;
608 /* do the actual binding */
611 texUnit
->Current1D
= newTexObj
;
614 texUnit
->Current2D
= newTexObj
;
617 texUnit
->Current3D
= newTexObj
;
619 case GL_TEXTURE_CUBE_MAP_ARB
:
620 texUnit
->CurrentCubeMap
= newTexObj
;
623 gl_problem(ctx
, "bad target in BindTexture");
626 ctx
->NewState
|= _NEW_TEXTURE
;
628 /* Pass BindTexture call to device driver */
629 if (ctx
->Driver
.BindTexture
)
630 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
632 if (oldTexObj
->Name
> 0) {
633 /* never delete default (id=0) texture objects */
634 oldTexObj
->RefCount
--;
635 if (oldTexObj
->RefCount
<= 0) {
636 if (ctx
->Driver
.DeleteTexture
) {
637 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
639 _mesa_free_texture_object(ctx
->Shared
, oldTexObj
);
647 * Execute glPrioritizeTextures
650 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
651 const GLclampf
*priorities
)
653 GET_CURRENT_CONTEXT(ctx
);
656 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
658 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
665 for (i
= 0; i
< n
; i
++) {
666 if (texName
[i
] > 0) {
667 struct gl_texture_object
*t
= (struct gl_texture_object
*)
668 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
670 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
671 if (ctx
->Driver
.PrioritizeTexture
)
672 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
677 ctx
->NewState
|= _NEW_TEXTURE
;
683 * Execute glAreTexturesResident
686 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
687 GLboolean
*residences
)
689 GET_CURRENT_CONTEXT(ctx
);
690 GLboolean allResident
= GL_TRUE
;
693 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
694 "glAreTexturesResident", GL_FALSE
);
696 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
700 if (!texName
|| !residences
)
703 for (i
= 0; i
< n
; i
++) {
704 struct gl_texture_object
*t
;
705 if (texName
[i
] == 0) {
706 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
709 t
= (struct gl_texture_object
*)
710 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
712 if (ctx
->Driver
.IsTextureResident
) {
713 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
715 allResident
= GL_FALSE
;
718 residences
[i
] = GL_TRUE
;
722 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
732 * Execute glIsTexture
735 _mesa_IsTexture( GLuint texture
)
737 GET_CURRENT_CONTEXT(ctx
);
738 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
740 if (texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {