1204668dc3514064841a62a7bdd85dbeee5e8424
1 /* $Id: texobj.c,v 1.37 2000/12/26 05:09:29 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 * 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
;
182 t
->Complete
= GL_TRUE
; /* be optimistic */
184 /* Always need the base level image */
185 if (!t
->Image
[baseLevel
]) {
186 incomplete(t
, "Image[baseLevel] == NULL");
187 t
->Complete
= GL_FALSE
;
191 /* Compute _MaxLevel */
192 if (t
->Dimensions
== 1) {
193 maxLog2
= t
->Image
[baseLevel
]->WidthLog2
;
195 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
196 maxLog2
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
197 t
->Image
[baseLevel
]->HeightLog2
);
199 else if (t
->Dimensions
== 3) {
200 GLint max
= MAX2(t
->Image
[baseLevel
]->WidthLog2
,
201 t
->Image
[baseLevel
]->HeightLog2
);
202 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[baseLevel
]->DepthLog2
));
205 t
->_MaxLevel
= baseLevel
+ maxLog2
;
206 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
207 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, ctx
->Const
.MaxTextureLevels
- 1);
209 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
210 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
212 if (t
->Dimensions
== 6) {
213 /* make sure that all six cube map level 0 images are the same size */
214 const GLint w
= t
->Image
[baseLevel
]->Width2
;
215 const GLint h
= t
->Image
[baseLevel
]->Height2
;
216 if (!t
->NegX
[baseLevel
] ||
217 t
->NegX
[baseLevel
]->Width2
!= w
||
218 t
->NegX
[baseLevel
]->Height2
!= h
||
219 !t
->PosY
[baseLevel
] ||
220 t
->PosY
[baseLevel
]->Width2
!= w
||
221 t
->PosY
[baseLevel
]->Height2
!= h
||
222 !t
->NegY
[baseLevel
] ||
223 t
->NegY
[baseLevel
]->Width2
!= w
||
224 t
->NegY
[baseLevel
]->Height2
!= h
||
225 !t
->PosZ
[baseLevel
] ||
226 t
->PosZ
[baseLevel
]->Width2
!= w
||
227 t
->PosZ
[baseLevel
]->Height2
!= h
||
228 !t
->NegZ
[baseLevel
] ||
229 t
->NegZ
[baseLevel
]->Width2
!= w
||
230 t
->NegZ
[baseLevel
]->Height2
!= h
) {
231 t
->Complete
= GL_FALSE
;
232 incomplete(t
, "Non-quare cubemap image");
237 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
239 * Mipmapping: determine if we have a complete set of mipmaps
242 GLint minLevel
= baseLevel
;
243 GLint maxLevel
= t
->_MaxLevel
;
245 if (minLevel
> maxLevel
) {
246 t
->Complete
= GL_FALSE
;
247 incomplete(t
, "minLevel > maxLevel");
251 /* Test dimension-independent attributes */
252 for (i
= minLevel
; i
<= maxLevel
; i
++) {
254 if (t
->Image
[i
]->Format
!= t
->Image
[baseLevel
]->Format
) {
255 t
->Complete
= GL_FALSE
;
256 incomplete(t
, "Format[i] != Format[baseLevel]");
259 if (t
->Image
[i
]->Border
!= t
->Image
[baseLevel
]->Border
) {
260 t
->Complete
= GL_FALSE
;
261 incomplete(t
, "Border[i] != Border[baseLevel]");
267 /* Test things which depend on number of texture image dimensions */
268 if (t
->Dimensions
== 1) {
269 /* Test 1-D mipmaps */
270 GLuint width
= t
->Image
[baseLevel
]->Width2
;
271 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
275 if (i
>= minLevel
&& i
<= maxLevel
) {
277 t
->Complete
= GL_FALSE
;
278 incomplete(t
, "1D Image[i] == NULL");
281 if (t
->Image
[i
]->Width2
!= width
) {
282 t
->Complete
= GL_FALSE
;
283 incomplete(t
, "1D Image[i] bad width");
288 return; /* found smallest needed mipmap, all done! */
292 else if (t
->Dimensions
== 2) {
293 /* Test 2-D mipmaps */
294 GLuint width
= t
->Image
[baseLevel
]->Width2
;
295 GLuint height
= t
->Image
[baseLevel
]->Height2
;
296 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
303 if (i
>= minLevel
&& i
<= maxLevel
) {
305 t
->Complete
= GL_FALSE
;
306 incomplete(t
, "2D Image[i] == NULL");
309 if (t
->Image
[i
]->Width2
!= width
) {
310 t
->Complete
= GL_FALSE
;
311 incomplete(t
, "2D Image[i] bad width");
314 if (t
->Image
[i
]->Height2
!= height
) {
315 t
->Complete
= GL_FALSE
;
316 incomplete(t
, "2D Image[i] bad height");
319 if (width
==1 && height
==1) {
320 return; /* found smallest needed mipmap, all done! */
325 else if (t
->Dimensions
== 3) {
326 /* Test 3-D mipmaps */
327 GLuint width
= t
->Image
[baseLevel
]->Width2
;
328 GLuint height
= t
->Image
[baseLevel
]->Height2
;
329 GLuint depth
= t
->Image
[baseLevel
]->Depth2
;
330 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
340 if (i
>= minLevel
&& i
<= maxLevel
) {
342 incomplete(t
, "3D Image[i] == NULL");
343 t
->Complete
= GL_FALSE
;
346 if (t
->Image
[i
]->Width2
!= width
) {
347 t
->Complete
= GL_FALSE
;
348 incomplete(t
, "3D Image[i] bad width");
351 if (t
->Image
[i
]->Height2
!= height
) {
352 t
->Complete
= GL_FALSE
;
353 incomplete(t
, "3D Image[i] bad height");
356 if (t
->Image
[i
]->Depth2
!= depth
) {
357 t
->Complete
= GL_FALSE
;
358 incomplete(t
, "3D Image[i] bad depth");
362 if (width
== 1 && height
== 1 && depth
== 1) {
363 return; /* found smallest needed mipmap, all done! */
367 else if (t
->Dimensions
== 6) {
368 /* make sure 6 cube faces are consistant */
369 GLuint width
= t
->Image
[baseLevel
]->Width2
;
370 GLuint height
= t
->Image
[baseLevel
]->Height2
;
371 for (i
= baseLevel
+ 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
378 if (i
>= minLevel
&& i
<= maxLevel
) {
379 /* check that we have images defined */
380 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
381 !t
->PosY
[i
] || !t
->NegY
[i
] ||
382 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
383 t
->Complete
= GL_FALSE
;
384 incomplete(t
, "CubeMap Image[i] == NULL");
387 /* check that all six images have same size */
388 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
389 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
390 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
391 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
392 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
393 t
->Complete
= GL_FALSE
;
394 incomplete(t
, "CubeMap Image[i] bad size");
398 if (width
== 1 && height
== 1) {
399 return; /* found smallest needed mipmap, all done! */
404 /* Dimensions = ??? */
405 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
411 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
415 * Execute glGenTextures
418 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
420 GET_CURRENT_CONTEXT(ctx
);
423 ASSERT_OUTSIDE_BEGIN_END(ctx
);
426 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
434 * This must be atomic (generation and allocation of texture IDs)
436 _glthread_LOCK_MUTEX(GenTexturesLock
);
438 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
440 /* Return the texture names */
442 texName
[i
] = first
+ i
;
445 /* Allocate new, empty texture objects */
447 GLuint name
= first
+ i
;
449 (void) _mesa_alloc_texture_object(ctx
->Shared
, name
, dims
);
452 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
458 * Execute glDeleteTextures
461 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
463 GET_CURRENT_CONTEXT(ctx
);
465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
471 if (texName
[i
] > 0) {
472 struct gl_texture_object
*delObj
= (struct gl_texture_object
*)
473 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
475 /* First check if this texture is currently bound.
476 * If so, unbind it and decrement the reference count.
479 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
480 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
481 if (delObj
== unit
->Current1D
) {
482 unit
->Current1D
= ctx
->Shared
->Default1D
;
483 ctx
->Shared
->Default1D
->RefCount
++;
485 else if (delObj
== unit
->Current2D
) {
486 unit
->Current2D
= ctx
->Shared
->Default2D
;
487 ctx
->Shared
->Default2D
->RefCount
++;
489 else if (delObj
== unit
->Current3D
) {
490 unit
->Current3D
= ctx
->Shared
->Default3D
;
491 ctx
->Shared
->Default3D
->RefCount
++;
493 else if (delObj
== unit
->CurrentCubeMap
) {
494 unit
->CurrentCubeMap
= ctx
->Shared
->DefaultCubeMap
;
495 ctx
->Shared
->DefaultCubeMap
->RefCount
++;
498 ctx
->NewState
|= _NEW_TEXTURE
;
500 /* Decrement reference count and delete if zero */
502 ASSERT( delObj
->RefCount
>= 0 );
503 if (delObj
->RefCount
== 0) {
504 if (ctx
->Driver
.DeleteTexture
)
505 (*ctx
->Driver
.DeleteTexture
)( ctx
, delObj
);
506 _mesa_free_texture_object(ctx
->Shared
, delObj
);
516 * Execute glBindTexture
519 _mesa_BindTexture( GLenum target
, GLuint texName
)
521 GET_CURRENT_CONTEXT(ctx
);
522 GLuint unit
= ctx
->Texture
.CurrentUnit
;
523 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
524 struct gl_texture_object
*oldTexObj
;
525 struct gl_texture_object
*newTexObj
;
527 ASSERT_OUTSIDE_BEGIN_END(ctx
);
529 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
530 fprintf(stderr
, "glBindTexture %s %d\n",
531 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
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
++;
609 /* do the actual binding, but first flush outstanding vertices:
611 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
615 texUnit
->Current1D
= newTexObj
;
618 texUnit
->Current2D
= newTexObj
;
621 texUnit
->Current3D
= newTexObj
;
623 case GL_TEXTURE_CUBE_MAP_ARB
:
624 texUnit
->CurrentCubeMap
= newTexObj
;
627 gl_problem(ctx
, "bad target in BindTexture");
630 /* Pass BindTexture call to device driver */
631 if (ctx
->Driver
.BindTexture
)
632 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
634 if (oldTexObj
->Name
> 0) {
635 /* never delete default (id=0) texture objects */
636 oldTexObj
->RefCount
--;
637 if (oldTexObj
->RefCount
<= 0) {
638 if (ctx
->Driver
.DeleteTexture
) {
639 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
641 _mesa_free_texture_object(ctx
->Shared
, oldTexObj
);
649 * Execute glPrioritizeTextures
652 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
653 const GLclampf
*priorities
)
655 GET_CURRENT_CONTEXT(ctx
);
657 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
660 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
667 for (i
= 0; i
< n
; i
++) {
668 if (texName
[i
] > 0) {
669 struct gl_texture_object
*t
= (struct gl_texture_object
*)
670 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
672 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
673 if (ctx
->Driver
.PrioritizeTexture
)
674 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
679 ctx
->NewState
|= _NEW_TEXTURE
;
685 * Execute glAreTexturesResident
688 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
689 GLboolean
*residences
)
691 GET_CURRENT_CONTEXT(ctx
);
692 GLboolean allResident
= GL_TRUE
;
694 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
697 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
701 if (!texName
|| !residences
)
704 for (i
= 0; i
< n
; i
++) {
705 struct gl_texture_object
*t
;
706 if (texName
[i
] == 0) {
707 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
710 t
= (struct gl_texture_object
*)
711 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
713 if (ctx
->Driver
.IsTextureResident
) {
714 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
716 allResident
= GL_FALSE
;
719 residences
[i
] = GL_TRUE
;
723 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
733 * Execute glIsTexture
736 _mesa_IsTexture( GLuint texture
)
738 GET_CURRENT_CONTEXT(ctx
);
739 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
740 return texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
);