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 #include "bufferobj.h"
45 #include "program/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(struct gl_context
*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( struct gl_context
*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_1D_ARRAY_EXT
||
108 target
== GL_TEXTURE_2D_ARRAY_EXT
||
109 target
== GL_TEXTURE_BUFFER
);
111 memset(obj
, 0, sizeof(*obj
));
112 /* init the non-zero fields */
113 _glthread_INIT_MUTEX(obj
->Mutex
);
116 obj
->Target
= target
;
117 obj
->Priority
= 1.0F
;
119 obj
->MaxLevel
= 1000;
121 /* must be one; no support for (YUV) planes in separate buffers */
122 obj
->RequiredTextureImageUnits
= 1;
125 obj
->Sampler
.WrapS
= GL_REPEAT
;
126 obj
->Sampler
.WrapT
= GL_REPEAT
;
127 obj
->Sampler
.WrapR
= GL_REPEAT
;
128 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
130 obj
->Sampler
.MagFilter
= GL_LINEAR
;
131 obj
->Sampler
.MinLod
= -1000.0;
132 obj
->Sampler
.MaxLod
= 1000.0;
133 obj
->Sampler
.LodBias
= 0.0;
134 obj
->Sampler
.MaxAnisotropy
= 1.0;
139 * Deallocate a texture object struct. It should have already been
140 * removed from the texture object pool.
141 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
143 * \param shared the shared GL state to which the object belongs.
144 * \param texObj the texture object to delete.
147 _mesa_delete_texture_object(struct gl_context
*ctx
,
148 struct gl_texture_object
*texObj
)
152 /* Set Target to an invalid value. With some assertions elsewhere
153 * we can try to detect possible use of deleted textures.
155 texObj
->Target
= 0x99;
157 /* free the texture images */
158 for (face
= 0; face
< 6; face
++) {
159 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
160 if (texObj
->Image
[face
][i
]) {
161 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
166 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
168 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
169 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
171 /* free this object */
178 * Copy texture object state from one texture object to another.
179 * Use for glPush/PopAttrib.
181 * \param dest destination texture object.
182 * \param src source texture object.
185 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
186 const struct gl_texture_object
*src
)
188 dest
->Target
= src
->Target
;
189 dest
->Name
= src
->Name
;
190 dest
->Priority
= src
->Priority
;
191 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
192 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
193 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
194 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
195 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
196 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
197 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
198 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
199 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
200 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
201 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
202 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
203 dest
->BaseLevel
= src
->BaseLevel
;
204 dest
->MaxLevel
= src
->MaxLevel
;
205 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
206 dest
->_MaxLevel
= src
->_MaxLevel
;
207 dest
->_MaxLambda
= src
->_MaxLambda
;
208 dest
->GenerateMipmap
= src
->GenerateMipmap
;
209 dest
->_Complete
= src
->_Complete
;
211 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
216 * Free all texture images of the given texture object.
218 * \param ctx GL context.
219 * \param t texture object.
221 * \sa _mesa_clear_texture_image().
224 _mesa_clear_texture_object(struct gl_context
*ctx
,
225 struct gl_texture_object
*texObj
)
229 if (texObj
->Target
== 0)
232 for (i
= 0; i
< MAX_FACES
; i
++) {
233 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
234 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
236 _mesa_clear_texture_image(ctx
, texImage
);
243 * Check if the given texture object is valid by examining its Target field.
244 * For debugging only.
247 valid_texture_object(const struct gl_texture_object
*tex
)
249 switch (tex
->Target
) {
254 case GL_TEXTURE_CUBE_MAP_ARB
:
255 case GL_TEXTURE_1D_ARRAY_EXT
:
256 case GL_TEXTURE_2D_ARRAY_EXT
:
257 case GL_TEXTURE_BUFFER
:
260 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
263 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
264 tex
->Target
, tex
->Name
);
271 * Reference (or unreference) a texture object.
272 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
273 * If 'tex' is non-null, increment its refcount.
274 * This is normally only called from the _mesa_reference_texobj() macro
275 * when there's a real pointer change.
278 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
279 struct gl_texture_object
*tex
)
284 /* Unreference the old texture */
285 GLboolean deleteFlag
= GL_FALSE
;
286 struct gl_texture_object
*oldTex
= *ptr
;
288 ASSERT(valid_texture_object(oldTex
));
289 (void) valid_texture_object
; /* silence warning in release builds */
291 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
292 ASSERT(oldTex
->RefCount
> 0);
295 deleteFlag
= (oldTex
->RefCount
== 0);
296 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
299 GET_CURRENT_CONTEXT(ctx
);
301 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
303 _mesa_problem(NULL
, "Unable to delete texture, no context");
311 /* reference new texture */
312 ASSERT(valid_texture_object(tex
));
313 _glthread_LOCK_MUTEX(tex
->Mutex
);
314 if (tex
->RefCount
== 0) {
315 /* this texture's being deleted (look just above) */
316 /* Not sure this can every really happen. Warn if it does. */
317 _mesa_problem(NULL
, "referencing deleted texture object");
324 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
331 * Mark a texture object as incomplete.
332 * \param t texture object
333 * \param fmt... string describing why it's incomplete (for debugging).
336 incomplete(struct gl_texture_object
*t
, const char *fmt
, ...)
343 vsnprintf(s
, sizeof(s
), fmt
, args
);
346 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
348 t
->_Complete
= GL_FALSE
;
353 * Examine a texture object to determine if it is complete.
355 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
358 * \param ctx GL context.
359 * \param t texture object.
361 * According to the texture target, verifies that each of the mipmaps is
362 * present and has the expected size.
365 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
366 struct gl_texture_object
*t
)
368 const GLint baseLevel
= t
->BaseLevel
;
369 GLint maxLog2
= 0, maxLevels
= 0;
371 t
->_Complete
= GL_TRUE
; /* be optimistic */
373 /* Detect cases where the application set the base level to an invalid
376 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
377 incomplete(t
, "base level = %d is invalid", baseLevel
);
381 /* Always need the base level image */
382 if (!t
->Image
[0][baseLevel
]) {
383 incomplete(t
, "Image[baseLevel=%d] == NULL", baseLevel
);
387 /* Check width/height/depth for zero */
388 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
389 t
->Image
[0][baseLevel
]->Height
== 0 ||
390 t
->Image
[0][baseLevel
]->Depth
== 0) {
391 incomplete(t
, "texture width = 0");
395 /* Compute _MaxLevel */
396 if ((t
->Target
== GL_TEXTURE_1D
) ||
397 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
398 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
399 maxLevels
= ctx
->Const
.MaxTextureLevels
;
401 else if ((t
->Target
== GL_TEXTURE_2D
) ||
402 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
403 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
404 t
->Image
[0][baseLevel
]->HeightLog2
);
405 maxLevels
= ctx
->Const
.MaxTextureLevels
;
407 else if (t
->Target
== GL_TEXTURE_3D
) {
408 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
409 t
->Image
[0][baseLevel
]->HeightLog2
);
410 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
411 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
413 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
414 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
415 t
->Image
[0][baseLevel
]->HeightLog2
);
416 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
419 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
423 ASSERT(maxLevels
> 0);
425 if (t
->MaxLevel
< t
->BaseLevel
) {
426 incomplete(t
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
427 t
->MaxLevel
, t
->BaseLevel
);
431 t
->_MaxLevel
= baseLevel
+ maxLog2
;
432 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
433 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
435 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
436 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
439 /* This texture object was created with glTexStorage1/2/3D() so we
440 * know that all the mipmap levels are the right size and all cube
441 * map faces are the same size.
442 * We don't need to do any of the additional checks below.
447 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
448 /* make sure that all six cube map level 0 images are the same size */
449 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
450 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
452 for (face
= 1; face
< 6; face
++) {
453 if (t
->Image
[face
][baseLevel
] == NULL
||
454 t
->Image
[face
][baseLevel
]->Width2
!= w
||
455 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
456 incomplete(t
, "Cube face missing or mismatched size");
462 /* extra checking for mipmaps */
463 if (t
->Sampler
.MinFilter
!= GL_NEAREST
&& t
->Sampler
.MinFilter
!= GL_LINEAR
) {
465 * Mipmapping: determine if we have a complete set of mipmaps
468 GLint minLevel
= baseLevel
;
469 GLint maxLevel
= t
->_MaxLevel
;
471 if (minLevel
> maxLevel
) {
472 incomplete(t
, "minLevel > maxLevel");
476 /* Test dimension-independent attributes */
477 for (i
= minLevel
; i
<= maxLevel
; i
++) {
478 if (t
->Image
[0][i
]) {
479 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
480 incomplete(t
, "Format[i] != Format[baseLevel]");
483 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
484 incomplete(t
, "Border[i] != Border[baseLevel]");
490 /* Test things which depend on number of texture image dimensions */
491 if ((t
->Target
== GL_TEXTURE_1D
) ||
492 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
493 /* Test 1-D mipmaps */
494 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
495 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
499 if (i
>= minLevel
&& i
<= maxLevel
) {
500 const struct gl_texture_image
*img
= t
->Image
[0][i
];
502 incomplete(t
, "1D Image[%d] is missing", i
);
505 if (img
->Width2
!= width
) {
506 incomplete(t
, "1D Image[%d] bad width %u", i
, img
->Width2
);
511 return; /* found smallest needed mipmap, all done! */
515 else if ((t
->Target
== GL_TEXTURE_2D
) ||
516 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
517 /* Test 2-D mipmaps */
518 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
519 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
520 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
527 if (i
>= minLevel
&& i
<= maxLevel
) {
528 const struct gl_texture_image
*img
= t
->Image
[0][i
];
530 incomplete(t
, "2D Image[%d of %d] is missing", i
, maxLevel
);
533 if (img
->Width2
!= width
) {
534 incomplete(t
, "2D Image[%d] bad width %u", i
, img
->Width2
);
537 if (img
->Height2
!= height
) {
538 incomplete(t
, "2D Image[i] bad height %u", i
, img
->Height2
);
541 if (width
==1 && height
==1) {
542 return; /* found smallest needed mipmap, all done! */
547 else if (t
->Target
== GL_TEXTURE_3D
) {
548 /* Test 3-D mipmaps */
549 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
550 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
551 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
552 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
562 if (i
>= minLevel
&& i
<= maxLevel
) {
563 const struct gl_texture_image
*img
= t
->Image
[0][i
];
565 incomplete(t
, "3D Image[%d] is missing", i
);
568 if (img
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
569 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
572 if (img
->Width2
!= width
) {
573 incomplete(t
, "3D Image[%d] bad width %u", i
, img
->Width2
);
576 if (img
->Height2
!= height
) {
577 incomplete(t
, "3D Image[%d] bad height %u", i
, img
->Height2
);
580 if (img
->Depth2
!= depth
) {
581 incomplete(t
, "3D Image[%d] bad depth %u", i
, img
->Depth2
);
585 if (width
== 1 && height
== 1 && depth
== 1) {
586 return; /* found smallest needed mipmap, all done! */
590 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
591 /* make sure 6 cube faces are consistant */
592 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
593 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
594 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
601 if (i
>= minLevel
&& i
<= maxLevel
) {
603 for (face
= 0; face
< 6; face
++) {
604 /* check that we have images defined */
605 if (!t
->Image
[face
][i
]) {
606 incomplete(t
, "CubeMap Image[n][i] == NULL");
609 /* Don't support GL_DEPTH_COMPONENT for cube maps */
610 if (ctx
->VersionMajor
< 3 && !ctx
->Extensions
.EXT_gpu_shader4
) {
611 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
612 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
616 /* check that all six images have same size */
617 if (t
->Image
[face
][i
]->Width2
!= width
||
618 t
->Image
[face
][i
]->Height2
!= height
) {
619 incomplete(t
, "CubeMap Image[n][i] bad size");
624 if (width
== 1 && height
== 1) {
625 return; /* found smallest needed mipmap, all done! */
631 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
638 * Check if the given cube map texture is "cube complete" as defined in
639 * the OpenGL specification.
642 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
644 const GLint baseLevel
= texObj
->BaseLevel
;
645 const struct gl_texture_image
*img0
, *img
;
648 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
651 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
))
654 /* check first face */
655 img0
= texObj
->Image
[0][baseLevel
];
658 img0
->Width
!= img0
->Height
)
661 /* check remaining faces vs. first face */
662 for (face
= 1; face
< 6; face
++) {
663 img
= texObj
->Image
[face
][baseLevel
];
665 img
->Width
!= img0
->Width
||
666 img
->Height
!= img0
->Height
||
667 img
->TexFormat
!= img0
->TexFormat
)
676 * Mark a texture object dirty. It forces the object to be incomplete
677 * and optionally forces the context to re-validate its state.
679 * \param ctx GL context.
680 * \param texObj texture object.
681 * \param invalidate_state also invalidate context state.
684 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
685 GLboolean invalidate_state
)
687 texObj
->_Complete
= GL_FALSE
;
688 if (invalidate_state
)
689 ctx
->NewState
|= _NEW_TEXTURE
;
694 * Return pointer to a default/fallback texture.
695 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
696 * That's the value a sampler should get when sampling from an
697 * incomplete texture.
699 struct gl_texture_object
*
700 _mesa_get_fallback_texture(struct gl_context
*ctx
)
702 if (!ctx
->Shared
->FallbackTex
) {
703 /* create fallback texture now */
704 static GLubyte texels
[8 * 8][4];
705 struct gl_texture_object
*texObj
;
706 struct gl_texture_image
*texImage
;
710 for (i
= 0; i
< 8 * 8; i
++) {
717 /* create texture object */
718 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, GL_TEXTURE_2D
);
719 assert(texObj
->RefCount
== 1);
720 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
721 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
723 /* create level[0] texture image */
724 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, 0);
726 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, GL_RGBA
, GL_RGBA
,
729 /* init the image fields */
730 _mesa_init_teximage_fields(ctx
, texImage
,
731 8, 8, 1, 0, GL_RGBA
, texFormat
);
733 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
736 ctx
->Driver
.TexImage2D(ctx
, texImage
, GL_RGBA
,
738 GL_RGBA
, GL_UNSIGNED_BYTE
, texels
,
739 &ctx
->DefaultPacking
);
741 _mesa_test_texobj_completeness(ctx
, texObj
);
742 assert(texObj
->_Complete
);
744 ctx
->Shared
->FallbackTex
= texObj
;
746 return ctx
->Shared
->FallbackTex
;
753 /***********************************************************************/
754 /** \name API functions */
759 * Generate texture names.
761 * \param n number of texture names to be generated.
762 * \param textures an array in which will hold the generated texture names.
764 * \sa glGenTextures().
766 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
767 * IDs which are stored in \p textures. Corresponding empty texture
768 * objects are also generated.
771 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
773 GET_CURRENT_CONTEXT(ctx
);
776 ASSERT_OUTSIDE_BEGIN_END(ctx
);
779 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
787 * This must be atomic (generation and allocation of texture IDs)
789 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
791 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
793 /* Allocate new, empty texture objects */
794 for (i
= 0; i
< n
; i
++) {
795 struct gl_texture_object
*texObj
;
796 GLuint name
= first
+ i
;
798 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
800 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
801 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
805 /* insert into hash table */
806 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
811 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
816 * Check if the given texture object is bound to the current draw or
817 * read framebuffer. If so, Unbind it.
820 unbind_texobj_from_fbo(struct gl_context
*ctx
,
821 struct gl_texture_object
*texObj
)
823 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
826 for (i
= 0; i
< n
; i
++) {
827 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
830 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
831 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
832 fb
->Attachment
[j
].Texture
== texObj
) {
833 /* Vertices are already flushed by _mesa_DeleteTextures */
834 ctx
->NewState
|= _NEW_BUFFERS
;
835 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
844 * Check if the given texture object is bound to any texture image units and
845 * unbind it if so (revert to default textures).
848 unbind_texobj_from_texunits(struct gl_context
*ctx
,
849 struct gl_texture_object
*texObj
)
853 for (u
= 0; u
< Elements(ctx
->Texture
.Unit
); u
++) {
854 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
855 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
856 if (texObj
== unit
->CurrentTex
[tex
]) {
857 _mesa_reference_texobj(&unit
->CurrentTex
[tex
],
858 ctx
->Shared
->DefaultTex
[tex
]);
859 ASSERT(unit
->CurrentTex
[tex
]);
868 * Delete named textures.
870 * \param n number of textures to be deleted.
871 * \param textures array of texture IDs to be deleted.
873 * \sa glDeleteTextures().
875 * If we're about to delete a texture that's currently bound to any
876 * texture unit, unbind the texture first. Decrement the reference
877 * count on the texture object and delete it if it's zero.
878 * Recall that texture objects can be shared among several rendering
882 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
884 GET_CURRENT_CONTEXT(ctx
);
886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
891 for (i
= 0; i
< n
; i
++) {
892 if (textures
[i
] > 0) {
893 struct gl_texture_object
*delObj
894 = _mesa_lookup_texture(ctx
, textures
[i
]);
897 _mesa_lock_texture(ctx
, delObj
);
899 /* Check if texture is bound to any framebuffer objects.
901 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
903 unbind_texobj_from_fbo(ctx
, delObj
);
905 /* Check if this texture is currently bound to any texture units.
908 unbind_texobj_from_texunits(ctx
, delObj
);
910 _mesa_unlock_texture(ctx
, delObj
);
912 ctx
->NewState
|= _NEW_TEXTURE
;
914 /* The texture _name_ is now free for re-use.
915 * Remove it from the hash table now.
917 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
918 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
919 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
921 /* Unreference the texobj. If refcount hits zero, the texture
924 _mesa_reference_texobj(&delObj
, NULL
);
932 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
933 * into the corresponding Mesa texture target index.
934 * Note that proxy targets are not valid here.
935 * \return TEXTURE_x_INDEX or -1 if target is invalid
938 target_enum_to_index(GLenum target
)
942 return TEXTURE_1D_INDEX
;
944 return TEXTURE_2D_INDEX
;
946 return TEXTURE_3D_INDEX
;
947 case GL_TEXTURE_CUBE_MAP_ARB
:
948 return TEXTURE_CUBE_INDEX
;
949 case GL_TEXTURE_1D_ARRAY_EXT
:
950 return TEXTURE_1D_ARRAY_INDEX
;
951 case GL_TEXTURE_2D_ARRAY_EXT
:
952 return TEXTURE_2D_ARRAY_INDEX
;
953 case GL_TEXTURE_BUFFER_ARB
:
954 return TEXTURE_BUFFER_INDEX
;
962 * Bind a named texture to a texturing target.
964 * \param target texture target.
965 * \param texName texture name.
967 * \sa glBindTexture().
969 * Determines the old texture object bound and returns immediately if rebinding
970 * the same texture. Get the current texture which is either a default texture
971 * if name is null, a named texture from the hash, or a new texture if the
972 * given texture name is new. Increments its reference count, binds it, and
973 * calls dd_function_table::BindTexture. Decrements the old texture reference
974 * count and deletes it if it reaches zero.
977 _mesa_BindTexture( GLenum target
, GLuint texName
)
979 GET_CURRENT_CONTEXT(ctx
);
980 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
981 struct gl_texture_object
*newTexObj
= NULL
;
983 ASSERT_OUTSIDE_BEGIN_END(ctx
);
985 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
986 _mesa_debug(ctx
, "glBindTexture %s %d\n",
987 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
989 targetIndex
= target_enum_to_index(target
);
990 if (targetIndex
< 0) {
991 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
994 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
997 * Get pointer to new texture object (newTexObj)
1000 /* Use a default texture object */
1001 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1004 /* non-default texture object */
1005 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1007 /* error checking */
1008 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1009 /* the named texture object's target doesn't match the given target */
1010 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1011 "glBindTexture(target mismatch)" );
1016 /* if this is a new texture id, allocate a texture object now */
1017 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1019 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1023 /* and insert it into hash table */
1024 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1025 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1026 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1028 newTexObj
->Target
= target
;
1031 assert(valid_texture_object(newTexObj
));
1033 /* Check if this texture is only used by this context and is already bound.
1034 * If so, just return.
1037 GLboolean early_out
;
1038 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1039 early_out
= ((ctx
->Shared
->RefCount
== 1)
1040 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1041 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1047 /* flush before changing binding */
1048 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1050 /* Do the actual binding. The refcount on the previously bound
1051 * texture object will be decremented. It'll be deleted if the
1054 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1055 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1057 /* Pass BindTexture call to device driver */
1058 if (ctx
->Driver
.BindTexture
)
1059 ctx
->Driver
.BindTexture(ctx
, target
, newTexObj
);
1064 * Set texture priorities.
1066 * \param n number of textures.
1067 * \param texName texture names.
1068 * \param priorities corresponding texture priorities.
1070 * \sa glPrioritizeTextures().
1072 * Looks up each texture in the hash, clamps the corresponding priority between
1073 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1076 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1077 const GLclampf
*priorities
)
1079 GET_CURRENT_CONTEXT(ctx
);
1081 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1084 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1091 for (i
= 0; i
< n
; i
++) {
1092 if (texName
[i
] > 0) {
1093 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1095 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1100 ctx
->NewState
|= _NEW_TEXTURE
;
1106 * See if textures are loaded in texture memory.
1108 * \param n number of textures to query.
1109 * \param texName array with the texture names.
1110 * \param residences array which will hold the residence status.
1112 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1114 * Note: we assume all textures are always resident
1116 GLboolean GLAPIENTRY
1117 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1118 GLboolean
*residences
)
1120 GET_CURRENT_CONTEXT(ctx
);
1121 GLboolean allResident
= GL_TRUE
;
1123 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1126 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1130 if (!texName
|| !residences
)
1133 /* We only do error checking on the texture names */
1134 for (i
= 0; i
< n
; i
++) {
1135 struct gl_texture_object
*t
;
1136 if (texName
[i
] == 0) {
1137 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1140 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1142 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1152 * See if a name corresponds to a texture.
1154 * \param texture texture name.
1156 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1159 * \sa glIsTexture().
1161 * Calls _mesa_HashLookup().
1163 GLboolean GLAPIENTRY
1164 _mesa_IsTexture( GLuint texture
)
1166 struct gl_texture_object
*t
;
1167 GET_CURRENT_CONTEXT(ctx
);
1168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1173 t
= _mesa_lookup_texture(ctx
, texture
);
1175 /* IsTexture is true only after object has been bound once. */
1176 return t
&& t
->Target
;
1181 * Simplest implementation of texture locking: grab the shared tex
1182 * mutex. Examine the shared context state timestamp and if there has
1183 * been a change, set the appropriate bits in ctx->NewState.
1185 * This is used to deal with synchronizing things when a texture object
1186 * is used/modified by different contexts (or threads) which are sharing
1189 * See also _mesa_lock/unlock_texture() in teximage.h
1192 _mesa_lock_context_textures( struct gl_context
*ctx
)
1194 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1196 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1197 ctx
->NewState
|= _NEW_TEXTURE
;
1198 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1204 _mesa_unlock_context_textures( struct gl_context
*ctx
)
1206 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1207 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);