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.
44 * Allocate a new texture object and add it to the linked list of texture
45 * objects. If name>0 then also insert the new texture object into the hash
47 * Input: shared - the shared GL state structure to contain the texture object
48 * name - integer name for the texture object
49 * dimensions - either 1, 2 or 3
50 * Return: pointer to new texture object
52 struct gl_texture_object
*
53 gl_alloc_texture_object( struct gl_shared_state
*shared
, GLuint name
,
56 struct gl_texture_object
*obj
;
58 ASSERT(dimensions
<= 3);
60 obj
= CALLOC_STRUCT(gl_texture_object
);
63 /* init the non-zero fields */
66 obj
->Dimensions
= dimensions
;
67 obj
->WrapS
= GL_REPEAT
;
68 obj
->WrapT
= GL_REPEAT
;
69 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
70 obj
->MagFilter
= GL_LINEAR
;
71 obj
->MinLod
= -1000.0;
75 obj
->MinMagThresh
= 0.0F
;
76 obj
->Palette
.Table
[0] = 255;
77 obj
->Palette
.Table
[1] = 255;
78 obj
->Palette
.Table
[2] = 255;
79 obj
->Palette
.Table
[3] = 255;
80 obj
->Palette
.Size
= 1;
81 obj
->Palette
.IntFormat
= GL_RGBA
;
82 obj
->Palette
.Format
= GL_RGBA
;
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 gl_free_texture_object( struct gl_shared_state
*shared
,
108 struct gl_texture_object
*t
)
110 struct gl_texture_object
*tprev
, *tcurr
;
114 /* Remove t from dirty list so we don't touch free'd memory later.
115 * Test for shared since Proxy texture aren't in global linked list.
118 gl_remove_texobj_from_dirty_list( shared
, t
);
120 /* unlink t from the linked list */
122 _glthread_LOCK_MUTEX(shared
->Mutex
);
124 tcurr
= shared
->TexObjectList
;
128 tprev
->Next
= t
->Next
;
131 shared
->TexObjectList
= t
->Next
;
138 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
142 /* remove from hash table */
143 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
146 /* free texture image */
149 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
151 _mesa_free_texture_image( t
->Image
[i
] );
155 /* free this object */
162 * Examine a texture object to determine if it is complete or not.
163 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
165 void gl_test_texture_object_completeness( const GLcontext
*ctx
, struct gl_texture_object
*t
)
167 t
->Complete
= GL_TRUE
; /* be optimistic */
169 /* Always need level zero image */
171 t
->Complete
= GL_FALSE
;
175 /* Compute number of mipmap levels */
176 if (t
->Dimensions
==1) {
177 t
->P
= t
->Image
[0]->WidthLog2
;
179 else if (t
->Dimensions
==2) {
180 t
->P
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
182 else if (t
->Dimensions
==3) {
183 GLint max
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
184 max
= MAX2(max
, (GLint
)(t
->Image
[0]->DepthLog2
));
188 /* Compute M (see the 1.2 spec) used during mipmapping */
189 t
->M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->P
) - t
->BaseLevel
);
192 if (t
->MinFilter
!=GL_NEAREST
&& t
->MinFilter
!=GL_LINEAR
) {
194 * Mipmapping: determine if we have a complete set of mipmaps
197 GLint minLevel
= t
->BaseLevel
;
198 GLint maxLevel
= MIN2(t
->P
, ctx
->Const
.MaxTextureLevels
-1);
199 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
201 if (minLevel
> maxLevel
) {
202 t
->Complete
= GL_FALSE
;
206 /* Test dimension-independent attributes */
207 for (i
= minLevel
; i
<= maxLevel
; i
++) {
209 if (t
->Image
[i
]->Format
!= t
->Image
[0]->Format
) {
210 t
->Complete
= GL_FALSE
;
213 if (t
->Image
[i
]->Border
!= t
->Image
[0]->Border
) {
214 t
->Complete
= GL_FALSE
;
220 /* Test things which depend on number of texture image dimensions */
221 if (t
->Dimensions
==1) {
222 /* Test 1-D mipmaps */
223 GLuint width
= t
->Image
[0]->Width2
;
224 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
228 if (i
>= minLevel
&& i
<= maxLevel
) {
230 t
->Complete
= GL_FALSE
;
233 if (t
->Image
[i
]->Width2
!= width
) {
234 t
->Complete
= GL_FALSE
;
239 return; /* found smallest needed mipmap, all done! */
243 else if (t
->Dimensions
==2) {
244 /* Test 2-D mipmaps */
245 GLuint width
= t
->Image
[0]->Width2
;
246 GLuint height
= t
->Image
[0]->Height2
;
247 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
254 if (i
>= minLevel
&& i
<= maxLevel
) {
256 t
->Complete
= GL_FALSE
;
259 if (t
->Image
[i
]->Width2
!= width
) {
260 t
->Complete
= GL_FALSE
;
263 if (t
->Image
[i
]->Height2
!= height
) {
264 t
->Complete
= GL_FALSE
;
267 if (width
==1 && height
==1) {
268 return; /* found smallest needed mipmap, all done! */
273 else if (t
->Dimensions
==3) {
274 /* Test 3-D mipmaps */
275 GLuint width
= t
->Image
[0]->Width2
;
276 GLuint height
= t
->Image
[0]->Height2
;
277 GLuint depth
= t
->Image
[0]->Depth2
;
278 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
288 if (i
>= minLevel
&& i
<= maxLevel
) {
290 t
->Complete
= GL_FALSE
;
293 if (t
->Image
[i
]->Width2
!= width
) {
294 t
->Complete
= GL_FALSE
;
297 if (t
->Image
[i
]->Height2
!= height
) {
298 t
->Complete
= GL_FALSE
;
301 if (t
->Image
[i
]->Depth2
!= depth
) {
302 t
->Complete
= GL_FALSE
;
306 if (width
==1 && height
==1 && depth
==1) {
307 return; /* found smallest needed mipmap, all done! */
312 /* Dimensions = ??? */
313 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
319 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock
);
323 * Execute glGenTextures
326 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
328 GET_CURRENT_CONTEXT(ctx
);
332 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
334 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
340 * This must be atomic (generation and allocation of texture IDs)
342 _glthread_LOCK_MUTEX(GenTexturesLock
);
344 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
346 /* Return the texture names */
348 texName
[i
] = first
+ i
;
351 /* Allocate new, empty texture objects */
353 GLuint name
= first
+ i
;
355 (void) gl_alloc_texture_object(ctx
->Shared
, name
, dims
);
358 _glthread_UNLOCK_MUTEX(GenTexturesLock
);
364 * Execute glDeleteTextures
367 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
369 GET_CURRENT_CONTEXT(ctx
);
372 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
375 struct gl_texture_object
*t
;
377 t
= (struct gl_texture_object
*)
378 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
380 /* First check if this texture is currently bound.
381 * If so, unbind it and decrement the reference count.
384 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
385 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
387 for (d
= 1 ; d
<= 3 ; d
++) {
388 if (unit
->CurrentD
[d
] == t
) {
389 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
390 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
392 ASSERT( t
->RefCount
>= 0 );
397 /* Decrement reference count and delete if zero */
399 ASSERT( t
->RefCount
>= 0 );
400 if (t
->RefCount
== 0) {
401 if (ctx
->Driver
.DeleteTexture
)
402 (*ctx
->Driver
.DeleteTexture
)( ctx
, t
);
403 gl_free_texture_object(ctx
->Shared
, t
);
413 * Execute glBindTexture
416 _mesa_BindTexture( GLenum target
, GLuint texName
)
418 GET_CURRENT_CONTEXT(ctx
);
419 GLuint unit
= ctx
->Texture
.CurrentUnit
;
420 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
421 struct gl_texture_object
*oldTexObj
;
422 struct gl_texture_object
*newTexObj
;
425 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
426 fprintf(stderr
, "glBindTexture %s %d\n",
427 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
429 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
442 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
446 oldTexObj
= texUnit
->CurrentD
[dim
];
448 if (oldTexObj
->Name
== texName
)
452 newTexObj
= ctx
->Shared
->DefaultD
[dim
];
454 struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
455 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
458 newTexObj
= gl_alloc_texture_object(ctx
->Shared
, texName
, dim
);
460 if (newTexObj
->Dimensions
!= dim
) {
461 if (newTexObj
->Dimensions
) {
462 /* the named texture object's dimensions don't match the target */
463 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
466 newTexObj
->Dimensions
= dim
;
470 newTexObj
->RefCount
++;
472 texUnit
->CurrentD
[dim
] = newTexObj
;
474 /* If we've changed the CurrentD[123] texture object then update the
475 * ctx->Texture.Current pointer to point to the new texture object.
477 texUnit
->Current
= texUnit
->CurrentD
[texUnit
->CurrentDimension
];
479 /* Check if we may have to use a new triangle rasterizer */
480 if ((ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) &&
481 ( oldTexObj
->WrapS
!= newTexObj
->WrapS
482 || oldTexObj
->WrapT
!= newTexObj
->WrapT
483 || oldTexObj
->WrapR
!= newTexObj
->WrapR
484 || oldTexObj
->MinFilter
!= newTexObj
->MinFilter
485 || oldTexObj
->MagFilter
!= newTexObj
->MagFilter
486 || (oldTexObj
->Image
[0] && newTexObj
->Image
[0] &&
487 (oldTexObj
->Image
[0]->Format
!=newTexObj
->Image
[0]->Format
))))
489 ctx
->NewState
|= (NEW_RASTER_OPS
| NEW_TEXTURING
);
492 if (oldTexObj
->Complete
!= newTexObj
->Complete
)
493 ctx
->NewState
|= NEW_TEXTURING
;
495 /* Pass BindTexture call to device driver */
496 if (ctx
->Driver
.BindTexture
) {
497 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
500 if (oldTexObj
->Name
> 0) {
501 /* never delete default (id=0) texture objects */
502 oldTexObj
->RefCount
--;
503 if (oldTexObj
->RefCount
<= 0) {
504 if (ctx
->Driver
.DeleteTexture
) {
505 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
507 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
515 * Execute glPrioritizeTextures
518 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
519 const GLclampf
*priorities
)
521 GET_CURRENT_CONTEXT(ctx
);
524 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
526 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
531 struct gl_texture_object
*t
;
533 t
= (struct gl_texture_object
*)
534 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
536 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
538 if (ctx
->Driver
.PrioritizeTexture
)
539 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
548 * Execute glAreTexturesResident
551 _mesa_AreTexturesResident( GLsizei n
, const GLuint
*texName
,
552 GLboolean
*residences
)
554 GET_CURRENT_CONTEXT(ctx
);
555 GLboolean resident
= GL_TRUE
;
558 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
559 "glAreTexturesResident",
562 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)" );
567 struct gl_texture_object
*t
;
569 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
572 t
= (struct gl_texture_object
*)
573 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
575 if (ctx
->Driver
.IsTextureResident
)
576 residences
[i
] = ctx
->Driver
.IsTextureResident( ctx
, t
);
578 residences
[i
] = GL_TRUE
;
581 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
591 * Execute glIsTexture
594 _mesa_IsTexture( GLuint texture
)
596 GET_CURRENT_CONTEXT(ctx
);
597 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
599 if (texture
>0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {