3 * Texture object management.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include "mfeatures.h"
43 #include "shader/prog_instruction.h"
47 /**********************************************************************/
48 /** \name Internal functions */
53 * Return the gl_texture_object for a given ID.
55 struct gl_texture_object
*
56 _mesa_lookup_texture(GLcontext
*ctx
, GLuint id
)
58 return (struct gl_texture_object
*)
59 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
65 * Allocate and initialize a new texture object. But don't put it into the
66 * texture object hash table.
68 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * \param shared the shared GL state structure to contain the texture object
72 * \param name integer name for the texture object
73 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
74 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * \return pointer to new texture object.
79 struct gl_texture_object
*
80 _mesa_new_texture_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
82 struct gl_texture_object
*obj
;
84 obj
= MALLOC_STRUCT(gl_texture_object
);
85 _mesa_initialize_texture_object(obj
, name
, target
);
91 * Initialize a new texture object to default values.
92 * \param obj the texture object
93 * \param name the texture name
94 * \param target the texture target
97 _mesa_initialize_texture_object( struct gl_texture_object
*obj
,
98 GLuint name
, GLenum target
)
100 ASSERT(target
== 0 ||
101 target
== GL_TEXTURE_1D
||
102 target
== GL_TEXTURE_2D
||
103 target
== GL_TEXTURE_3D
||
104 target
== GL_TEXTURE_CUBE_MAP_ARB
||
105 target
== GL_TEXTURE_RECTANGLE_NV
||
106 target
== GL_TEXTURE_1D_ARRAY_EXT
||
107 target
== GL_TEXTURE_2D_ARRAY_EXT
);
109 _mesa_bzero(obj
, sizeof(*obj
));
110 /* init the non-zero fields */
111 _glthread_INIT_MUTEX(obj
->Mutex
);
114 obj
->Target
= target
;
115 obj
->Priority
= 1.0F
;
116 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
117 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
118 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
119 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
120 obj
->MinFilter
= GL_LINEAR
;
123 obj
->WrapS
= GL_REPEAT
;
124 obj
->WrapT
= GL_REPEAT
;
125 obj
->WrapR
= GL_REPEAT
;
126 obj
->MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
128 obj
->MagFilter
= GL_LINEAR
;
129 obj
->MinLod
= -1000.0;
130 obj
->MaxLod
= 1000.0;
133 obj
->MaxLevel
= 1000;
134 obj
->MaxAnisotropy
= 1.0;
135 obj
->CompareMode
= GL_NONE
; /* ARB_shadow */
136 obj
->CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
137 obj
->CompareFailValue
= 0.0F
; /* ARB_shadow_ambient */
138 obj
->DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
139 obj
->Swizzle
[0] = GL_RED
;
140 obj
->Swizzle
[1] = GL_GREEN
;
141 obj
->Swizzle
[2] = GL_BLUE
;
142 obj
->Swizzle
[3] = GL_ALPHA
;
143 obj
->_Swizzle
= SWIZZLE_NOOP
;
148 * Some texture initialization can't be finished until we know which
149 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
152 finish_texture_init(GLcontext
*ctx
, GLenum target
,
153 struct gl_texture_object
*obj
)
155 assert(obj
->Target
== 0);
157 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
158 /* have to init wrap and filter state here - kind of klunky */
159 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
160 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
161 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
162 obj
->MinFilter
= GL_LINEAR
;
163 if (ctx
->Driver
.TexParameter
) {
164 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
165 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
166 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
167 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
168 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
169 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
176 * Deallocate a texture object struct. It should have already been
177 * removed from the texture object pool.
178 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
180 * \param shared the shared GL state to which the object belongs.
181 * \param texObj the texture object to delete.
184 _mesa_delete_texture_object( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
190 /* Set Target to an invalid value. With some assertions elsewhere
191 * we can try to detect possible use of deleted textures.
193 texObj
->Target
= 0x99;
195 _mesa_free_colortable_data(&texObj
->Palette
);
197 /* free the texture images */
198 for (face
= 0; face
< 6; face
++) {
199 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
200 if (texObj
->Image
[face
][i
]) {
201 _mesa_delete_texture_image( ctx
, texObj
->Image
[face
][i
] );
206 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
207 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
209 /* free this object */
217 * Copy texture object state from one texture object to another.
218 * Use for glPush/PopAttrib.
220 * \param dest destination texture object.
221 * \param src source texture object.
224 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
225 const struct gl_texture_object
*src
)
227 dest
->Target
= src
->Target
;
228 dest
->Name
= src
->Name
;
229 dest
->Priority
= src
->Priority
;
230 dest
->BorderColor
[0] = src
->BorderColor
[0];
231 dest
->BorderColor
[1] = src
->BorderColor
[1];
232 dest
->BorderColor
[2] = src
->BorderColor
[2];
233 dest
->BorderColor
[3] = src
->BorderColor
[3];
234 dest
->WrapS
= src
->WrapS
;
235 dest
->WrapT
= src
->WrapT
;
236 dest
->WrapR
= src
->WrapR
;
237 dest
->MinFilter
= src
->MinFilter
;
238 dest
->MagFilter
= src
->MagFilter
;
239 dest
->MinLod
= src
->MinLod
;
240 dest
->MaxLod
= src
->MaxLod
;
241 dest
->LodBias
= src
->LodBias
;
242 dest
->BaseLevel
= src
->BaseLevel
;
243 dest
->MaxLevel
= src
->MaxLevel
;
244 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
245 dest
->CompareMode
= src
->CompareMode
;
246 dest
->CompareFunc
= src
->CompareFunc
;
247 dest
->CompareFailValue
= src
->CompareFailValue
;
248 dest
->DepthMode
= src
->DepthMode
;
249 dest
->_MaxLevel
= src
->_MaxLevel
;
250 dest
->_MaxLambda
= src
->_MaxLambda
;
251 dest
->GenerateMipmap
= src
->GenerateMipmap
;
252 dest
->Palette
= src
->Palette
;
253 dest
->_Complete
= src
->_Complete
;
254 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
255 dest
->_Swizzle
= src
->_Swizzle
;
260 * Clear all texture images of the given texture object.
262 * \param ctx GL context.
263 * \param t texture object.
265 * \sa _mesa_clear_texture_image().
268 _mesa_clear_texture_object(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
272 if (texObj
->Target
== 0)
275 for (i
= 0; i
< MAX_FACES
; i
++) {
276 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
277 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
279 _mesa_clear_texture_image(ctx
, texImage
);
286 * Check if the given texture object is valid by examining its Target field.
287 * For debugging only.
290 valid_texture_object(const struct gl_texture_object
*tex
)
292 switch (tex
->Target
) {
297 case GL_TEXTURE_CUBE_MAP_ARB
:
298 case GL_TEXTURE_RECTANGLE_NV
:
299 case GL_TEXTURE_1D_ARRAY_EXT
:
300 case GL_TEXTURE_2D_ARRAY_EXT
:
303 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
306 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
307 tex
->Target
, tex
->Name
);
314 * Reference (or unreference) a texture object.
315 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
316 * If 'tex' is non-null, increment its refcount.
319 _mesa_reference_texobj(struct gl_texture_object
**ptr
,
320 struct gl_texture_object
*tex
)
329 /* Unreference the old texture */
330 GLboolean deleteFlag
= GL_FALSE
;
331 struct gl_texture_object
*oldTex
= *ptr
;
333 ASSERT(valid_texture_object(oldTex
));
335 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
336 ASSERT(oldTex
->RefCount
> 0);
339 deleteFlag
= (oldTex
->RefCount
== 0);
340 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
343 GET_CURRENT_CONTEXT(ctx
);
345 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
347 _mesa_problem(NULL
, "Unable to delete texture, no context");
355 /* reference new texture */
356 ASSERT(valid_texture_object(tex
));
357 _glthread_LOCK_MUTEX(tex
->Mutex
);
358 if (tex
->RefCount
== 0) {
359 /* this texture's being deleted (look just above) */
360 /* Not sure this can every really happen. Warn if it does. */
361 _mesa_problem(NULL
, "referencing deleted texture object");
368 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
375 * Report why a texture object is incomplete.
377 * \param t texture object.
378 * \param why string describing why it's incomplete.
380 * \note For debug purposes only.
384 incomplete(const struct gl_texture_object
*t
, const char *why
)
386 _mesa_printf("Texture Obj %d incomplete because: %s\n", t
->Name
, why
);
389 #define incomplete(t, why)
394 * Examine a texture object to determine if it is complete.
396 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
399 * \param ctx GL context.
400 * \param t texture object.
402 * According to the texture target, verifies that each of the mipmaps is
403 * present and has the expected size.
406 _mesa_test_texobj_completeness( const GLcontext
*ctx
,
407 struct gl_texture_object
*t
)
409 const GLint baseLevel
= t
->BaseLevel
;
410 GLint maxLog2
= 0, maxLevels
= 0;
412 t
->_Complete
= GL_TRUE
; /* be optimistic */
414 /* Detect cases where the application set the base level to an invalid
417 if ((baseLevel
< 0) || (baseLevel
> MAX_TEXTURE_LEVELS
)) {
419 _mesa_sprintf(s
, "base level = %d is invalid", baseLevel
);
421 t
->_Complete
= GL_FALSE
;
425 /* Always need the base level image */
426 if (!t
->Image
[0][baseLevel
]) {
428 _mesa_sprintf(s
, "Image[baseLevel=%d] == NULL", baseLevel
);
430 t
->_Complete
= GL_FALSE
;
434 /* Check width/height/depth for zero */
435 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
436 t
->Image
[0][baseLevel
]->Height
== 0 ||
437 t
->Image
[0][baseLevel
]->Depth
== 0) {
438 incomplete(t
, "texture width = 0");
439 t
->_Complete
= GL_FALSE
;
443 /* Compute _MaxLevel */
444 if ((t
->Target
== GL_TEXTURE_1D
) ||
445 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
446 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
447 maxLevels
= ctx
->Const
.MaxTextureLevels
;
449 else if ((t
->Target
== GL_TEXTURE_2D
) ||
450 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
451 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
452 t
->Image
[0][baseLevel
]->HeightLog2
);
453 maxLevels
= ctx
->Const
.MaxTextureLevels
;
455 else if (t
->Target
== GL_TEXTURE_3D
) {
456 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
457 t
->Image
[0][baseLevel
]->HeightLog2
);
458 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
459 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
461 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
462 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
463 t
->Image
[0][baseLevel
]->HeightLog2
);
464 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
466 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
467 maxLog2
= 0; /* not applicable */
468 maxLevels
= 1; /* no mipmapping */
471 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
475 ASSERT(maxLevels
> 0);
477 t
->_MaxLevel
= baseLevel
+ maxLog2
;
478 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
479 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
481 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
482 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
484 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
485 /* make sure that all six cube map level 0 images are the same size */
486 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
487 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
489 for (face
= 1; face
< 6; face
++) {
490 if (t
->Image
[face
][baseLevel
] == NULL
||
491 t
->Image
[face
][baseLevel
]->Width2
!= w
||
492 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
493 t
->_Complete
= GL_FALSE
;
494 incomplete(t
, "Cube face missing or mismatched size");
500 /* extra checking for mipmaps */
501 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
503 * Mipmapping: determine if we have a complete set of mipmaps
506 GLint minLevel
= baseLevel
;
507 GLint maxLevel
= t
->_MaxLevel
;
509 if (minLevel
> maxLevel
) {
510 t
->_Complete
= GL_FALSE
;
511 incomplete(t
, "minLevel > maxLevel");
515 /* Test dimension-independent attributes */
516 for (i
= minLevel
; i
<= maxLevel
; i
++) {
517 if (t
->Image
[0][i
]) {
518 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
519 t
->_Complete
= GL_FALSE
;
520 incomplete(t
, "Format[i] != Format[baseLevel]");
523 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
524 t
->_Complete
= GL_FALSE
;
525 incomplete(t
, "Border[i] != Border[baseLevel]");
531 /* Test things which depend on number of texture image dimensions */
532 if ((t
->Target
== GL_TEXTURE_1D
) ||
533 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
534 /* Test 1-D mipmaps */
535 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
536 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
540 if (i
>= minLevel
&& i
<= maxLevel
) {
541 if (!t
->Image
[0][i
]) {
542 t
->_Complete
= GL_FALSE
;
543 incomplete(t
, "1D Image[0][i] == NULL");
546 if (t
->Image
[0][i
]->Width2
!= width
) {
547 t
->_Complete
= GL_FALSE
;
548 incomplete(t
, "1D Image[0][i] bad width");
553 return; /* found smallest needed mipmap, all done! */
557 else if ((t
->Target
== GL_TEXTURE_2D
) ||
558 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
559 /* Test 2-D mipmaps */
560 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
561 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
562 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
569 if (i
>= minLevel
&& i
<= maxLevel
) {
570 if (!t
->Image
[0][i
]) {
571 t
->_Complete
= GL_FALSE
;
572 incomplete(t
, "2D Image[0][i] == NULL");
575 if (t
->Image
[0][i
]->Width2
!= width
) {
576 t
->_Complete
= GL_FALSE
;
577 incomplete(t
, "2D Image[0][i] bad width");
580 if (t
->Image
[0][i
]->Height2
!= height
) {
581 t
->_Complete
= GL_FALSE
;
582 incomplete(t
, "2D Image[0][i] bad height");
585 if (width
==1 && height
==1) {
586 return; /* found smallest needed mipmap, all done! */
591 else if (t
->Target
== GL_TEXTURE_3D
) {
592 /* Test 3-D mipmaps */
593 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
594 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
595 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
596 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
606 if (i
>= minLevel
&& i
<= maxLevel
) {
607 if (!t
->Image
[0][i
]) {
608 incomplete(t
, "3D Image[0][i] == NULL");
609 t
->_Complete
= GL_FALSE
;
612 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
613 t
->_Complete
= GL_FALSE
;
614 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
617 if (t
->Image
[0][i
]->Width2
!= width
) {
618 t
->_Complete
= GL_FALSE
;
619 incomplete(t
, "3D Image[0][i] bad width");
622 if (t
->Image
[0][i
]->Height2
!= height
) {
623 t
->_Complete
= GL_FALSE
;
624 incomplete(t
, "3D Image[0][i] bad height");
627 if (t
->Image
[0][i
]->Depth2
!= depth
) {
628 t
->_Complete
= GL_FALSE
;
629 incomplete(t
, "3D Image[0][i] bad depth");
633 if (width
== 1 && height
== 1 && depth
== 1) {
634 return; /* found smallest needed mipmap, all done! */
638 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
639 /* make sure 6 cube faces are consistant */
640 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
641 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
642 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
649 if (i
>= minLevel
&& i
<= maxLevel
) {
651 for (face
= 0; face
< 6; face
++) {
652 /* check that we have images defined */
653 if (!t
->Image
[face
][i
]) {
654 t
->_Complete
= GL_FALSE
;
655 incomplete(t
, "CubeMap Image[n][i] == NULL");
658 /* Don't support GL_DEPTH_COMPONENT for cube maps */
659 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
660 t
->_Complete
= GL_FALSE
;
661 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
664 /* check that all six images have same size */
665 if (t
->Image
[face
][i
]->Width2
!=width
||
666 t
->Image
[face
][i
]->Height2
!=height
) {
667 t
->_Complete
= GL_FALSE
;
668 incomplete(t
, "CubeMap Image[n][i] bad size");
673 if (width
== 1 && height
== 1) {
674 return; /* found smallest needed mipmap, all done! */
678 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
679 /* XXX special checking? */
683 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
690 * Mark a texture object dirty. It forces the object to be incomplete
691 * and optionally forces the context to re-validate its state.
693 * \param ctx GL context.
694 * \param texObj texture object.
695 * \param invalidate_state also invalidate context state.
698 _mesa_dirty_texobj(GLcontext
*ctx
, struct gl_texture_object
*texObj
,
699 GLboolean invalidate_state
)
701 texObj
->_Complete
= GL_FALSE
;
702 if (invalidate_state
)
703 ctx
->NewState
|= _NEW_TEXTURE
;
708 * Return pointer to a default/fallback texture.
709 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
710 * That's the value a sampler should get when sampling from an
711 * incomplete texture.
713 struct gl_texture_object
*
714 _mesa_get_fallback_texture(GLcontext
*ctx
)
716 if (!ctx
->Shared
->FallbackTex
) {
717 /* create fallback texture now */
718 static GLubyte texels
[8 * 8][4];
719 struct gl_texture_object
*texObj
;
720 struct gl_texture_image
*texImage
;
723 for (i
= 0; i
< 8 * 8; i
++) {
730 /* create texture object */
731 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, GL_TEXTURE_2D
);
732 assert(texObj
->RefCount
== 1);
733 texObj
->MinFilter
= GL_NEAREST
;
734 texObj
->MagFilter
= GL_NEAREST
;
736 /* create level[0] texture image */
737 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, 0);
739 /* init the image fields */
740 _mesa_init_teximage_fields(ctx
, GL_TEXTURE_2D
, texImage
,
741 8, 8, 1, 0, GL_RGBA
);
744 ctx
->Driver
.TexImage2D(ctx
, GL_TEXTURE_2D
, 0, GL_RGBA
,
746 GL_RGBA
, GL_UNSIGNED_BYTE
, texels
,
747 &ctx
->DefaultPacking
, texObj
, texImage
);
749 _mesa_test_texobj_completeness(ctx
, texObj
);
750 assert(texObj
->_Complete
);
752 ctx
->Shared
->FallbackTex
= texObj
;
754 return ctx
->Shared
->FallbackTex
;
761 /***********************************************************************/
762 /** \name API functions */
767 * Generate texture names.
769 * \param n number of texture names to be generated.
770 * \param textures an array in which will hold the generated texture names.
772 * \sa glGenTextures().
774 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
775 * IDs which are stored in \p textures. Corresponding empty texture
776 * objects are also generated.
779 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
781 GET_CURRENT_CONTEXT(ctx
);
784 ASSERT_OUTSIDE_BEGIN_END(ctx
);
787 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
795 * This must be atomic (generation and allocation of texture IDs)
797 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
799 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
801 /* Allocate new, empty texture objects */
802 for (i
= 0; i
< n
; i
++) {
803 struct gl_texture_object
*texObj
;
804 GLuint name
= first
+ i
;
806 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
808 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
809 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
813 /* insert into hash table */
814 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
819 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
824 * Check if the given texture object is bound to the current draw or
825 * read framebuffer. If so, Unbind it.
828 unbind_texobj_from_fbo(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
830 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
833 for (i
= 0; i
< n
; i
++) {
834 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
837 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
838 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
839 fb
->Attachment
[j
].Texture
== texObj
) {
840 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
849 * Check if the given texture object is bound to any texture image units and
850 * unbind it if so (revert to default textures).
853 unbind_texobj_from_texunits(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
857 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
858 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
859 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
860 if (texObj
== unit
->CurrentTex
[tex
]) {
861 _mesa_reference_texobj(&unit
->CurrentTex
[tex
],
862 ctx
->Shared
->DefaultTex
[tex
]);
863 ASSERT(unit
->CurrentTex
[tex
]);
872 * Delete named textures.
874 * \param n number of textures to be deleted.
875 * \param textures array of texture IDs to be deleted.
877 * \sa glDeleteTextures().
879 * If we're about to delete a texture that's currently bound to any
880 * texture unit, unbind the texture first. Decrement the reference
881 * count on the texture object and delete it if it's zero.
882 * Recall that texture objects can be shared among several rendering
886 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
888 GET_CURRENT_CONTEXT(ctx
);
890 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
895 for (i
= 0; i
< n
; i
++) {
896 if (textures
[i
] > 0) {
897 struct gl_texture_object
*delObj
898 = _mesa_lookup_texture(ctx
, textures
[i
]);
901 _mesa_lock_texture(ctx
, delObj
);
903 /* Check if texture is bound to any framebuffer objects.
905 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
907 unbind_texobj_from_fbo(ctx
, delObj
);
909 /* Check if this texture is currently bound to any texture units.
912 unbind_texobj_from_texunits(ctx
, delObj
);
914 _mesa_unlock_texture(ctx
, delObj
);
916 ctx
->NewState
|= _NEW_TEXTURE
;
918 /* The texture _name_ is now free for re-use.
919 * Remove it from the hash table now.
921 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
922 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
923 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
925 /* Unreference the texobj. If refcount hits zero, the texture
928 _mesa_reference_texobj(&delObj
, NULL
);
936 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
937 * into the corresponding Mesa texture target index.
938 * Return -1 if target is invalid.
941 target_enum_to_index(GLenum target
)
945 return TEXTURE_1D_INDEX
;
947 return TEXTURE_2D_INDEX
;
949 return TEXTURE_3D_INDEX
;
950 case GL_TEXTURE_CUBE_MAP_ARB
:
951 return TEXTURE_CUBE_INDEX
;
952 case GL_TEXTURE_RECTANGLE_NV
:
953 return TEXTURE_RECT_INDEX
;
954 case GL_TEXTURE_1D_ARRAY_EXT
:
955 return TEXTURE_1D_ARRAY_INDEX
;
956 case GL_TEXTURE_2D_ARRAY_EXT
:
957 return TEXTURE_2D_ARRAY_INDEX
;
965 * Bind a named texture to a texturing target.
967 * \param target texture target.
968 * \param texName texture name.
970 * \sa glBindTexture().
972 * Determines the old texture object bound and returns immediately if rebinding
973 * the same texture. Get the current texture which is either a default texture
974 * if name is null, a named texture from the hash, or a new texture if the
975 * given texture name is new. Increments its reference count, binds it, and
976 * calls dd_function_table::BindTexture. Decrements the old texture reference
977 * count and deletes it if it reaches zero.
980 _mesa_BindTexture( GLenum target
, GLuint texName
)
982 GET_CURRENT_CONTEXT(ctx
);
983 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
984 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
985 struct gl_texture_object
*newTexObj
= NULL
, *defaultTexObj
= NULL
;
987 GLboolean early_out
= GL_FALSE
;
988 ASSERT_OUTSIDE_BEGIN_END(ctx
);
990 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
991 _mesa_debug(ctx
, "glBindTexture %s %d\n",
992 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
994 targetIndex
= target_enum_to_index(target
);
995 if (targetIndex
< 0) {
996 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
999 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1000 defaultTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1003 * Get pointer to new texture object (newTexObj)
1006 newTexObj
= defaultTexObj
;
1009 /* non-default texture object */
1010 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1012 /* error checking */
1013 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1014 /* the named texture object's target doesn't match the given target */
1015 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1016 "glBindTexture(target mismatch)" );
1019 if (newTexObj
->Target
== 0) {
1020 finish_texture_init(ctx
, target
, newTexObj
);
1024 /* if this is a new texture id, allocate a texture object now */
1025 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
1027 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1031 /* and insert it into hash table */
1032 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1033 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1034 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1036 newTexObj
->Target
= target
;
1039 assert(valid_texture_object(newTexObj
));
1041 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1042 if ((ctx
->Shared
->RefCount
== 1)
1043 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
])) {
1044 early_out
= GL_TRUE
;
1046 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1052 /* flush before changing binding */
1053 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1055 /* Do the actual binding. The refcount on the previously bound
1056 * texture object will be decremented. It'll be deleted if the
1059 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1060 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1062 /* Pass BindTexture call to device driver */
1063 if (ctx
->Driver
.BindTexture
)
1064 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
1069 * Set texture priorities.
1071 * \param n number of textures.
1072 * \param texName texture names.
1073 * \param priorities corresponding texture priorities.
1075 * \sa glPrioritizeTextures().
1077 * Looks up each texture in the hash, clamps the corresponding priority between
1078 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1081 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1082 const GLclampf
*priorities
)
1084 GET_CURRENT_CONTEXT(ctx
);
1086 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1089 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1096 for (i
= 0; i
< n
; i
++) {
1097 if (texName
[i
] > 0) {
1098 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1100 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1105 ctx
->NewState
|= _NEW_TEXTURE
;
1109 * See if textures are loaded in texture memory.
1111 * \param n number of textures to query.
1112 * \param texName array with the texture names.
1113 * \param residences array which will hold the residence status.
1115 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1117 * \sa glAreTexturesResident().
1119 * Looks up each texture in the hash and calls
1120 * dd_function_table::IsTextureResident.
1122 GLboolean GLAPIENTRY
1123 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1124 GLboolean
*residences
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 GLboolean allResident
= GL_TRUE
;
1129 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1132 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1136 if (!texName
|| !residences
)
1139 for (i
= 0; i
< n
; i
++) {
1140 struct gl_texture_object
*t
;
1141 if (texName
[i
] == 0) {
1142 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1145 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1147 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1150 if (!ctx
->Driver
.IsTextureResident
||
1151 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1152 /* The texture is resident */
1154 residences
[i
] = GL_TRUE
;
1157 /* The texture is not resident */
1159 allResident
= GL_FALSE
;
1160 for (j
= 0; j
< i
; j
++)
1161 residences
[j
] = GL_TRUE
;
1163 residences
[i
] = GL_FALSE
;
1171 * See if a name corresponds to a texture.
1173 * \param texture texture name.
1175 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1178 * \sa glIsTexture().
1180 * Calls _mesa_HashLookup().
1182 GLboolean GLAPIENTRY
1183 _mesa_IsTexture( GLuint texture
)
1185 struct gl_texture_object
*t
;
1186 GET_CURRENT_CONTEXT(ctx
);
1187 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1192 t
= _mesa_lookup_texture(ctx
, texture
);
1194 /* IsTexture is true only after object has been bound once. */
1195 return t
&& t
->Target
;
1200 * Simplest implementation of texture locking: grab the shared tex
1201 * mutex. Examine the shared context state timestamp and if there has
1202 * been a change, set the appropriate bits in ctx->NewState.
1204 * This is used to deal with synchronizing things when a texture object
1205 * is used/modified by different contexts (or threads) which are sharing
1208 * See also _mesa_lock/unlock_texture() in teximage.h
1211 _mesa_lock_context_textures( GLcontext
*ctx
)
1213 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1215 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1216 ctx
->NewState
|= _NEW_TEXTURE
;
1217 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1223 _mesa_unlock_context_textures( GLcontext
*ctx
)
1225 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1226 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);