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 "program/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(struct gl_context
*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( struct gl_context
*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 memset(obj
, 0, 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
;
144 obj
->sRGBDecode
= GL_DECODE_EXT
;
149 * Some texture initialization can't be finished until we know which
150 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
153 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
154 struct gl_texture_object
*obj
)
156 assert(obj
->Target
== 0);
158 if (target
== GL_TEXTURE_RECTANGLE_NV
) {
159 /* have to init wrap and filter state here - kind of klunky */
160 obj
->WrapS
= GL_CLAMP_TO_EDGE
;
161 obj
->WrapT
= GL_CLAMP_TO_EDGE
;
162 obj
->WrapR
= GL_CLAMP_TO_EDGE
;
163 obj
->MinFilter
= GL_LINEAR
;
164 if (ctx
->Driver
.TexParameter
) {
165 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
166 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
167 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
168 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
169 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
170 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
177 * Deallocate a texture object struct. It should have already been
178 * removed from the texture object pool.
179 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
181 * \param shared the shared GL state to which the object belongs.
182 * \param texObj the texture object to delete.
185 _mesa_delete_texture_object(struct gl_context
*ctx
,
186 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 */
216 * Copy texture object state from one texture object to another.
217 * Use for glPush/PopAttrib.
219 * \param dest destination texture object.
220 * \param src source texture object.
223 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
224 const struct gl_texture_object
*src
)
226 dest
->Target
= src
->Target
;
227 dest
->Name
= src
->Name
;
228 dest
->Priority
= src
->Priority
;
229 dest
->BorderColor
.f
[0] = src
->BorderColor
.f
[0];
230 dest
->BorderColor
.f
[1] = src
->BorderColor
.f
[1];
231 dest
->BorderColor
.f
[2] = src
->BorderColor
.f
[2];
232 dest
->BorderColor
.f
[3] = src
->BorderColor
.f
[3];
233 dest
->WrapS
= src
->WrapS
;
234 dest
->WrapT
= src
->WrapT
;
235 dest
->WrapR
= src
->WrapR
;
236 dest
->MinFilter
= src
->MinFilter
;
237 dest
->MagFilter
= src
->MagFilter
;
238 dest
->MinLod
= src
->MinLod
;
239 dest
->MaxLod
= src
->MaxLod
;
240 dest
->LodBias
= src
->LodBias
;
241 dest
->BaseLevel
= src
->BaseLevel
;
242 dest
->MaxLevel
= src
->MaxLevel
;
243 dest
->MaxAnisotropy
= src
->MaxAnisotropy
;
244 dest
->CompareMode
= src
->CompareMode
;
245 dest
->CompareFunc
= src
->CompareFunc
;
246 dest
->CompareFailValue
= src
->CompareFailValue
;
247 dest
->DepthMode
= src
->DepthMode
;
248 dest
->_MaxLevel
= src
->_MaxLevel
;
249 dest
->_MaxLambda
= src
->_MaxLambda
;
250 dest
->GenerateMipmap
= src
->GenerateMipmap
;
251 dest
->Palette
= src
->Palette
;
252 dest
->_Complete
= src
->_Complete
;
253 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
254 dest
->_Swizzle
= src
->_Swizzle
;
259 * Free all texture images of the given texture object.
261 * \param ctx GL context.
262 * \param t texture object.
264 * \sa _mesa_clear_texture_image().
267 _mesa_clear_texture_object(struct gl_context
*ctx
,
268 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
));
334 (void) valid_texture_object
; /* silence warning in release builds */
336 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
337 ASSERT(oldTex
->RefCount
> 0);
340 deleteFlag
= (oldTex
->RefCount
== 0);
341 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
344 GET_CURRENT_CONTEXT(ctx
);
346 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
348 _mesa_problem(NULL
, "Unable to delete texture, no context");
356 /* reference new texture */
357 ASSERT(valid_texture_object(tex
));
358 _glthread_LOCK_MUTEX(tex
->Mutex
);
359 if (tex
->RefCount
== 0) {
360 /* this texture's being deleted (look just above) */
361 /* Not sure this can every really happen. Warn if it does. */
362 _mesa_problem(NULL
, "referencing deleted texture object");
369 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
376 * Mark a texture object as incomplete.
377 * \param t texture object
378 * \param fmt... string describing why it's incomplete (for debugging).
381 incomplete(struct gl_texture_object
*t
, const char *fmt
, ...)
388 vsnprintf(s
, sizeof(s
), fmt
, args
);
391 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
393 t
->_Complete
= GL_FALSE
;
398 * Examine a texture object to determine if it is complete.
400 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
403 * \param ctx GL context.
404 * \param t texture object.
406 * According to the texture target, verifies that each of the mipmaps is
407 * present and has the expected size.
410 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
411 struct gl_texture_object
*t
)
413 const GLint baseLevel
= t
->BaseLevel
;
414 GLint maxLog2
= 0, maxLevels
= 0;
416 t
->_Complete
= GL_TRUE
; /* be optimistic */
418 /* Detect cases where the application set the base level to an invalid
421 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
422 incomplete(t
, "base level = %d is invalid", baseLevel
);
426 /* Always need the base level image */
427 if (!t
->Image
[0][baseLevel
]) {
428 incomplete(t
, "Image[baseLevel=%d] == NULL", baseLevel
);
432 /* Check width/height/depth for zero */
433 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
434 t
->Image
[0][baseLevel
]->Height
== 0 ||
435 t
->Image
[0][baseLevel
]->Depth
== 0) {
436 incomplete(t
, "texture width = 0");
440 /* Compute _MaxLevel */
441 if ((t
->Target
== GL_TEXTURE_1D
) ||
442 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
443 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
444 maxLevels
= ctx
->Const
.MaxTextureLevels
;
446 else if ((t
->Target
== GL_TEXTURE_2D
) ||
447 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
448 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
449 t
->Image
[0][baseLevel
]->HeightLog2
);
450 maxLevels
= ctx
->Const
.MaxTextureLevels
;
452 else if (t
->Target
== GL_TEXTURE_3D
) {
453 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
454 t
->Image
[0][baseLevel
]->HeightLog2
);
455 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
456 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
458 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
459 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
460 t
->Image
[0][baseLevel
]->HeightLog2
);
461 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
463 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
464 maxLog2
= 0; /* not applicable */
465 maxLevels
= 1; /* no mipmapping */
468 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
472 ASSERT(maxLevels
> 0);
474 if (t
->MaxLevel
< t
->BaseLevel
) {
475 incomplete(t
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
476 t
->MaxLevel
, t
->BaseLevel
);
480 t
->_MaxLevel
= baseLevel
+ maxLog2
;
481 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
482 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
484 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
485 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
487 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
488 /* make sure that all six cube map level 0 images are the same size */
489 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
490 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
492 for (face
= 1; face
< 6; face
++) {
493 if (t
->Image
[face
][baseLevel
] == NULL
||
494 t
->Image
[face
][baseLevel
]->Width2
!= w
||
495 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
496 incomplete(t
, "Cube face missing or mismatched size");
502 /* extra checking for mipmaps */
503 if (t
->MinFilter
!= GL_NEAREST
&& t
->MinFilter
!= GL_LINEAR
) {
505 * Mipmapping: determine if we have a complete set of mipmaps
508 GLint minLevel
= baseLevel
;
509 GLint maxLevel
= t
->_MaxLevel
;
511 if (minLevel
> maxLevel
) {
512 incomplete(t
, "minLevel > maxLevel");
516 /* Test dimension-independent attributes */
517 for (i
= minLevel
; i
<= maxLevel
; i
++) {
518 if (t
->Image
[0][i
]) {
519 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
520 incomplete(t
, "Format[i] != Format[baseLevel]");
523 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
524 incomplete(t
, "Border[i] != Border[baseLevel]");
530 /* Test things which depend on number of texture image dimensions */
531 if ((t
->Target
== GL_TEXTURE_1D
) ||
532 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
533 /* Test 1-D mipmaps */
534 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
535 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
539 if (i
>= minLevel
&& i
<= maxLevel
) {
540 if (!t
->Image
[0][i
]) {
541 incomplete(t
, "1D Image[0][i] == NULL");
544 if (t
->Image
[0][i
]->Width2
!= width
) {
545 incomplete(t
, "1D Image[0][i] bad width");
550 return; /* found smallest needed mipmap, all done! */
554 else if ((t
->Target
== GL_TEXTURE_2D
) ||
555 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
556 /* Test 2-D mipmaps */
557 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
558 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
559 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
566 if (i
>= minLevel
&& i
<= maxLevel
) {
567 if (!t
->Image
[0][i
]) {
568 incomplete(t
, "2D Image[0][i] == NULL");
571 if (t
->Image
[0][i
]->Width2
!= width
) {
572 incomplete(t
, "2D Image[0][i] bad width");
575 if (t
->Image
[0][i
]->Height2
!= height
) {
576 incomplete(t
, "2D Image[0][i] bad height");
579 if (width
==1 && height
==1) {
580 return; /* found smallest needed mipmap, all done! */
585 else if (t
->Target
== GL_TEXTURE_3D
) {
586 /* Test 3-D mipmaps */
587 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
588 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
589 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
590 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
600 if (i
>= minLevel
&& i
<= maxLevel
) {
601 if (!t
->Image
[0][i
]) {
602 incomplete(t
, "3D Image[0][i] == NULL");
605 if (t
->Image
[0][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
606 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
609 if (t
->Image
[0][i
]->Width2
!= width
) {
610 incomplete(t
, "3D Image[0][i] bad width");
613 if (t
->Image
[0][i
]->Height2
!= height
) {
614 incomplete(t
, "3D Image[0][i] bad height");
617 if (t
->Image
[0][i
]->Depth2
!= depth
) {
618 incomplete(t
, "3D Image[0][i] bad depth");
622 if (width
== 1 && height
== 1 && depth
== 1) {
623 return; /* found smallest needed mipmap, all done! */
627 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
628 /* make sure 6 cube faces are consistant */
629 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
630 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
631 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
638 if (i
>= minLevel
&& i
<= maxLevel
) {
640 for (face
= 0; face
< 6; face
++) {
641 /* check that we have images defined */
642 if (!t
->Image
[face
][i
]) {
643 incomplete(t
, "CubeMap Image[n][i] == NULL");
646 /* Don't support GL_DEPTH_COMPONENT for cube maps */
647 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
648 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
651 /* check that all six images have same size */
652 if (t
->Image
[face
][i
]->Width2
!= width
||
653 t
->Image
[face
][i
]->Height2
!= height
) {
654 incomplete(t
, "CubeMap Image[n][i] bad size");
659 if (width
== 1 && height
== 1) {
660 return; /* found smallest needed mipmap, all done! */
664 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
665 /* XXX special checking? */
669 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
676 * Check if the given cube map texture is "cube complete" as defined in
677 * the OpenGL specification.
680 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
682 const GLint baseLevel
= texObj
->BaseLevel
;
683 const struct gl_texture_image
*img0
, *img
;
686 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
689 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
))
692 /* check first face */
693 img0
= texObj
->Image
[0][baseLevel
];
696 img0
->Width
!= img0
->Height
)
699 /* check remaining faces vs. first face */
700 for (face
= 1; face
< 6; face
++) {
701 img
= texObj
->Image
[face
][baseLevel
];
703 img
->Width
!= img0
->Width
||
704 img
->Height
!= img0
->Height
||
705 img
->TexFormat
!= img0
->TexFormat
)
714 * Mark a texture object dirty. It forces the object to be incomplete
715 * and optionally forces the context to re-validate its state.
717 * \param ctx GL context.
718 * \param texObj texture object.
719 * \param invalidate_state also invalidate context state.
722 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
723 GLboolean invalidate_state
)
725 texObj
->_Complete
= GL_FALSE
;
726 if (invalidate_state
)
727 ctx
->NewState
|= _NEW_TEXTURE
;
732 * Return pointer to a default/fallback texture.
733 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
734 * That's the value a sampler should get when sampling from an
735 * incomplete texture.
737 struct gl_texture_object
*
738 _mesa_get_fallback_texture(struct gl_context
*ctx
)
740 if (!ctx
->Shared
->FallbackTex
) {
741 /* create fallback texture now */
742 static GLubyte texels
[8 * 8][4];
743 struct gl_texture_object
*texObj
;
744 struct gl_texture_image
*texImage
;
748 for (i
= 0; i
< 8 * 8; i
++) {
755 /* create texture object */
756 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, GL_TEXTURE_2D
);
757 assert(texObj
->RefCount
== 1);
758 texObj
->MinFilter
= GL_NEAREST
;
759 texObj
->MagFilter
= GL_NEAREST
;
761 /* create level[0] texture image */
762 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, 0);
764 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, GL_RGBA
, GL_RGBA
,
767 /* init the image fields */
768 _mesa_init_teximage_fields(ctx
, GL_TEXTURE_2D
, texImage
,
769 8, 8, 1, 0, GL_RGBA
, texFormat
);
771 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
774 ctx
->Driver
.TexImage2D(ctx
, GL_TEXTURE_2D
, 0, GL_RGBA
,
776 GL_RGBA
, GL_UNSIGNED_BYTE
, texels
,
777 &ctx
->DefaultPacking
, texObj
, texImage
);
779 _mesa_test_texobj_completeness(ctx
, texObj
);
780 assert(texObj
->_Complete
);
782 ctx
->Shared
->FallbackTex
= texObj
;
784 return ctx
->Shared
->FallbackTex
;
791 /***********************************************************************/
792 /** \name API functions */
797 * Generate texture names.
799 * \param n number of texture names to be generated.
800 * \param textures an array in which will hold the generated texture names.
802 * \sa glGenTextures().
804 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
805 * IDs which are stored in \p textures. Corresponding empty texture
806 * objects are also generated.
809 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
811 GET_CURRENT_CONTEXT(ctx
);
814 ASSERT_OUTSIDE_BEGIN_END(ctx
);
817 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
825 * This must be atomic (generation and allocation of texture IDs)
827 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
829 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
831 /* Allocate new, empty texture objects */
832 for (i
= 0; i
< n
; i
++) {
833 struct gl_texture_object
*texObj
;
834 GLuint name
= first
+ i
;
836 texObj
= (*ctx
->Driver
.NewTextureObject
)( ctx
, name
, target
);
838 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
839 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
843 /* insert into hash table */
844 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
849 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
854 * Check if the given texture object is bound to the current draw or
855 * read framebuffer. If so, Unbind it.
858 unbind_texobj_from_fbo(struct gl_context
*ctx
,
859 struct gl_texture_object
*texObj
)
861 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
864 for (i
= 0; i
< n
; i
++) {
865 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
868 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
869 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
870 fb
->Attachment
[j
].Texture
== texObj
) {
871 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
880 * Check if the given texture object is bound to any texture image units and
881 * unbind it if so (revert to default textures).
884 unbind_texobj_from_texunits(struct gl_context
*ctx
,
885 struct gl_texture_object
*texObj
)
889 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
890 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
891 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
892 if (texObj
== unit
->CurrentTex
[tex
]) {
893 _mesa_reference_texobj(&unit
->CurrentTex
[tex
],
894 ctx
->Shared
->DefaultTex
[tex
]);
895 ASSERT(unit
->CurrentTex
[tex
]);
904 * Delete named textures.
906 * \param n number of textures to be deleted.
907 * \param textures array of texture IDs to be deleted.
909 * \sa glDeleteTextures().
911 * If we're about to delete a texture that's currently bound to any
912 * texture unit, unbind the texture first. Decrement the reference
913 * count on the texture object and delete it if it's zero.
914 * Recall that texture objects can be shared among several rendering
918 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
920 GET_CURRENT_CONTEXT(ctx
);
922 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
927 for (i
= 0; i
< n
; i
++) {
928 if (textures
[i
] > 0) {
929 struct gl_texture_object
*delObj
930 = _mesa_lookup_texture(ctx
, textures
[i
]);
933 _mesa_lock_texture(ctx
, delObj
);
935 /* Check if texture is bound to any framebuffer objects.
937 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
939 unbind_texobj_from_fbo(ctx
, delObj
);
941 /* Check if this texture is currently bound to any texture units.
944 unbind_texobj_from_texunits(ctx
, delObj
);
946 _mesa_unlock_texture(ctx
, delObj
);
948 ctx
->NewState
|= _NEW_TEXTURE
;
950 /* The texture _name_ is now free for re-use.
951 * Remove it from the hash table now.
953 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
954 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
955 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
957 /* Unreference the texobj. If refcount hits zero, the texture
960 _mesa_reference_texobj(&delObj
, NULL
);
968 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
969 * into the corresponding Mesa texture target index.
970 * Note that proxy targets are not valid here.
971 * \return TEXTURE_x_INDEX or -1 if target is invalid
974 target_enum_to_index(GLenum target
)
978 return TEXTURE_1D_INDEX
;
980 return TEXTURE_2D_INDEX
;
982 return TEXTURE_3D_INDEX
;
983 case GL_TEXTURE_CUBE_MAP_ARB
:
984 return TEXTURE_CUBE_INDEX
;
985 case GL_TEXTURE_RECTANGLE_NV
:
986 return TEXTURE_RECT_INDEX
;
987 case GL_TEXTURE_1D_ARRAY_EXT
:
988 return TEXTURE_1D_ARRAY_INDEX
;
989 case GL_TEXTURE_2D_ARRAY_EXT
:
990 return TEXTURE_2D_ARRAY_INDEX
;
998 * Bind a named texture to a texturing target.
1000 * \param target texture target.
1001 * \param texName texture name.
1003 * \sa glBindTexture().
1005 * Determines the old texture object bound and returns immediately if rebinding
1006 * the same texture. Get the current texture which is either a default texture
1007 * if name is null, a named texture from the hash, or a new texture if the
1008 * given texture name is new. Increments its reference count, binds it, and
1009 * calls dd_function_table::BindTexture. Decrements the old texture reference
1010 * count and deletes it if it reaches zero.
1013 _mesa_BindTexture( GLenum target
, GLuint texName
)
1015 GET_CURRENT_CONTEXT(ctx
);
1016 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
1017 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1018 struct gl_texture_object
*newTexObj
= NULL
;
1020 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1022 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1023 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1024 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1026 targetIndex
= target_enum_to_index(target
);
1027 if (targetIndex
< 0) {
1028 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1031 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1034 * Get pointer to new texture object (newTexObj)
1037 /* Use a default texture object */
1038 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1041 /* non-default texture object */
1042 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1044 /* error checking */
1045 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1046 /* the named texture object's target doesn't match the given target */
1047 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1048 "glBindTexture(target mismatch)" );
1051 if (newTexObj
->Target
== 0) {
1052 finish_texture_init(ctx
, target
, newTexObj
);
1056 /* if this is a new texture id, allocate a texture object now */
1057 newTexObj
= (*ctx
->Driver
.NewTextureObject
)(ctx
, texName
, target
);
1059 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1063 /* and insert it into hash table */
1064 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1065 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1066 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1068 newTexObj
->Target
= target
;
1071 assert(valid_texture_object(newTexObj
));
1073 /* Check if this texture is only used by this context and is already bound.
1074 * If so, just return.
1077 GLboolean early_out
;
1078 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1079 early_out
= ((ctx
->Shared
->RefCount
== 1)
1080 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1081 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1087 /* flush before changing binding */
1088 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1090 /* Do the actual binding. The refcount on the previously bound
1091 * texture object will be decremented. It'll be deleted if the
1094 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1095 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1097 /* Pass BindTexture call to device driver */
1098 if (ctx
->Driver
.BindTexture
)
1099 (*ctx
->Driver
.BindTexture
)( ctx
, target
, newTexObj
);
1104 * Set texture priorities.
1106 * \param n number of textures.
1107 * \param texName texture names.
1108 * \param priorities corresponding texture priorities.
1110 * \sa glPrioritizeTextures().
1112 * Looks up each texture in the hash, clamps the corresponding priority between
1113 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1116 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1117 const GLclampf
*priorities
)
1119 GET_CURRENT_CONTEXT(ctx
);
1121 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1124 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1131 for (i
= 0; i
< n
; i
++) {
1132 if (texName
[i
] > 0) {
1133 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1135 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1140 ctx
->NewState
|= _NEW_TEXTURE
;
1146 * See if textures are loaded in texture memory.
1148 * \param n number of textures to query.
1149 * \param texName array with the texture names.
1150 * \param residences array which will hold the residence status.
1152 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1154 * \sa glAreTexturesResident().
1156 * Looks up each texture in the hash and calls
1157 * dd_function_table::IsTextureResident.
1159 GLboolean GLAPIENTRY
1160 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1161 GLboolean
*residences
)
1163 GET_CURRENT_CONTEXT(ctx
);
1164 GLboolean allResident
= GL_TRUE
;
1166 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1169 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1173 if (!texName
|| !residences
)
1176 for (i
= 0; i
< n
; i
++) {
1177 struct gl_texture_object
*t
;
1178 if (texName
[i
] == 0) {
1179 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1182 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1184 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1187 if (!ctx
->Driver
.IsTextureResident
||
1188 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1189 /* The texture is resident */
1191 residences
[i
] = GL_TRUE
;
1194 /* The texture is not resident */
1196 allResident
= GL_FALSE
;
1197 for (j
= 0; j
< i
; j
++)
1198 residences
[j
] = GL_TRUE
;
1200 residences
[i
] = GL_FALSE
;
1209 * See if a name corresponds to a texture.
1211 * \param texture texture name.
1213 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1216 * \sa glIsTexture().
1218 * Calls _mesa_HashLookup().
1220 GLboolean GLAPIENTRY
1221 _mesa_IsTexture( GLuint texture
)
1223 struct gl_texture_object
*t
;
1224 GET_CURRENT_CONTEXT(ctx
);
1225 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1230 t
= _mesa_lookup_texture(ctx
, texture
);
1232 /* IsTexture is true only after object has been bound once. */
1233 return t
&& t
->Target
;
1238 * Simplest implementation of texture locking: grab the shared tex
1239 * mutex. Examine the shared context state timestamp and if there has
1240 * been a change, set the appropriate bits in ctx->NewState.
1242 * This is used to deal with synchronizing things when a texture object
1243 * is used/modified by different contexts (or threads) which are sharing
1246 * See also _mesa_lock/unlock_texture() in teximage.h
1249 _mesa_lock_context_textures( struct gl_context
*ctx
)
1251 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1253 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1254 ctx
->NewState
|= _NEW_TEXTURE
;
1255 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1261 _mesa_unlock_context_textures( struct gl_context
*ctx
)
1263 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1264 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);