62238ff6fd4d45850f46a5b765c10978d2ee0f8a
1 /* $Id: texobj.c,v 1.30 2000/10/29 18:23:16 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 * 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 obj
->MinMagThresh
= 0.0F
;
81 _mesa_init_colortable(&obj
->Palette
);
83 /* insert into linked list */
85 _glthread_LOCK_MUTEX(shared
->Mutex
);
86 obj
->Next
= shared
->TexObjectList
;
87 shared
->TexObjectList
= obj
;
88 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
92 /* insert into hash table */
93 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
101 * Deallocate a texture object struct and remove it from the given
103 * Input: shared - the shared GL state to which the object belongs
104 * t - the texture object to delete
106 void gl_free_texture_object( struct gl_shared_state
*shared
,
107 struct gl_texture_object
*t
)
109 struct gl_texture_object
*tprev
, *tcurr
;
113 /* Remove t from dirty list so we don't touch free'd memory later.
114 * Test for shared since Proxy texture aren't in global linked list.
117 gl_remove_texobj_from_dirty_list( shared
, t
);
119 /* unlink t from the linked list */
121 _glthread_LOCK_MUTEX(shared
->Mutex
);
123 tcurr
= shared
->TexObjectList
;
127 tprev
->Next
= t
->Next
;
130 shared
->TexObjectList
= t
->Next
;
137 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
141 /* remove from hash table */
142 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
145 _mesa_free_colortable_data(&t
->Palette
);
147 /* free texture images */
150 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
152 _mesa_free_texture_image( t
->Image
[i
] );
156 /* free this object */
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 or not.
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 level zero 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 all six level 0 images are 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) gl_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 struct gl_texture_object
*t
;
472 t
= (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
];
482 for (d
= 1 ; d
<= 3 ; d
++) {
483 if (unit
->CurrentD
[d
] == t
) {
484 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
485 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
487 ASSERT( t
->RefCount
>= 0 );
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
.HaveTextureCubeMap
) {
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 /* Check if we may have to use a new triangle rasterizer */
602 if ((ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) &&
603 ( oldTexObj
->WrapS
!= newTexObj
->WrapS
604 || oldTexObj
->WrapT
!= newTexObj
->WrapT
605 || oldTexObj
->WrapR
!= newTexObj
->WrapR
606 || oldTexObj
->MinFilter
!= newTexObj
->MinFilter
607 || oldTexObj
->MagFilter
!= newTexObj
->MagFilter
608 || (oldTexObj
->Image
[0] && newTexObj
->Image
[0] &&
609 (oldTexObj
->Image
[0]->Format
!=newTexObj
->Image
[0]->Format
))))
611 ctx
->NewState
|= (NEW_RASTER_OPS
| NEW_TEXTURING
);
614 if (oldTexObj
->Complete
!= newTexObj
->Complete
)
615 ctx
->NewState
|= NEW_TEXTURING
;
617 /* Pass BindTexture call to device driver */
618 if (ctx
->Driver
.BindTexture
) {
619 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
620 /* Make sure the Driver.UpdateState() function gets called! */
621 ctx
->NewState
|= NEW_TEXTURING
;
624 if (oldTexObj
->Name
> 0) {
625 /* never delete default (id=0) texture objects */
626 oldTexObj
->RefCount
--;
627 if (oldTexObj
->RefCount
<= 0) {
628 if (ctx
->Driver
.DeleteTexture
) {
629 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
631 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
639 * Execute glPrioritizeTextures
642 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
643 const GLclampf
*priorities
)
645 GET_CURRENT_CONTEXT(ctx
);
648 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
650 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
657 for (i
= 0; i
< n
; i
++) {
658 if (texName
[i
] > 0) {
659 struct gl_texture_object
*t
= (struct gl_texture_object
*)
660 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
662 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
663 if (ctx
->Driver
.PrioritizeTexture
)
664 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
673 * Execute glAreTexturesResident
676 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
677 GLboolean
*residences
)
679 GET_CURRENT_CONTEXT(ctx
);
680 GLboolean allResident
= GL_TRUE
;
683 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
684 "glAreTexturesResident", GL_FALSE
);
686 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
690 if (!texName
|| !residences
)
693 for (i
= 0; i
< n
; i
++) {
694 struct gl_texture_object
*t
;
695 if (texName
[i
] == 0) {
696 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
699 t
= (struct gl_texture_object
*)
700 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
702 if (ctx
->Driver
.IsTextureResident
) {
703 residences
[i
] = ctx
->Driver
.IsTextureResident(ctx
, t
);
705 allResident
= GL_FALSE
;
708 residences
[i
] = GL_TRUE
;
712 gl_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)");
722 * Execute glIsTexture
725 _mesa_IsTexture( GLuint texture
)
727 GET_CURRENT_CONTEXT(ctx
);
728 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
730 if (texture
> 0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {