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"
32 #if FEATURE_colortable
45 #include "shader/prog_instruction.h"
49 /**********************************************************************/
50 /** \name Internal functions */
55 * Return the gl_texture_object for a given ID.
57 struct gl_texture_object *
58 _mesa_lookup_texture(GLcontext *ctx, GLuint id)
60 return (struct gl_texture_object *)
61 _mesa_HashLookup(ctx->Shared->TexObjects, id);
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
79 * \return pointer to new texture object.
81 struct gl_texture_object *
82 _mesa_new_texture_object( GLcontext *ctx, GLuint name, GLenum target )
84 struct gl_texture_object *obj;
86 obj = MALLOC_STRUCT(gl_texture_object);
87 _mesa_initialize_texture_object(obj, name, target);
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
99 _mesa_initialize_texture_object( struct gl_texture_object *obj,
100 GLuint name, GLenum target )
102 ASSERT(target == 0 ||
103 target == GL_TEXTURE_1D ||
104 target == GL_TEXTURE_2D ||
105 target == GL_TEXTURE_3D ||
106 target == GL_TEXTURE_CUBE_MAP_ARB ||
107 target == GL_TEXTURE_RECTANGLE_NV ||
108 target == GL_TEXTURE_1D_ARRAY_EXT ||
109 target == GL_TEXTURE_2D_ARRAY_EXT);
111 _mesa_bzero(obj, sizeof(*obj));
112 /* init the non-zero fields */
113 _glthread_INIT_MUTEX(obj->Mutex);
116 obj->Target = target;
117 obj->Priority = 1.0F;
118 if (target == GL_TEXTURE_RECTANGLE_NV) {
119 obj->WrapS = GL_CLAMP_TO_EDGE;
120 obj->WrapT = GL_CLAMP_TO_EDGE;
121 obj->WrapR = GL_CLAMP_TO_EDGE;
122 obj->MinFilter = GL_LINEAR;
125 obj->WrapS = GL_REPEAT;
126 obj->WrapT = GL_REPEAT;
127 obj->WrapR = GL_REPEAT;
128 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
130 obj->MagFilter = GL_LINEAR;
131 obj->MinLod = -1000.0;
132 obj->MaxLod = 1000.0;
135 obj->MaxLevel = 1000;
136 obj->MaxAnisotropy = 1.0;
137 obj->CompareMode = GL_NONE; /* ARB_shadow */
138 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
139 obj->CompareFailValue = 0.0F; /* ARB_shadow_ambient */
140 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
141 obj->Swizzle[0] = GL_RED;
142 obj->Swizzle[1] = GL_GREEN;
143 obj->Swizzle[2] = GL_BLUE;
144 obj->Swizzle[3] = GL_ALPHA;
145 obj->_Swizzle = SWIZZLE_NOOP;
150 * Some texture initialization can't be finished until we know which
151 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
154 finish_texture_init(GLcontext *ctx, GLenum target,
155 struct gl_texture_object *obj)
157 assert(obj->Target == 0);
159 if (target == GL_TEXTURE_RECTANGLE_NV) {
160 /* have to init wrap and filter state here - kind of klunky */
161 obj->WrapS = GL_CLAMP_TO_EDGE;
162 obj->WrapT = GL_CLAMP_TO_EDGE;
163 obj->WrapR = GL_CLAMP_TO_EDGE;
164 obj->MinFilter = GL_LINEAR;
165 if (ctx->Driver.TexParameter) {
166 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
167 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
168 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
169 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
170 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
171 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
178 * Deallocate a texture object struct. It should have already been
179 * removed from the texture object pool.
180 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
182 * \param shared the shared GL state to which the object belongs.
183 * \param texObj the texture object to delete.
186 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
192 /* Set Target to an invalid value. With some assertions elsewhere
193 * we can try to detect possible use of deleted textures.
195 texObj->Target = 0x99;
197 #if FEATURE_colortable
198 _mesa_free_colortable_data(&texObj->Palette);
201 /* free the texture images */
202 for (face = 0; face < 6; face++) {
203 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
204 if (texObj->Image[face][i]) {
205 _mesa_delete_texture_image( ctx, texObj->Image[face][i] );
210 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
211 _glthread_DESTROY_MUTEX(texObj->Mutex);
213 /* free this object */
221 * Copy texture object state from one texture object to another.
222 * Use for glPush/PopAttrib.
224 * \param dest destination texture object.
225 * \param src source texture object.
228 _mesa_copy_texture_object( struct gl_texture_object *dest,
229 const struct gl_texture_object *src )
231 dest->Target = src->Target;
232 dest->Name = src->Name;
233 dest->Priority = src->Priority;
234 dest->BorderColor[0] = src->BorderColor[0];
235 dest->BorderColor[1] = src->BorderColor[1];
236 dest->BorderColor[2] = src->BorderColor[2];
237 dest->BorderColor[3] = src->BorderColor[3];
238 dest->WrapS = src->WrapS;
239 dest->WrapT = src->WrapT;
240 dest->WrapR = src->WrapR;
241 dest->MinFilter = src->MinFilter;
242 dest->MagFilter = src->MagFilter;
243 dest->MinLod = src->MinLod;
244 dest->MaxLod = src->MaxLod;
245 dest->LodBias = src->LodBias;
246 dest->BaseLevel = src->BaseLevel;
247 dest->MaxLevel = src->MaxLevel;
248 dest->MaxAnisotropy = src->MaxAnisotropy;
249 dest->CompareMode = src->CompareMode;
250 dest->CompareFunc = src->CompareFunc;
251 dest->CompareFailValue = src->CompareFailValue;
252 dest->DepthMode = src->DepthMode;
253 dest->_MaxLevel = src->_MaxLevel;
254 dest->_MaxLambda = src->_MaxLambda;
255 dest->GenerateMipmap = src->GenerateMipmap;
256 dest->Palette = src->Palette;
257 dest->_Complete = src->_Complete;
258 COPY_4V(dest->Swizzle, src->Swizzle);
259 dest->_Swizzle = src->_Swizzle;
264 * Check if the given texture object is valid by examining its Target field.
265 * For debugging only.
268 valid_texture_object(const struct gl_texture_object *tex)
270 switch (tex->Target) {
275 case GL_TEXTURE_CUBE_MAP_ARB:
276 case GL_TEXTURE_RECTANGLE_NV:
277 case GL_TEXTURE_1D_ARRAY_EXT:
278 case GL_TEXTURE_2D_ARRAY_EXT:
281 _mesa_problem(NULL, "invalid reference to a deleted texture object");
284 _mesa_problem(NULL, "invalid texture object Target value");
291 * Reference (or unreference) a texture object.
292 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
293 * If 'tex' is non-null, increment its refcount.
296 _mesa_reference_texobj(struct gl_texture_object **ptr,
297 struct gl_texture_object *tex)
306 /* Unreference the old texture */
307 GLboolean deleteFlag = GL_FALSE;
308 struct gl_texture_object *oldTex = *ptr;
310 assert(valid_texture_object(oldTex));
312 _glthread_LOCK_MUTEX(oldTex->Mutex);
313 ASSERT(oldTex->RefCount > 0);
316 deleteFlag = (oldTex->RefCount == 0);
317 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
320 GET_CURRENT_CONTEXT(ctx);
322 ctx->Driver.DeleteTexture(ctx, oldTex);
324 _mesa_problem(NULL, "Unable to delete texture, no context");
332 /* reference new texture */
333 assert(valid_texture_object(tex));
334 _glthread_LOCK_MUTEX(tex->Mutex);
335 if (tex->RefCount == 0) {
336 /* this texture's being deleted (look just above) */
337 /* Not sure this can every really happen. Warn if it does. */
338 _mesa_problem(NULL, "referencing deleted texture object");
345 _glthread_UNLOCK_MUTEX(tex->Mutex);
352 * Report why a texture object is incomplete.
354 * \param t texture object.
355 * \param why string describing why it's incomplete.
357 * \note For debug purposes only.
361 incomplete(const struct gl_texture_object *t, const char *why)
363 _mesa_printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
366 #define incomplete(t, why)
371 * Examine a texture object to determine if it is complete.
373 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
376 * \param ctx GL context.
377 * \param t texture object.
379 * According to the texture target, verifies that each of the mipmaps is
380 * present and has the expected size.
383 _mesa_test_texobj_completeness( const GLcontext *ctx,
384 struct gl_texture_object *t )
386 const GLint baseLevel = t->BaseLevel;
387 GLint maxLog2 = 0, maxLevels = 0;
389 t->_Complete = GL_TRUE; /* be optimistic */
391 /* Detect cases where the application set the base level to an invalid
394 if ((baseLevel < 0) || (baseLevel > MAX_TEXTURE_LEVELS)) {
396 _mesa_sprintf(s, "base level = %d is invalid", baseLevel);
398 t->_Complete = GL_FALSE;
402 /* Always need the base level image */
403 if (!t->Image[0][baseLevel]) {
405 _mesa_sprintf(s, "Image[baseLevel=%d] == NULL", baseLevel);
407 t->_Complete = GL_FALSE;
411 /* Check width/height/depth for zero */
412 if (t->Image[0][baseLevel]->Width == 0 ||
413 t->Image[0][baseLevel]->Height == 0 ||
414 t->Image[0][baseLevel]->Depth == 0) {
415 incomplete(t, "texture width = 0");
416 t->_Complete = GL_FALSE;
420 /* Compute _MaxLevel */
421 if ((t->Target == GL_TEXTURE_1D) ||
422 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
423 maxLog2 = t->Image[0][baseLevel]->WidthLog2;
424 maxLevels = ctx->Const.MaxTextureLevels;
426 else if ((t->Target == GL_TEXTURE_2D) ||
427 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
428 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
429 t->Image[0][baseLevel]->HeightLog2);
430 maxLevels = ctx->Const.MaxTextureLevels;
432 else if (t->Target == GL_TEXTURE_3D) {
433 GLint max = MAX2(t->Image[0][baseLevel]->WidthLog2,
434 t->Image[0][baseLevel]->HeightLog2);
435 maxLog2 = MAX2(max, (GLint)(t->Image[0][baseLevel]->DepthLog2));
436 maxLevels = ctx->Const.Max3DTextureLevels;
438 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
439 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
440 t->Image[0][baseLevel]->HeightLog2);
441 maxLevels = ctx->Const.MaxCubeTextureLevels;
443 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
444 maxLog2 = 0; /* not applicable */
445 maxLevels = 1; /* no mipmapping */
448 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
452 ASSERT(maxLevels > 0);
454 t->_MaxLevel = baseLevel + maxLog2;
455 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
456 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
458 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
459 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
461 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
462 /* make sure that all six cube map level 0 images are the same size */
463 const GLuint w = t->Image[0][baseLevel]->Width2;
464 const GLuint h = t->Image[0][baseLevel]->Height2;
466 for (face = 1; face < 6; face++) {
467 if (t->Image[face][baseLevel] == NULL ||
468 t->Image[face][baseLevel]->Width2 != w ||
469 t->Image[face][baseLevel]->Height2 != h) {
470 t->_Complete = GL_FALSE;
471 incomplete(t, "Non-quare cubemap image");
477 /* extra checking for mipmaps */
478 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
480 * Mipmapping: determine if we have a complete set of mipmaps
483 GLint minLevel = baseLevel;
484 GLint maxLevel = t->_MaxLevel;
486 if (minLevel > maxLevel) {
487 t->_Complete = GL_FALSE;
488 incomplete(t, "minLevel > maxLevel");
492 /* Test dimension-independent attributes */
493 for (i = minLevel; i <= maxLevel; i++) {
494 if (t->Image[0][i]) {
495 if (t->Image[0][i]->TexFormat != t->Image[0][baseLevel]->TexFormat) {
496 t->_Complete = GL_FALSE;
497 incomplete(t, "Format[i] != Format[baseLevel]");
500 if (t->Image[0][i]->Border != t->Image[0][baseLevel]->Border) {
501 t->_Complete = GL_FALSE;
502 incomplete(t, "Border[i] != Border[baseLevel]");
508 /* Test things which depend on number of texture image dimensions */
509 if ((t->Target == GL_TEXTURE_1D) ||
510 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
511 /* Test 1-D mipmaps */
512 GLuint width = t->Image[0][baseLevel]->Width2;
513 for (i = baseLevel + 1; i < maxLevels; i++) {
517 if (i >= minLevel && i <= maxLevel) {
518 if (!t->Image[0][i]) {
519 t->_Complete = GL_FALSE;
520 incomplete(t, "1D Image[0][i] == NULL");
523 if (t->Image[0][i]->Width2 != width ) {
524 t->_Complete = GL_FALSE;
525 incomplete(t, "1D Image[0][i] bad width");
530 return; /* found smallest needed mipmap, all done! */
534 else if ((t->Target == GL_TEXTURE_2D) ||
535 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
536 /* Test 2-D mipmaps */
537 GLuint width = t->Image[0][baseLevel]->Width2;
538 GLuint height = t->Image[0][baseLevel]->Height2;
539 for (i = baseLevel + 1; i < maxLevels; i++) {
546 if (i >= minLevel && i <= maxLevel) {
547 if (!t->Image[0][i]) {
548 t->_Complete = GL_FALSE;
549 incomplete(t, "2D Image[0][i] == NULL");
552 if (t->Image[0][i]->Width2 != width) {
553 t->_Complete = GL_FALSE;
554 incomplete(t, "2D Image[0][i] bad width");
557 if (t->Image[0][i]->Height2 != height) {
558 t->_Complete = GL_FALSE;
559 incomplete(t, "2D Image[0][i] bad height");
562 if (width==1 && height==1) {
563 return; /* found smallest needed mipmap, all done! */
568 else if (t->Target == GL_TEXTURE_3D) {
569 /* Test 3-D mipmaps */
570 GLuint width = t->Image[0][baseLevel]->Width2;
571 GLuint height = t->Image[0][baseLevel]->Height2;
572 GLuint depth = t->Image[0][baseLevel]->Depth2;
573 for (i = baseLevel + 1; i < maxLevels; i++) {
583 if (i >= minLevel && i <= maxLevel) {
584 if (!t->Image[0][i]) {
585 incomplete(t, "3D Image[0][i] == NULL");
586 t->_Complete = GL_FALSE;
589 if (t->Image[0][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
590 t->_Complete = GL_FALSE;
591 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
594 if (t->Image[0][i]->Width2 != width) {
595 t->_Complete = GL_FALSE;
596 incomplete(t, "3D Image[0][i] bad width");
599 if (t->Image[0][i]->Height2 != height) {
600 t->_Complete = GL_FALSE;
601 incomplete(t, "3D Image[0][i] bad height");
604 if (t->Image[0][i]->Depth2 != depth) {
605 t->_Complete = GL_FALSE;
606 incomplete(t, "3D Image[0][i] bad depth");
610 if (width == 1 && height == 1 && depth == 1) {
611 return; /* found smallest needed mipmap, all done! */
615 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
616 /* make sure 6 cube faces are consistant */
617 GLuint width = t->Image[0][baseLevel]->Width2;
618 GLuint height = t->Image[0][baseLevel]->Height2;
619 for (i = baseLevel + 1; i < maxLevels; i++) {
626 if (i >= minLevel && i <= maxLevel) {
628 for (face = 0; face < 6; face++) {
629 /* check that we have images defined */
630 if (!t->Image[face][i]) {
631 t->_Complete = GL_FALSE;
632 incomplete(t, "CubeMap Image[n][i] == NULL");
635 /* Don't support GL_DEPTH_COMPONENT for cube maps */
636 if (t->Image[face][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
637 t->_Complete = GL_FALSE;
638 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
641 /* check that all six images have same size */
642 if (t->Image[face][i]->Width2!=width ||
643 t->Image[face][i]->Height2!=height) {
644 t->_Complete = GL_FALSE;
645 incomplete(t, "CubeMap Image[n][i] bad size");
650 if (width == 1 && height == 1) {
651 return; /* found smallest needed mipmap, all done! */
655 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
656 /* XXX special checking? */
660 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
667 * Return pointer to a default/fallback texture.
668 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
669 * That's the value a sampler should get when sampling from an
670 * incomplete texture.
672 struct gl_texture_object *
673 _mesa_get_fallback_texture(GLcontext *ctx)
675 if (!ctx->Shared->FallbackTex) {
676 /* create fallback texture now */
677 static GLubyte texels[8 * 8][4];
678 struct gl_texture_object *texObj;
679 struct gl_texture_image *texImage;
682 for (i = 0; i < 8 * 8; i++) {
689 /* create texture object */
690 texObj = ctx->Driver.NewTextureObject(ctx, 0, GL_TEXTURE_2D);
691 assert(texObj->RefCount == 1);
692 texObj->MinFilter = GL_NEAREST;
693 texObj->MagFilter = GL_NEAREST;
695 /* create level[0] texture image */
696 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, 0);
698 /* init the image fields */
699 _mesa_init_teximage_fields(ctx, GL_TEXTURE_2D, texImage,
700 8, 8, 1, 0, GL_RGBA);
703 ctx->Driver.TexImage2D(ctx, GL_TEXTURE_2D, 0, GL_RGBA,
705 GL_RGBA, GL_UNSIGNED_BYTE, texels,
706 &ctx->DefaultPacking, texObj, texImage);
708 _mesa_test_texobj_completeness(ctx, texObj);
709 assert(texObj->_Complete);
711 ctx->Shared->FallbackTex = texObj;
713 return ctx->Shared->FallbackTex;
721 /***********************************************************************/
722 /** \name API functions */
727 * Generate texture names.
729 * \param n number of texture names to be generated.
730 * \param textures an array in which will hold the generated texture names.
732 * \sa glGenTextures().
734 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
735 * IDs which are stored in \p textures. Corresponding empty texture
736 * objects are also generated.
739 _mesa_GenTextures( GLsizei n, GLuint *textures )
741 GET_CURRENT_CONTEXT(ctx);
744 ASSERT_OUTSIDE_BEGIN_END(ctx);
747 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
755 * This must be atomic (generation and allocation of texture IDs)
757 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
759 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
761 /* Allocate new, empty texture objects */
762 for (i = 0; i < n; i++) {
763 struct gl_texture_object *texObj;
764 GLuint name = first + i;
766 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
768 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
769 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
773 /* insert into hash table */
774 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
779 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
784 * Check if the given texture object is bound to the current draw or
785 * read framebuffer. If so, Unbind it.
788 unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
790 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
793 for (i = 0; i < n; i++) {
794 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
797 for (j = 0; j < BUFFER_COUNT; j++) {
798 if (fb->Attachment[j].Type == GL_TEXTURE &&
799 fb->Attachment[j].Texture == texObj) {
800 _mesa_remove_attachment(ctx, fb->Attachment + j);
809 * Check if the given texture object is bound to any texture image units and
810 * unbind it if so (revert to default textures).
813 unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
817 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
818 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
819 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
820 if (texObj == unit->CurrentTex[tex]) {
821 _mesa_reference_texobj(&unit->CurrentTex[tex],
822 ctx->Shared->DefaultTex[TEXTURE_1D_INDEX]);
823 ASSERT(unit->CurrentTex[tex]);
832 * Delete named textures.
834 * \param n number of textures to be deleted.
835 * \param textures array of texture IDs to be deleted.
837 * \sa glDeleteTextures().
839 * If we're about to delete a texture that's currently bound to any
840 * texture unit, unbind the texture first. Decrement the reference
841 * count on the texture object and delete it if it's zero.
842 * Recall that texture objects can be shared among several rendering
846 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
848 GET_CURRENT_CONTEXT(ctx);
850 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
855 for (i = 0; i < n; i++) {
856 if (textures[i] > 0) {
857 struct gl_texture_object *delObj
858 = _mesa_lookup_texture(ctx, textures[i]);
861 _mesa_lock_texture(ctx, delObj);
863 /* Check if texture is bound to any framebuffer objects.
865 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
867 unbind_texobj_from_fbo(ctx, delObj);
869 /* Check if this texture is currently bound to any texture units.
872 unbind_texobj_from_texunits(ctx, delObj);
874 _mesa_unlock_texture(ctx, delObj);
876 ctx->NewState |= _NEW_TEXTURE;
878 /* The texture _name_ is now free for re-use.
879 * Remove it from the hash table now.
881 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
882 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
883 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
885 /* Unreference the texobj. If refcount hits zero, the texture
888 _mesa_reference_texobj(&delObj, NULL);
896 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
897 * into the corresponding Mesa texture target index.
898 * Return -1 if target is invalid.
901 target_enum_to_index(GLenum target)
905 return TEXTURE_1D_INDEX;
907 return TEXTURE_2D_INDEX;
909 return TEXTURE_3D_INDEX;
910 case GL_TEXTURE_CUBE_MAP_ARB:
911 return TEXTURE_CUBE_INDEX;
912 case GL_TEXTURE_RECTANGLE_NV:
913 return TEXTURE_RECT_INDEX;
914 case GL_TEXTURE_1D_ARRAY_EXT:
915 return TEXTURE_1D_ARRAY_INDEX;
916 case GL_TEXTURE_2D_ARRAY_EXT:
917 return TEXTURE_2D_ARRAY_INDEX;
925 * Bind a named texture to a texturing target.
927 * \param target texture target.
928 * \param texName texture name.
930 * \sa glBindTexture().
932 * Determines the old texture object bound and returns immediately if rebinding
933 * the same texture. Get the current texture which is either a default texture
934 * if name is null, a named texture from the hash, or a new texture if the
935 * given texture name is new. Increments its reference count, binds it, and
936 * calls dd_function_table::BindTexture. Decrements the old texture reference
937 * count and deletes it if it reaches zero.
940 _mesa_BindTexture( GLenum target, GLuint texName )
942 GET_CURRENT_CONTEXT(ctx);
943 const GLuint unit = ctx->Texture.CurrentUnit;
944 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
945 struct gl_texture_object *newTexObj = NULL, *defaultTexObj = NULL;
947 ASSERT_OUTSIDE_BEGIN_END(ctx);
949 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
950 _mesa_debug(ctx, "glBindTexture %s %d\n",
951 _mesa_lookup_enum_by_nr(target), (GLint) texName);
953 targetIndex = target_enum_to_index(target);
954 if (targetIndex < 0) {
955 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
958 assert(targetIndex < NUM_TEXTURE_TARGETS);
959 defaultTexObj = ctx->Shared->DefaultTex[targetIndex];
962 * Get pointer to new texture object (newTexObj)
965 newTexObj = defaultTexObj;
968 /* non-default texture object */
969 newTexObj = _mesa_lookup_texture(ctx, texName);
972 if (newTexObj->Target != 0 && newTexObj->Target != target) {
973 /* the named texture object's target doesn't match the given target */
974 _mesa_error( ctx, GL_INVALID_OPERATION,
975 "glBindTexture(target mismatch)" );
978 if (newTexObj->Target == 0) {
979 finish_texture_init(ctx, target, newTexObj);
983 /* if this is a new texture id, allocate a texture object now */
984 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
986 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
990 /* and insert it into hash table */
991 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
992 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
993 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
995 newTexObj->Target = target;
998 assert(valid_texture_object(newTexObj));
1000 /* flush before changing binding */
1001 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1003 /* Do the actual binding. The refcount on the previously bound
1004 * texture object will be decremented. It'll be deleted if the
1007 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1008 ASSERT(texUnit->CurrentTex[targetIndex]);
1010 /* Pass BindTexture call to device driver */
1011 if (ctx->Driver.BindTexture)
1012 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
1017 * Set texture priorities.
1019 * \param n number of textures.
1020 * \param texName texture names.
1021 * \param priorities corresponding texture priorities.
1023 * \sa glPrioritizeTextures().
1025 * Looks up each texture in the hash, clamps the corresponding priority between
1026 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1029 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1030 const GLclampf *priorities )
1032 GET_CURRENT_CONTEXT(ctx);
1034 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1037 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1044 for (i = 0; i < n; i++) {
1045 if (texName[i] > 0) {
1046 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1048 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1049 if (ctx->Driver.PrioritizeTexture)
1050 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
1055 ctx->NewState |= _NEW_TEXTURE;
1059 * See if textures are loaded in texture memory.
1061 * \param n number of textures to query.
1062 * \param texName array with the texture names.
1063 * \param residences array which will hold the residence status.
1065 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1067 * \sa glAreTexturesResident().
1069 * Looks up each texture in the hash and calls
1070 * dd_function_table::IsTextureResident.
1072 GLboolean GLAPIENTRY
1073 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1074 GLboolean *residences)
1076 GET_CURRENT_CONTEXT(ctx);
1077 GLboolean allResident = GL_TRUE;
1079 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1082 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1086 if (!texName || !residences)
1089 for (i = 0; i < n; i++) {
1090 struct gl_texture_object *t;
1091 if (texName[i] == 0) {
1092 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1095 t = _mesa_lookup_texture(ctx, texName[i]);
1097 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1100 if (!ctx->Driver.IsTextureResident ||
1101 ctx->Driver.IsTextureResident(ctx, t)) {
1102 /* The texture is resident */
1104 residences[i] = GL_TRUE;
1107 /* The texture is not resident */
1109 allResident = GL_FALSE;
1110 for (j = 0; j < i; j++)
1111 residences[j] = GL_TRUE;
1113 residences[i] = GL_FALSE;
1121 * See if a name corresponds to a texture.
1123 * \param texture texture name.
1125 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1128 * \sa glIsTexture().
1130 * Calls _mesa_HashLookup().
1132 GLboolean GLAPIENTRY
1133 _mesa_IsTexture( GLuint texture )
1135 struct gl_texture_object *t;
1136 GET_CURRENT_CONTEXT(ctx);
1137 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1142 t = _mesa_lookup_texture(ctx, texture);
1144 /* IsTexture is true only after object has been bound once. */
1145 return t && t->Target;
1150 * Simplest implementation of texture locking: Grab the a new mutex in
1151 * the shared context. Examine the shared context state timestamp and
1152 * if there has been a change, set the appropriate bits in
1155 * This is used to deal with synchronizing things when a texture object
1156 * is used/modified by different contexts (or threads) which are sharing
1159 * See also _mesa_lock/unlock_texture() in teximage.h
1162 _mesa_lock_context_textures( GLcontext *ctx )
1164 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1166 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1167 ctx->NewState |= _NEW_TEXTURE;
1168 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1174 _mesa_unlock_context_textures( GLcontext *ctx )
1176 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1177 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);