3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 * Allocate a new texture object and add it to the linked list of texture
46 * objects. If name>0 then also insert the new texture object into the hash
48 * Input: shared - the shared GL state structure to contain the texture object
49 * name - integer name for the texture object
50 * dimensions - either 1, 2, 3 or 6 (cube map)
51 * Return: pointer to new texture object
53 struct gl_texture_object
*
54 gl_alloc_texture_object( struct gl_shared_state
*shared
, GLuint name
,
57 struct gl_texture_object
*obj
;
59 ASSERT(dimensions
<= 3 || dimensions
== 6);
61 obj
= CALLOC_STRUCT(gl_texture_object
);
64 /* init the non-zero fields */
67 obj
->Dimensions
= dimensions
;
68 obj
->WrapS
= GL_REPEAT
;
69 obj
->WrapT
= GL_REPEAT
;
70 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
71 obj
->MagFilter
= GL_LINEAR
;
72 obj
->MinLod
= -1000.0;
76 obj
->MinMagThresh
= 0.0F
;
77 _mesa_init_colortable(&obj
->Palette
);
79 /* insert into linked list */
81 _glthread_LOCK_MUTEX(shared
->Mutex
);
82 obj
->Next
= shared
->TexObjectList
;
83 shared
->TexObjectList
= obj
;
84 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
88 /* insert into hash table */
89 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
97 * Deallocate a texture object struct and remove it from the given
99 * Input: shared - the shared GL state to which the object belongs
100 * t - the texture object to delete
102 void gl_free_texture_object( struct gl_shared_state
*shared
,
103 struct gl_texture_object
*t
)
105 struct gl_texture_object
*tprev
, *tcurr
;
109 /* Remove t from dirty list so we don't touch free'd memory later.
110 * Test for shared since Proxy texture aren't in global linked list.
113 gl_remove_texobj_from_dirty_list( shared
, t
);
115 /* unlink t from the linked list */
117 _glthread_LOCK_MUTEX(shared
->Mutex
);
119 tcurr
= shared
->TexObjectList
;
123 tprev
->Next
= t
->Next
;
126 shared
->TexObjectList
= t
->Next
;
133 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
137 /* remove from hash table */
138 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
141 _mesa_free_colortable_data(&t
->Palette
);
143 /* free texture images */
146 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
148 _mesa_free_texture_image( t
->Image
[i
] );
152 /* free this object */
159 * Examine a texture object to determine if it is complete or not.
160 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
163 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
164 struct gl_texture_object
*t
)
166 t
->Complete
= GL_TRUE
; /* be optimistic */
168 /* Always need level zero image */
170 t
->Complete
= GL_FALSE
;
174 /* Compute number of mipmap levels */
175 if (t
->Dimensions
== 1) {
176 t
->P
= t
->Image
[0]->WidthLog2
;
178 else if (t
->Dimensions
== 2 || t
->Dimensions
== 6) {
179 t
->P
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
181 else if (t
->Dimensions
== 3) {
182 GLint max
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
183 max
= MAX2(max
, (GLint
)(t
->Image
[0]->DepthLog2
));
187 /* Compute M (see the 1.2 spec) used during mipmapping */
188 t
->M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->P
) - t
->BaseLevel
);
191 if (t
->Dimensions
== 6) {
192 /* make sure all six level 0 images are same size */
193 const GLint w
= t
->Image
[0]->Width2
;
194 const GLint h
= t
->Image
[0]->Height2
;
195 if (!t
->NegX
[0] || t
->NegX
[0]->Width2
!= w
|| t
->NegX
[0]->Height2
!= h
||
196 !t
->PosY
[0] || t
->PosY
[0]->Width2
!= w
|| t
->PosY
[0]->Height2
!= h
||
197 !t
->NegY
[0] || t
->NegY
[0]->Width2
!= w
|| t
->NegY
[0]->Height2
!= h
||
198 !t
->PosZ
[0] || t
->PosZ
[0]->Width2
!= w
|| t
->PosZ
[0]->Height2
!= h
||
199 !t
->NegZ
[0] || t
->NegZ
[0]->Width2
!= w
|| t
->NegZ
[0]->Height2
!= h
) {
200 t
->Complete
= GL_FALSE
;
205 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
207 * Mipmapping: determine if we have a complete set of mipmaps
210 GLint minLevel
= t
->BaseLevel
;
211 GLint maxLevel
= MIN2(t
->P
, ctx
->Const
.MaxTextureLevels
-1);
212 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
214 if (minLevel
> maxLevel
) {
215 t
->Complete
= GL_FALSE
;
219 /* Test dimension-independent attributes */
220 for (i
= minLevel
; i
<= maxLevel
; i
++) {
222 if (t
->Image
[i
]->Format
!= t
->Image
[0]->Format
) {
223 t
->Complete
= GL_FALSE
;
226 if (t
->Image
[i
]->Border
!= t
->Image
[0]->Border
) {
227 t
->Complete
= GL_FALSE
;
233 /* Test things which depend on number of texture image dimensions */
234 if (t
->Dimensions
== 1) {
235 /* Test 1-D mipmaps */
236 GLuint width
= t
->Image
[0]->Width2
;
237 for (i
= 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
241 if (i
>= minLevel
&& i
<= maxLevel
) {
243 t
->Complete
= GL_FALSE
;
246 if (t
->Image
[i
]->Width2
!= width
) {
247 t
->Complete
= GL_FALSE
;
252 return; /* found smallest needed mipmap, all done! */
256 else if (t
->Dimensions
== 2) {
257 /* Test 2-D mipmaps */
258 GLuint width
= t
->Image
[0]->Width2
;
259 GLuint height
= t
->Image
[0]->Height2
;
260 for (i
= 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
267 if (i
>= minLevel
&& i
<= maxLevel
) {
269 t
->Complete
= GL_FALSE
;
272 if (t
->Image
[i
]->Width2
!= width
) {
273 t
->Complete
= GL_FALSE
;
276 if (t
->Image
[i
]->Height2
!= height
) {
277 t
->Complete
= GL_FALSE
;
280 if (width
==1 && height
==1) {
281 return; /* found smallest needed mipmap, all done! */
286 else if (t
->Dimensions
== 3) {
287 /* Test 3-D mipmaps */
288 GLuint width
= t
->Image
[0]->Width2
;
289 GLuint height
= t
->Image
[0]->Height2
;
290 GLuint depth
= t
->Image
[0]->Depth2
;
291 for (i
= 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
301 if (i
>= minLevel
&& i
<= maxLevel
) {
303 t
->Complete
= GL_FALSE
;
306 if (t
->Image
[i
]->Width2
!= width
) {
307 t
->Complete
= GL_FALSE
;
310 if (t
->Image
[i
]->Height2
!= height
) {
311 t
->Complete
= GL_FALSE
;
314 if (t
->Image
[i
]->Depth2
!= depth
) {
315 t
->Complete
= GL_FALSE
;
319 if (width
== 1 && height
== 1 && depth
== 1) {
320 return; /* found smallest needed mipmap, all done! */
324 else if (t
->Dimensions
== 6) {
325 /* make sure 6 cube faces are consistant */
326 GLuint width
= t
->Image
[0]->Width2
;
327 GLuint height
= t
->Image
[0]->Height2
;
328 for (i
= 1; i
< ctx
->Const
.MaxTextureLevels
; i
++) {
335 if (i
>= minLevel
&& i
<= maxLevel
) {
336 /* check that we have images defined */
337 if (!t
->Image
[i
] || !t
->NegX
[i
] ||
338 !t
->PosY
[i
] || !t
->NegY
[i
] ||
339 !t
->PosZ
[i
] || !t
->NegZ
[i
]) {
340 t
->Complete
= GL_FALSE
;
343 /* check that all six images have same size */
344 if (t
->NegX
[i
]->Width2
!=width
|| t
->NegX
[i
]->Height2
!=height
||
345 t
->PosY
[i
]->Width2
!=width
|| t
->PosY
[i
]->Height2
!=height
||
346 t
->NegY
[i
]->Width2
!=width
|| t
->NegY
[i
]->Height2
!=height
||
347 t
->PosZ
[i
]->Width2
!=width
|| t
->PosZ
[i
]->Height2
!=height
||
348 t
->NegZ
[i
]->Width2
!=width
|| t
->NegZ
[i
]->Height2
!=height
) {
349 t
->Complete
= GL_FALSE
;
353 if (width
== 1 && height
== 1) {
354 return; /* found smallest needed mipmap, all done! */
359 /* Dimensions = ??? */
360 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
366 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
370 * Execute glGenTextures
373 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
375 GET_CURRENT_CONTEXT(ctx
);
379 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
381 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
387 * This must be atomic (generation and allocation of texture IDs)
389 _glthread_LOCK_MUTEX(GenTexturesLock
);
391 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
393 /* Return the texture names */
395 texName
[i
] = first
+ i
;
398 /* Allocate new, empty texture objects */
400 GLuint name
= first
+ i
;
402 (void) gl_alloc_texture_object(ctx
->Shared
, name
, dims
);
405 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
411 * Execute glDeleteTextures
414 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
416 GET_CURRENT_CONTEXT(ctx
);
419 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
422 struct gl_texture_object
*t
;
424 t
= (struct gl_texture_object
*)
425 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
427 /* First check if this texture is currently bound.
428 * If so, unbind it and decrement the reference count.
431 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
432 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
434 for (d
= 1 ; d
<= 3 ; d
++) {
435 if (unit
->CurrentD
[d
] == t
) {
436 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
437 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
439 ASSERT( t
->RefCount
>= 0 );
444 /* Decrement reference count and delete if zero */
446 ASSERT( t
->RefCount
>= 0 );
447 if (t
->RefCount
== 0) {
448 if (ctx
->Driver
.DeleteTexture
)
449 (*ctx
->Driver
.DeleteTexture
)( ctx
, t
);
450 gl_free_texture_object(ctx
->Shared
, t
);
460 * Execute glBindTexture
463 _mesa_BindTexture( GLenum target
, GLuint texName
)
465 GET_CURRENT_CONTEXT(ctx
);
466 GLuint unit
= ctx
->Texture
.CurrentUnit
;
467 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
468 struct gl_texture_object
*oldTexObj
;
469 struct gl_texture_object
*newTexObj
;
472 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
473 fprintf(stderr
, "glBindTexture %s %d\n",
474 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
481 oldTexObj
= texUnit
->CurrentD
[1];
485 oldTexObj
= texUnit
->CurrentD
[2];
489 oldTexObj
= texUnit
->CurrentD
[3];
491 case GL_TEXTURE_CUBE_MAP_ARB
:
492 if (ctx
->Extensions
.HaveTextureCubeMap
) {
494 oldTexObj
= texUnit
->CurrentCubeMap
;
499 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
503 if (oldTexObj
->Name
== texName
)
507 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
508 newTexObj
= ctx
->Shared
->DefaultCubeMap
;
510 newTexObj
= ctx
->Shared
->DefaultD
[dim
];
513 struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
514 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
517 newTexObj
= gl_alloc_texture_object(ctx
->Shared
, texName
, dim
);
519 if (newTexObj
->Dimensions
!= dim
) {
520 if (newTexObj
->Dimensions
) {
521 /* the named texture object's dimensions don't match the target */
522 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
525 newTexObj
->Dimensions
= dim
;
529 newTexObj
->RefCount
++;
533 texUnit
->CurrentD
[1] = newTexObj
;
536 texUnit
->CurrentD
[2] = newTexObj
;
539 texUnit
->CurrentD
[3] = newTexObj
;
541 case GL_TEXTURE_CUBE_MAP_ARB
:
542 texUnit
->CurrentCubeMap
= newTexObj
;
545 gl_problem(ctx
, "bad target in BindTexture");
548 /* If we've changed the CurrentD[123] texture object then update the
549 * ctx->Texture.Current pointer to point to the new texture object.
551 texUnit
->Current
= texUnit
->CurrentD
[texUnit
->CurrentDimension
];
553 /* Check if we may have to use a new triangle rasterizer */
554 if ((ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) &&
555 ( oldTexObj
->WrapS
!= newTexObj
->WrapS
556 || oldTexObj
->WrapT
!= newTexObj
->WrapT
557 || oldTexObj
->WrapR
!= newTexObj
->WrapR
558 || oldTexObj
->MinFilter
!= newTexObj
->MinFilter
559 || oldTexObj
->MagFilter
!= newTexObj
->MagFilter
560 || (oldTexObj
->Image
[0] && newTexObj
->Image
[0] &&
561 (oldTexObj
->Image
[0]->Format
!=newTexObj
->Image
[0]->Format
))))
563 ctx
->NewState
|= (NEW_RASTER_OPS
| NEW_TEXTURING
);
566 if (oldTexObj
->Complete
!= newTexObj
->Complete
)
567 ctx
->NewState
|= NEW_TEXTURING
;
569 /* Pass BindTexture call to device driver */
570 if (ctx
->Driver
.BindTexture
) {
571 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
574 if (oldTexObj
->Name
> 0) {
575 /* never delete default (id=0) texture objects */
576 oldTexObj
->RefCount
--;
577 if (oldTexObj
->RefCount
<= 0) {
578 if (ctx
->Driver
.DeleteTexture
) {
579 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
581 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
589 * Execute glPrioritizeTextures
592 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
593 const GLclampf
*priorities
)
595 GET_CURRENT_CONTEXT(ctx
);
598 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
600 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
605 struct gl_texture_object
*t
;
607 t
= (struct gl_texture_object
*)
608 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
610 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
612 if (ctx
->Driver
.PrioritizeTexture
)
613 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
622 * Execute glAreTexturesResident
625 _mesa_AreTexturesResident( GLsizei n
, const GLuint
*texName
,
626 GLboolean
*residences
)
628 GET_CURRENT_CONTEXT(ctx
);
629 GLboolean resident
= GL_TRUE
;
632 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
633 "glAreTexturesResident",
636 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)" );
641 struct gl_texture_object
*t
;
643 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
646 t
= (struct gl_texture_object
*)
647 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
649 if (ctx
->Driver
.IsTextureResident
)
650 residences
[i
] = ctx
->Driver
.IsTextureResident( ctx
, t
);
652 residences
[i
] = GL_TRUE
;
655 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
665 * Execute glIsTexture
668 _mesa_IsTexture( GLuint texture
)
670 GET_CURRENT_CONTEXT(ctx
);
671 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
673 if (texture
>0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {