1 /* $Id: texobj.c,v 1.14 2000/02/12 01:59:19 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.
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 or 3
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);
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 obj
->Palette
.Table
[0] = 255;
78 obj
->Palette
.Table
[1] = 255;
79 obj
->Palette
.Table
[2] = 255;
80 obj
->Palette
.Table
[3] = 255;
81 obj
->Palette
.Size
= 1;
82 obj
->Palette
.IntFormat
= GL_RGBA
;
83 obj
->Palette
.Format
= GL_RGBA
;
85 /* insert into linked list */
87 _glthread_LOCK_MUTEX(shared
->Mutex
);
88 obj
->Next
= shared
->TexObjectList
;
89 shared
->TexObjectList
= obj
;
90 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
94 /* insert into hash table */
95 _mesa_HashInsert(shared
->TexObjects
, name
, obj
);
103 * Deallocate a texture object struct and remove it from the given
105 * Input: shared - the shared GL state to which the object belongs
106 * t - the texture object to delete
108 void gl_free_texture_object( struct gl_shared_state
*shared
,
109 struct gl_texture_object
*t
)
111 struct gl_texture_object
*tprev
, *tcurr
;
115 /* Remove t from dirty list so we don't touch free'd memory later.
116 * Test for shared since Proxy texture aren't in global linked list.
119 gl_remove_texobj_from_dirty_list( shared
, t
);
121 /* unlink t from the linked list */
123 _glthread_LOCK_MUTEX(shared
->Mutex
);
125 tcurr
= shared
->TexObjectList
;
129 tprev
->Next
= t
->Next
;
132 shared
->TexObjectList
= t
->Next
;
139 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
143 /* remove from hash table */
144 _mesa_HashRemove(shared
->TexObjects
, t
->Name
);
147 /* free texture image */
150 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
152 gl_free_texture_image( t
->Image
[i
] );
156 /* free this object */
163 * Examine a texture object to determine if it is complete or not.
164 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
166 void gl_test_texture_object_completeness( const GLcontext
*ctx
, struct gl_texture_object
*t
)
168 t
->Complete
= GL_TRUE
; /* be optimistic */
170 /* Always need level zero image */
171 if (!t
->Image
[0] || !t
->Image
[0]->Data
) {
172 t
->Complete
= GL_FALSE
;
176 /* Compute number of mipmap levels */
177 if (t
->Dimensions
==1) {
178 t
->P
= t
->Image
[0]->WidthLog2
;
180 else if (t
->Dimensions
==2) {
181 t
->P
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
183 else if (t
->Dimensions
==3) {
184 GLint max
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
185 max
= MAX2(max
, (GLint
)(t
->Image
[0]->DepthLog2
));
189 /* Compute M (see the 1.2 spec) used during mipmapping */
190 t
->M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->P
) - t
->BaseLevel
);
193 if (t
->MinFilter
!=GL_NEAREST
&& t
->MinFilter
!=GL_LINEAR
) {
195 * Mipmapping: determine if we have a complete set of mipmaps
198 GLint minLevel
= t
->BaseLevel
;
199 GLint maxLevel
= MIN2(t
->P
, ctx
->Const
.MaxTextureLevels
-1);
200 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
202 if (minLevel
> maxLevel
) {
203 t
->Complete
= GL_FALSE
;
207 /* Test dimension-independent attributes */
208 for (i
= minLevel
; i
<= maxLevel
; i
++) {
210 if (!t
->Image
[i
]->Data
) {
211 t
->Complete
= GL_FALSE
;
214 if (t
->Image
[i
]->Format
!= t
->Image
[0]->Format
) {
215 t
->Complete
= GL_FALSE
;
218 if (t
->Image
[i
]->Border
!= t
->Image
[0]->Border
) {
219 t
->Complete
= GL_FALSE
;
225 /* Test things which depend on number of texture image dimensions */
226 if (t
->Dimensions
==1) {
227 /* Test 1-D mipmaps */
228 GLuint width
= t
->Image
[0]->Width2
;
229 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
233 if (i
>= minLevel
&& i
<= maxLevel
) {
235 t
->Complete
= GL_FALSE
;
238 if (!t
->Image
[i
]->Data
) {
239 t
->Complete
= GL_FALSE
;
242 if (t
->Image
[i
]->Width2
!= width
) {
243 t
->Complete
= GL_FALSE
;
248 return; /* found smallest needed mipmap, all done! */
252 else if (t
->Dimensions
==2) {
253 /* Test 2-D mipmaps */
254 GLuint width
= t
->Image
[0]->Width2
;
255 GLuint height
= t
->Image
[0]->Height2
;
256 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
263 if (i
>= minLevel
&& i
<= maxLevel
) {
265 t
->Complete
= GL_FALSE
;
268 if (t
->Image
[i
]->Width2
!= width
) {
269 t
->Complete
= GL_FALSE
;
272 if (t
->Image
[i
]->Height2
!= height
) {
273 t
->Complete
= GL_FALSE
;
276 if (width
==1 && height
==1) {
277 return; /* found smallest needed mipmap, all done! */
282 else if (t
->Dimensions
==3) {
283 /* Test 3-D mipmaps */
284 GLuint width
= t
->Image
[0]->Width2
;
285 GLuint height
= t
->Image
[0]->Height2
;
286 GLuint depth
= t
->Image
[0]->Depth2
;
287 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
297 if (i
>= minLevel
&& i
<= maxLevel
) {
299 t
->Complete
= GL_FALSE
;
302 if (t
->Image
[i
]->Width2
!= width
) {
303 t
->Complete
= GL_FALSE
;
306 if (t
->Image
[i
]->Height2
!= height
) {
307 t
->Complete
= GL_FALSE
;
310 if (t
->Image
[i
]->Depth2
!= depth
) {
311 t
->Complete
= GL_FALSE
;
315 if (width
==1 && height
==1 && depth
==1) {
316 return; /* found smallest needed mipmap, all done! */
321 /* Dimensions = ??? */
322 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
330 * Execute glGenTextures
333 _mesa_GenTextures( GLsizei n
, GLuint
*texName
)
335 GET_CURRENT_CONTEXT(ctx
);
339 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
341 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
345 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
347 /* Return the texture names */
349 texName
[i
] = first
+ i
;
352 /* Allocate new, empty texture objects */
354 GLuint name
= first
+ i
;
356 (void) gl_alloc_texture_object(ctx
->Shared
, name
, dims
);
363 * Execute glDeleteTextures
366 _mesa_DeleteTextures( GLsizei n
, const GLuint
*texName
)
368 GET_CURRENT_CONTEXT(ctx
);
371 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
374 struct gl_texture_object
*t
;
376 t
= (struct gl_texture_object
*)
377 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
379 /* First check if this texture is currently bound.
380 * If so, unbind it and decrement the reference count.
383 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
384 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
386 for (d
= 1 ; d
<= 3 ; d
++) {
387 if (unit
->CurrentD
[d
] == t
) {
388 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
389 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
391 ASSERT( t
->RefCount
>= 0 );
396 /* Decrement reference count and delete if zero */
398 ASSERT( t
->RefCount
>= 0 );
399 if (t
->RefCount
== 0) {
400 if (ctx
->Driver
.DeleteTexture
)
401 (*ctx
->Driver
.DeleteTexture
)( ctx
, t
);
402 gl_free_texture_object(ctx
->Shared
, t
);
412 * Execute glBindTexture
415 _mesa_BindTexture( GLenum target
, GLuint texName
)
417 GET_CURRENT_CONTEXT(ctx
);
418 GLuint unit
= ctx
->Texture
.CurrentUnit
;
419 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
420 struct gl_texture_object
*oldTexObj
;
421 struct gl_texture_object
*newTexObj
;
424 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
425 fprintf(stderr
, "glBindTexture %s %d\n",
426 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
428 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
441 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture(target)" );
445 oldTexObj
= texUnit
->CurrentD
[dim
];
447 if (oldTexObj
->Name
== texName
)
451 newTexObj
= ctx
->Shared
->DefaultD
[dim
];
453 struct _mesa_HashTable
*hash
= ctx
->Shared
->TexObjects
;
454 newTexObj
= (struct gl_texture_object
*) _mesa_HashLookup(hash
, texName
);
457 newTexObj
= gl_alloc_texture_object(ctx
->Shared
, texName
, dim
);
459 if (newTexObj
->Dimensions
!= dim
) {
460 if (newTexObj
->Dimensions
) {
461 /* the named texture object's dimensions don't match the target */
462 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
465 newTexObj
->Dimensions
= dim
;
469 newTexObj
->RefCount
++;
471 texUnit
->CurrentD
[dim
] = newTexObj
;
473 /* If we've changed the CurrentD[123] texture object then update the
474 * ctx->Texture.Current pointer to point to the new texture object.
476 texUnit
->Current
= texUnit
->CurrentD
[texUnit
->CurrentDimension
];
478 /* Check if we may have to use a new triangle rasterizer */
479 if ((ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) &&
480 ( oldTexObj
->WrapS
!= newTexObj
->WrapS
481 || oldTexObj
->WrapT
!= newTexObj
->WrapT
482 || oldTexObj
->WrapR
!= newTexObj
->WrapR
483 || oldTexObj
->MinFilter
!= newTexObj
->MinFilter
484 || oldTexObj
->MagFilter
!= newTexObj
->MagFilter
485 || (oldTexObj
->Image
[0] && newTexObj
->Image
[0] &&
486 (oldTexObj
->Image
[0]->Format
!=newTexObj
->Image
[0]->Format
))))
488 ctx
->NewState
|= (NEW_RASTER_OPS
| NEW_TEXTURING
);
491 if (oldTexObj
->Complete
!= newTexObj
->Complete
)
492 ctx
->NewState
|= NEW_TEXTURING
;
494 /* Pass BindTexture call to device driver */
495 if (ctx
->Driver
.BindTexture
) {
496 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
499 if (oldTexObj
->Name
> 0) {
500 /* never delete default (id=0) texture objects */
501 oldTexObj
->RefCount
--;
502 if (oldTexObj
->RefCount
<= 0) {
503 if (ctx
->Driver
.DeleteTexture
) {
504 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
506 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
514 * Execute glPrioritizeTextures
517 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
518 const GLclampf
*priorities
)
520 GET_CURRENT_CONTEXT(ctx
);
523 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
525 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
530 struct gl_texture_object
*t
;
532 t
= (struct gl_texture_object
*)
533 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
535 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
537 if (ctx
->Driver
.PrioritizeTexture
)
538 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
547 * Execute glAreTexturesResident
550 _mesa_AreTexturesResident( GLsizei n
, const GLuint
*texName
,
551 GLboolean
*residences
)
553 GET_CURRENT_CONTEXT(ctx
);
554 GLboolean resident
= GL_TRUE
;
557 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
558 "glAreTexturesResident",
561 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)" );
566 struct gl_texture_object
*t
;
568 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
571 t
= (struct gl_texture_object
*)
572 _mesa_HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
574 if (ctx
->Driver
.IsTextureResident
)
575 residences
[i
] = ctx
->Driver
.IsTextureResident( ctx
, t
);
577 residences
[i
] = GL_TRUE
;
580 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
590 * Execute glIsTexture
593 _mesa_IsTexture( GLuint texture
)
595 GET_CURRENT_CONTEXT(ctx
);
596 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
598 if (texture
>0 && _mesa_HashLookup(ctx
->Shared
->TexObjects
, texture
)) {