1 /* $Id: texobj.c,v 1.4 1999/10/09 20:17:07 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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.
26 /* $XFree86: xc/lib/GL/mesa/src/texobj.c,v 1.3 1999/04/04 00:20:32 dawes Exp $ */
39 #include "GL/xf86glx.h"
54 * Allocate a new texture object and add it to the linked list of texture
55 * objects. If name>0 then also insert the new texture object into the hash
57 * Input: shared - the shared GL state structure to contain the texture object
58 * name - integer name for the texture object
59 * dimensions - either 1, 2 or 3
60 * Return: pointer to new texture object
62 struct gl_texture_object
*
63 gl_alloc_texture_object( struct gl_shared_state
*shared
, GLuint name
,
66 struct gl_texture_object
*obj
;
68 assert(dimensions
<= 3);
70 obj
= (struct gl_texture_object
*)
71 calloc(1,sizeof(struct gl_texture_object
));
73 /* init the non-zero fields */
76 obj
->Dimensions
= dimensions
;
77 obj
->WrapS
= GL_REPEAT
;
78 obj
->WrapT
= GL_REPEAT
;
79 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
80 obj
->MagFilter
= GL_LINEAR
;
81 obj
->MinLod
= -1000.0;
85 obj
->MinMagThresh
= 0.0F
;
86 obj
->Palette
[0] = 255;
87 obj
->Palette
[1] = 255;
88 obj
->Palette
[2] = 255;
89 obj
->Palette
[3] = 255;
91 obj
->PaletteIntFormat
= GL_RGBA
;
92 obj
->PaletteFormat
= GL_RGBA
;
94 /* insert into linked list */
96 obj
->Next
= shared
->TexObjectList
;
97 shared
->TexObjectList
= obj
;
101 /* insert into hash table */
102 HashInsert(shared
->TexObjects
, name
, obj
);
110 * Deallocate a texture object struct and remove it from the given
112 * Input: shared - the shared GL state to which the object belongs
113 * t - the texture object to delete
115 void gl_free_texture_object( struct gl_shared_state
*shared
,
116 struct gl_texture_object
*t
)
118 struct gl_texture_object
*tprev
, *tcurr
;
122 /* Remove t from dirty list so we don't touch free'd memory later.
123 * Test for shared since Proxy texture aren't in global linked list.
126 gl_remove_texobj_from_dirty_list( shared
, t
);
128 /* unlink t from the linked list */
131 tcurr
= shared
->TexObjectList
;
135 tprev
->Next
= t
->Next
;
138 shared
->TexObjectList
= t
->Next
;
148 /* remove from hash table */
149 HashRemove(shared
->TexObjects
, t
->Name
);
152 /* free texture image */
155 for (i
=0;i
<MAX_TEXTURE_LEVELS
;i
++) {
157 gl_free_texture_image( t
->Image
[i
] );
161 /* free this object */
168 * Examine a texture object to determine if it is complete or not.
169 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
171 void gl_test_texture_object_completeness( const GLcontext
*ctx
, struct gl_texture_object
*t
)
173 t
->Complete
= GL_TRUE
; /* be optimistic */
175 /* Always need level zero image */
176 if (!t
->Image
[0] || !t
->Image
[0]->Data
) {
177 t
->Complete
= GL_FALSE
;
181 /* Compute number of mipmap levels */
182 if (t
->Dimensions
==1) {
183 t
->P
= t
->Image
[0]->WidthLog2
;
185 else if (t
->Dimensions
==2) {
186 t
->P
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
188 else if (t
->Dimensions
==3) {
189 GLint max
= MAX2(t
->Image
[0]->WidthLog2
, t
->Image
[0]->HeightLog2
);
190 max
= MAX2(max
, (GLint
)(t
->Image
[0]->DepthLog2
));
194 /* Compute M (see the 1.2 spec) used during mipmapping */
195 t
->M
= (GLfloat
) (MIN2(t
->MaxLevel
, t
->P
) - t
->BaseLevel
);
198 if (t
->MinFilter
!=GL_NEAREST
&& t
->MinFilter
!=GL_LINEAR
) {
200 * Mipmapping: determine if we have a complete set of mipmaps
203 GLint minLevel
= t
->BaseLevel
;
204 GLint maxLevel
= MIN2(t
->P
, ctx
->Const
.MaxTextureLevels
-1);
205 maxLevel
= MIN2(maxLevel
, t
->MaxLevel
);
207 if (minLevel
> maxLevel
) {
208 t
->Complete
= GL_FALSE
;
212 /* Test dimension-independent attributes */
213 for (i
= minLevel
; i
<= maxLevel
; i
++) {
215 if (!t
->Image
[i
]->Data
) {
216 t
->Complete
= GL_FALSE
;
219 if (t
->Image
[i
]->Format
!= t
->Image
[0]->Format
) {
220 t
->Complete
= GL_FALSE
;
223 if (t
->Image
[i
]->Border
!= t
->Image
[0]->Border
) {
224 t
->Complete
= GL_FALSE
;
230 /* Test things which depend on number of texture image dimensions */
231 if (t
->Dimensions
==1) {
232 /* Test 1-D mipmaps */
233 GLuint width
= t
->Image
[0]->Width2
;
234 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
238 if (i
>= minLevel
&& i
<= maxLevel
) {
240 t
->Complete
= GL_FALSE
;
243 if (!t
->Image
[i
]->Data
) {
244 t
->Complete
= GL_FALSE
;
247 if (t
->Image
[i
]->Width2
!= width
) {
248 t
->Complete
= GL_FALSE
;
253 return; /* found smallest needed mipmap, all done! */
257 else if (t
->Dimensions
==2) {
258 /* Test 2-D mipmaps */
259 GLuint width
= t
->Image
[0]->Width2
;
260 GLuint height
= t
->Image
[0]->Height2
;
261 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
268 if (i
>= minLevel
&& i
<= maxLevel
) {
270 t
->Complete
= GL_FALSE
;
273 if (t
->Image
[i
]->Width2
!= width
) {
274 t
->Complete
= GL_FALSE
;
277 if (t
->Image
[i
]->Height2
!= height
) {
278 t
->Complete
= GL_FALSE
;
281 if (width
==1 && height
==1) {
282 return; /* found smallest needed mipmap, all done! */
287 else if (t
->Dimensions
==3) {
288 /* Test 3-D mipmaps */
289 GLuint width
= t
->Image
[0]->Width2
;
290 GLuint height
= t
->Image
[0]->Height2
;
291 GLuint depth
= t
->Image
[0]->Depth2
;
292 for (i
=1; i
<ctx
->Const
.MaxTextureLevels
; i
++) {
302 if (i
>= minLevel
&& i
<= maxLevel
) {
304 t
->Complete
= GL_FALSE
;
307 if (t
->Image
[i
]->Width2
!= width
) {
308 t
->Complete
= GL_FALSE
;
311 if (t
->Image
[i
]->Height2
!= height
) {
312 t
->Complete
= GL_FALSE
;
315 if (t
->Image
[i
]->Depth2
!= depth
) {
316 t
->Complete
= GL_FALSE
;
320 if (width
==1 && height
==1 && depth
==1) {
321 return; /* found smallest needed mipmap, all done! */
326 /* Dimensions = ??? */
327 gl_problem(NULL
, "Bug in gl_test_texture_object_completeness\n");
335 * Execute glGenTextures
337 void gl_GenTextures( GLcontext
*ctx
, GLsizei n
, GLuint
*texName
)
342 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGenTextures");
344 gl_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
348 first
= HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
350 /* Return the texture names */
352 texName
[i
] = first
+ i
;
355 /* Allocate new, empty texture objects */
357 GLuint name
= first
+ i
;
359 (void) gl_alloc_texture_object(ctx
->Shared
, name
, dims
);
366 * Execute glDeleteTextures
368 void gl_DeleteTextures( GLcontext
*ctx
, GLsizei n
, const GLuint
*texName
)
372 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDeleteTextures");
375 struct gl_texture_object
*t
;
377 t
= (struct gl_texture_object
*)
378 HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
381 for (u
=0; u
<MAX_TEXTURE_UNITS
; u
++) {
382 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
384 for (d
= 1 ; d
<= 3 ; d
++) {
385 if (unit
->CurrentD
[d
]==t
) {
386 unit
->CurrentD
[d
] = ctx
->Shared
->DefaultD
[d
];
387 ctx
->Shared
->DefaultD
[d
]->RefCount
++;
389 assert( t
->RefCount
>= 0 );
394 /* tell device driver to delete texture */
395 if (ctx
->Driver
.DeleteTexture
) {
396 (*ctx
->Driver
.DeleteTexture
)( ctx
, t
);
399 if (t
->RefCount
==0) {
400 gl_free_texture_object(ctx
->Shared
, t
);
410 * Execute glBindTexture
412 void gl_BindTexture( GLcontext
*ctx
, GLenum target
, GLuint texName
)
414 GLuint unit
= ctx
->Texture
.CurrentUnit
;
415 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
416 struct gl_texture_object
*oldTexObj
;
417 struct gl_texture_object
*newTexObj
;
420 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
421 fprintf(stderr
, "glBindTexture %s %d\n",
422 gl_lookup_enum_by_nr(target
), (GLint
) texName
);
424 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBindTexture");
426 dim
= target
- GL_TEXTURE_1D
;
428 if (dim
< 0 || dim
> 2) {
429 gl_error( ctx
, GL_INVALID_ENUM
, "glBindTexture" );
434 oldTexObj
= texUnit
->CurrentD
[dim
];
436 if (oldTexObj
->Name
== texName
)
440 newTexObj
= ctx
->Shared
->DefaultD
[dim
];
442 struct HashTable
*hash
= ctx
->Shared
->TexObjects
;
443 newTexObj
= (struct gl_texture_object
*) HashLookup(hash
, texName
);
446 newTexObj
= gl_alloc_texture_object(ctx
->Shared
, texName
, dim
);
448 if (newTexObj
->Dimensions
!= dim
) {
449 if (newTexObj
->Dimensions
) {
450 gl_error( ctx
, GL_INVALID_OPERATION
, "glBindTexture" );
453 newTexObj
->Dimensions
= dim
;
457 newTexObj
->RefCount
++;
459 texUnit
->CurrentD
[dim
] = newTexObj
;
461 /* If we've changed the CurrentD[123] texture object then update the
462 * ctx->Texture.Current pointer to point to the new texture object.
464 texUnit
->Current
= texUnit
->CurrentD
[texUnit
->CurrentDimension
];
466 /* Check if we may have to use a new triangle rasterizer */
467 if ((ctx
->IndirectTriangles
& DD_SW_RASTERIZE
) &&
468 ( oldTexObj
->WrapS
!= newTexObj
->WrapS
469 || oldTexObj
->WrapT
!= newTexObj
->WrapT
470 || oldTexObj
->WrapR
!= newTexObj
->WrapR
471 || oldTexObj
->MinFilter
!= newTexObj
->MinFilter
472 || oldTexObj
->MagFilter
!= newTexObj
->MagFilter
473 || (oldTexObj
->Image
[0] && newTexObj
->Image
[0] &&
474 (oldTexObj
->Image
[0]->Format
!=newTexObj
->Image
[0]->Format
))))
476 ctx
->NewState
|= (NEW_RASTER_OPS
| NEW_TEXTURING
);
479 if (oldTexObj
->Complete
!= newTexObj
->Complete
)
480 ctx
->NewState
|= NEW_TEXTURING
;
482 /* Pass BindTexture call to device driver */
483 if (ctx
->Driver
.BindTexture
) {
484 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
487 if (oldTexObj
->Name
> 0) {
488 /* never delete default (id=0) texture objects */
489 oldTexObj
->RefCount
--;
490 if (oldTexObj
->RefCount
<= 0) {
491 if (ctx
->Driver
.DeleteTexture
) {
492 (*ctx
->Driver
.DeleteTexture
)( ctx
, oldTexObj
);
494 gl_free_texture_object(ctx
->Shared
, oldTexObj
);
502 * Execute glPrioritizeTextures
504 void gl_PrioritizeTextures( GLcontext
*ctx
,
505 GLsizei n
, const GLuint
*texName
,
506 const GLclampf
*priorities
)
510 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPrioritizeTextures");
512 gl_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
517 struct gl_texture_object
*t
;
519 t
= (struct gl_texture_object
*)
520 HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
522 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
524 if (ctx
->Driver
.PrioritizeTexture
)
525 ctx
->Driver
.PrioritizeTexture( ctx
, t
, t
->Priority
);
534 * Execute glAreTexturesResident
536 GLboolean
gl_AreTexturesResident( GLcontext
*ctx
, GLsizei n
,
537 const GLuint
*texName
,
538 GLboolean
*residences
)
540 GLboolean resident
= GL_TRUE
;
543 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
,
544 "glAreTexturesResident",
547 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)" );
552 struct gl_texture_object
*t
;
554 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
557 t
= (struct gl_texture_object
*)
558 HashLookup(ctx
->Shared
->TexObjects
, texName
[i
]);
560 if (ctx
->Driver
.IsTextureResident
)
561 residences
[i
] = ctx
->Driver
.IsTextureResident( ctx
, t
);
563 residences
[i
] = GL_TRUE
;
566 gl_error( ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(textures)" );
576 * Execute glIsTexture
578 GLboolean
gl_IsTexture( GLcontext
*ctx
, GLuint texture
)
580 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glIsTextures",
582 if (texture
>0 && HashLookup(ctx
->Shared
->TexObjects
, texture
)) {