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_RECTANGLE_NV
||
108 target
== GL_TEXTURE_1D_ARRAY_EXT
||
109 target
== GL_TEXTURE_2D_ARRAY_EXT
||
110 target
== GL_TEXTURE_EXTERNAL_OES
||
111 target
== GL_TEXTURE_BUFFER
);
113 memset(obj
, 0, sizeof(*obj
));
114 /* init the non-zero fields */
115 _glthread_INIT_MUTEX(obj
->Mutex
);
118 obj
->Target
= target
;
119 obj
->Priority
= 1.0F
;
121 obj
->MaxLevel
= 1000;
123 /* must be one; no support for (YUV) planes in separate buffers */
124 obj
->RequiredTextureImageUnits
= 1;
127 if (target
== GL_TEXTURE_RECTANGLE_NV
||
128 target
== GL_TEXTURE_EXTERNAL_OES
) {
129 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
130 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
131 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
132 obj
->Sampler
.MinFilter
= GL_LINEAR
;
135 obj
->Sampler
.WrapS
= GL_REPEAT
;
136 obj
->Sampler
.WrapT
= GL_REPEAT
;
137 obj
->Sampler
.WrapR
= GL_REPEAT
;
138 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
140 obj
->Sampler
.MagFilter
= GL_LINEAR
;
141 obj
->Sampler
.MinLod
= -1000.0;
142 obj
->Sampler
.MaxLod
= 1000.0;
143 obj
->Sampler
.LodBias
= 0.0;
144 obj
->Sampler
.MaxAnisotropy
= 1.0;
145 obj
->Sampler
.CompareMode
= GL_NONE
; /* ARB_shadow */
146 obj
->Sampler
.CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
147 obj
->Sampler
.CompareFailValue
= 0.0F
; /* ARB_shadow_ambient */
148 obj
->Sampler
.DepthMode
= GL_LUMINANCE
; /* ARB_depth_texture */
149 obj
->Sampler
.CubeMapSeamless
= GL_FALSE
;
150 obj
->Swizzle
[0] = GL_RED
;
151 obj
->Swizzle
[1] = GL_GREEN
;
152 obj
->Swizzle
[2] = GL_BLUE
;
153 obj
->Swizzle
[3] = GL_ALPHA
;
154 obj
->_Swizzle
= SWIZZLE_NOOP
;
155 obj
->Sampler
.sRGBDecode
= GL_DECODE_EXT
;
160 * Some texture initialization can't be finished until we know which
161 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
164 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
165 struct gl_texture_object
*obj
)
167 assert(obj
->Target
== 0);
169 if (target
== GL_TEXTURE_RECTANGLE_NV
||
170 target
== GL_TEXTURE_EXTERNAL_OES
) {
171 /* have to init wrap and filter state here - kind of klunky */
172 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
173 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
174 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
175 obj
->Sampler
.MinFilter
= GL_LINEAR
;
176 if (ctx
->Driver
.TexParameter
) {
177 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
178 static const GLfloat fparam_filter
[1] = {(GLfloat
) GL_LINEAR
};
179 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
180 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
181 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
182 ctx
->Driver
.TexParameter(ctx
, target
, obj
, GL_TEXTURE_MIN_FILTER
, fparam_filter
);
189 * Deallocate a texture object struct. It should have already been
190 * removed from the texture object pool.
191 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
193 * \param shared the shared GL state to which the object belongs.
194 * \param texObj the texture object to delete.
197 _mesa_delete_texture_object(struct gl_context
*ctx
,
198 struct gl_texture_object
*texObj
)
202 /* Set Target to an invalid value. With some assertions elsewhere
203 * we can try to detect possible use of deleted textures.
205 texObj
->Target
= 0x99;
207 /* free the texture images */
208 for (face
= 0; face
< 6; face
++) {
209 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
210 if (texObj
->Image
[face
][i
]) {
211 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
216 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
218 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
219 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
221 /* free this object */
228 * Copy texture object state from one texture object to another.
229 * Use for glPush/PopAttrib.
231 * \param dest destination texture object.
232 * \param src source texture object.
235 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
236 const struct gl_texture_object
*src
)
238 dest
->Target
= src
->Target
;
239 dest
->Name
= src
->Name
;
240 dest
->Priority
= src
->Priority
;
241 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
242 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
243 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
244 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
245 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
246 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
247 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
248 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
249 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
250 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
251 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
252 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
253 dest
->BaseLevel
= src
->BaseLevel
;
254 dest
->MaxLevel
= src
->MaxLevel
;
255 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
256 dest
->Sampler
.CompareMode
= src
->Sampler
.CompareMode
;
257 dest
->Sampler
.CompareFunc
= src
->Sampler
.CompareFunc
;
258 dest
->Sampler
.CompareFailValue
= src
->Sampler
.CompareFailValue
;
259 dest
->Sampler
.CubeMapSeamless
= src
->Sampler
.CubeMapSeamless
;
260 dest
->Sampler
.DepthMode
= src
->Sampler
.DepthMode
;
261 dest
->Sampler
.sRGBDecode
= src
->Sampler
.sRGBDecode
;
262 dest
->_MaxLevel
= src
->_MaxLevel
;
263 dest
->_MaxLambda
= src
->_MaxLambda
;
264 dest
->GenerateMipmap
= src
->GenerateMipmap
;
265 dest
->_Complete
= src
->_Complete
;
266 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
267 dest
->_Swizzle
= src
->_Swizzle
;
269 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
274 * Free all texture images of the given texture object.
276 * \param ctx GL context.
277 * \param t texture object.
279 * \sa _mesa_clear_texture_image().
282 _mesa_clear_texture_object(struct gl_context
*ctx
,
283 struct gl_texture_object
*texObj
)
287 if (texObj
->Target
== 0)
290 for (i
= 0; i
< MAX_FACES
; i
++) {
291 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
292 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
294 _mesa_clear_texture_image(ctx
, texImage
);
301 * Check if the given texture object is valid by examining its Target field.
302 * For debugging only.
305 valid_texture_object(const struct gl_texture_object
*tex
)
307 switch (tex
->Target
) {
312 case GL_TEXTURE_CUBE_MAP_ARB
:
313 case GL_TEXTURE_RECTANGLE_NV
:
314 case GL_TEXTURE_1D_ARRAY_EXT
:
315 case GL_TEXTURE_2D_ARRAY_EXT
:
316 case GL_TEXTURE_BUFFER
:
317 case GL_TEXTURE_EXTERNAL_OES
:
320 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
323 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
324 tex
->Target
, tex
->Name
);
331 * Reference (or unreference) a texture object.
332 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
333 * If 'tex' is non-null, increment its refcount.
334 * This is normally only called from the _mesa_reference_texobj() macro
335 * when there's a real pointer change.
338 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
339 struct gl_texture_object
*tex
)
344 /* Unreference the old texture */
345 GLboolean deleteFlag
= GL_FALSE
;
346 struct gl_texture_object
*oldTex
= *ptr
;
348 ASSERT(valid_texture_object(oldTex
));
349 (void) valid_texture_object
; /* silence warning in release builds */
351 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
352 ASSERT(oldTex
->RefCount
> 0);
355 deleteFlag
= (oldTex
->RefCount
== 0);
356 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
359 GET_CURRENT_CONTEXT(ctx
);
361 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
363 _mesa_problem(NULL
, "Unable to delete texture, no context");
371 /* reference new texture */
372 ASSERT(valid_texture_object(tex
));
373 _glthread_LOCK_MUTEX(tex
->Mutex
);
374 if (tex
->RefCount
== 0) {
375 /* this texture's being deleted (look just above) */
376 /* Not sure this can every really happen. Warn if it does. */
377 _mesa_problem(NULL
, "referencing deleted texture object");
384 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
391 * Mark a texture object as incomplete.
392 * \param t texture object
393 * \param fmt... string describing why it's incomplete (for debugging).
396 incomplete(struct gl_texture_object
*t
, const char *fmt
, ...)
403 vsnprintf(s
, sizeof(s
), fmt
, args
);
406 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
408 t
->_Complete
= GL_FALSE
;
413 * Examine a texture object to determine if it is complete.
415 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
418 * \param ctx GL context.
419 * \param t texture object.
421 * According to the texture target, verifies that each of the mipmaps is
422 * present and has the expected size.
425 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
426 struct gl_texture_object
*t
)
428 const GLint baseLevel
= t
->BaseLevel
;
429 GLint maxLog2
= 0, maxLevels
= 0;
431 t
->_Complete
= GL_TRUE
; /* be optimistic */
433 /* Detect cases where the application set the base level to an invalid
436 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
437 incomplete(t
, "base level = %d is invalid", baseLevel
);
441 /* Always need the base level image */
442 if (!t
->Image
[0][baseLevel
]) {
443 incomplete(t
, "Image[baseLevel=%d] == NULL", baseLevel
);
447 /* Check width/height/depth for zero */
448 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
449 t
->Image
[0][baseLevel
]->Height
== 0 ||
450 t
->Image
[0][baseLevel
]->Depth
== 0) {
451 incomplete(t
, "texture width = 0");
455 /* Compute _MaxLevel */
456 if ((t
->Target
== GL_TEXTURE_1D
) ||
457 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
458 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
459 maxLevels
= ctx
->Const
.MaxTextureLevels
;
461 else if ((t
->Target
== GL_TEXTURE_2D
) ||
462 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
463 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
464 t
->Image
[0][baseLevel
]->HeightLog2
);
465 maxLevels
= ctx
->Const
.MaxTextureLevels
;
467 else if (t
->Target
== GL_TEXTURE_3D
) {
468 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
469 t
->Image
[0][baseLevel
]->HeightLog2
);
470 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
471 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
473 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
474 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
475 t
->Image
[0][baseLevel
]->HeightLog2
);
476 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
478 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
||
479 t
->Target
== GL_TEXTURE_EXTERNAL_OES
) {
480 maxLog2
= 0; /* not applicable */
481 maxLevels
= 1; /* no mipmapping */
484 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
488 ASSERT(maxLevels
> 0);
490 if (t
->MaxLevel
< t
->BaseLevel
) {
491 incomplete(t
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
492 t
->MaxLevel
, t
->BaseLevel
);
496 t
->_MaxLevel
= baseLevel
+ maxLog2
;
497 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
498 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
500 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
501 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
504 /* This texture object was created with glTexStorage1/2/3D() so we
505 * know that all the mipmap levels are the right size and all cube
506 * map faces are the same size.
507 * We don't need to do any of the additional checks below.
512 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
513 /* make sure that all six cube map level 0 images are the same size */
514 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
515 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
517 for (face
= 1; face
< 6; face
++) {
518 if (t
->Image
[face
][baseLevel
] == NULL
||
519 t
->Image
[face
][baseLevel
]->Width2
!= w
||
520 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
521 incomplete(t
, "Cube face missing or mismatched size");
527 /* extra checking for mipmaps */
528 if (t
->Sampler
.MinFilter
!= GL_NEAREST
&& t
->Sampler
.MinFilter
!= GL_LINEAR
) {
530 * Mipmapping: determine if we have a complete set of mipmaps
533 GLint minLevel
= baseLevel
;
534 GLint maxLevel
= t
->_MaxLevel
;
536 if (minLevel
> maxLevel
) {
537 incomplete(t
, "minLevel > maxLevel");
541 /* Test dimension-independent attributes */
542 for (i
= minLevel
; i
<= maxLevel
; i
++) {
543 if (t
->Image
[0][i
]) {
544 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
545 incomplete(t
, "Format[i] != Format[baseLevel]");
548 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
549 incomplete(t
, "Border[i] != Border[baseLevel]");
555 /* Test things which depend on number of texture image dimensions */
556 if ((t
->Target
== GL_TEXTURE_1D
) ||
557 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
558 /* Test 1-D mipmaps */
559 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
560 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
564 if (i
>= minLevel
&& i
<= maxLevel
) {
565 const struct gl_texture_image
*img
= t
->Image
[0][i
];
567 incomplete(t
, "1D Image[%d] is missing", i
);
570 if (img
->Width2
!= width
) {
571 incomplete(t
, "1D Image[%d] bad width %u", i
, img
->Width2
);
576 return; /* found smallest needed mipmap, all done! */
580 else if ((t
->Target
== GL_TEXTURE_2D
) ||
581 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
582 /* Test 2-D mipmaps */
583 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
584 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
585 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
592 if (i
>= minLevel
&& i
<= maxLevel
) {
593 const struct gl_texture_image
*img
= t
->Image
[0][i
];
595 incomplete(t
, "2D Image[%d of %d] is missing", i
, maxLevel
);
598 if (img
->Width2
!= width
) {
599 incomplete(t
, "2D Image[%d] bad width %u", i
, img
->Width2
);
602 if (img
->Height2
!= height
) {
603 incomplete(t
, "2D Image[i] bad height %u", i
, img
->Height2
);
606 if (width
==1 && height
==1) {
607 return; /* found smallest needed mipmap, all done! */
612 else if (t
->Target
== GL_TEXTURE_3D
) {
613 /* Test 3-D mipmaps */
614 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
615 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
616 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
617 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
627 if (i
>= minLevel
&& i
<= maxLevel
) {
628 const struct gl_texture_image
*img
= t
->Image
[0][i
];
630 incomplete(t
, "3D Image[%d] is missing", i
);
633 if (img
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
634 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
637 if (img
->Width2
!= width
) {
638 incomplete(t
, "3D Image[%d] bad width %u", i
, img
->Width2
);
641 if (img
->Height2
!= height
) {
642 incomplete(t
, "3D Image[%d] bad height %u", i
, img
->Height2
);
645 if (img
->Depth2
!= depth
) {
646 incomplete(t
, "3D Image[%d] bad depth %u", i
, img
->Depth2
);
650 if (width
== 1 && height
== 1 && depth
== 1) {
651 return; /* found smallest needed mipmap, all done! */
655 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
656 /* make sure 6 cube faces are consistant */
657 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
658 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
659 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
666 if (i
>= minLevel
&& i
<= maxLevel
) {
668 for (face
= 0; face
< 6; face
++) {
669 /* check that we have images defined */
670 if (!t
->Image
[face
][i
]) {
671 incomplete(t
, "CubeMap Image[n][i] == NULL");
674 /* Don't support GL_DEPTH_COMPONENT for cube maps */
675 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
676 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
679 /* check that all six images have same size */
680 if (t
->Image
[face
][i
]->Width2
!= width
||
681 t
->Image
[face
][i
]->Height2
!= height
) {
682 incomplete(t
, "CubeMap Image[n][i] bad size");
687 if (width
== 1 && height
== 1) {
688 return; /* found smallest needed mipmap, all done! */
692 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
693 /* XXX special checking? */
697 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
704 * Check if the given cube map texture is "cube complete" as defined in
705 * the OpenGL specification.
708 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
710 const GLint baseLevel
= texObj
->BaseLevel
;
711 const struct gl_texture_image
*img0
, *img
;
714 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
717 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
))
720 /* check first face */
721 img0
= texObj
->Image
[0][baseLevel
];
724 img0
->Width
!= img0
->Height
)
727 /* check remaining faces vs. first face */
728 for (face
= 1; face
< 6; face
++) {
729 img
= texObj
->Image
[face
][baseLevel
];
731 img
->Width
!= img0
->Width
||
732 img
->Height
!= img0
->Height
||
733 img
->TexFormat
!= img0
->TexFormat
)
742 * Mark a texture object dirty. It forces the object to be incomplete
743 * and optionally forces the context to re-validate its state.
745 * \param ctx GL context.
746 * \param texObj texture object.
747 * \param invalidate_state also invalidate context state.
750 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
751 GLboolean invalidate_state
)
753 texObj
->_Complete
= GL_FALSE
;
754 if (invalidate_state
)
755 ctx
->NewState
|= _NEW_TEXTURE
;
760 * Return pointer to a default/fallback texture.
761 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
762 * That's the value a sampler should get when sampling from an
763 * incomplete texture.
765 struct gl_texture_object
*
766 _mesa_get_fallback_texture(struct gl_context
*ctx
)
768 if (!ctx
->Shared
->FallbackTex
) {
769 /* create fallback texture now */
770 static GLubyte texels
[8 * 8][4];
771 struct gl_texture_object
*texObj
;
772 struct gl_texture_image
*texImage
;
776 for (i
= 0; i
< 8 * 8; i
++) {
783 /* create texture object */
784 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, GL_TEXTURE_2D
);
785 assert(texObj
->RefCount
== 1);
786 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
787 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
789 /* create level[0] texture image */
790 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, 0);
792 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, GL_RGBA
, GL_RGBA
,
795 /* init the image fields */
796 _mesa_init_teximage_fields(ctx
, GL_TEXTURE_2D
, texImage
,
797 8, 8, 1, 0, GL_RGBA
, texFormat
);
799 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
802 ctx
->Driver
.TexImage2D(ctx
, GL_TEXTURE_2D
, 0, GL_RGBA
,
804 GL_RGBA
, GL_UNSIGNED_BYTE
, texels
,
805 &ctx
->DefaultPacking
, texObj
, texImage
);
807 _mesa_test_texobj_completeness(ctx
, texObj
);
808 assert(texObj
->_Complete
);
810 ctx
->Shared
->FallbackTex
= texObj
;
812 return ctx
->Shared
->FallbackTex
;
819 /***********************************************************************/
820 /** \name API functions */
825 * Generate texture names.
827 * \param n number of texture names to be generated.
828 * \param textures an array in which will hold the generated texture names.
830 * \sa glGenTextures().
832 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
833 * IDs which are stored in \p textures. Corresponding empty texture
834 * objects are also generated.
837 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
839 GET_CURRENT_CONTEXT(ctx
);
842 ASSERT_OUTSIDE_BEGIN_END(ctx
);
845 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
853 * This must be atomic (generation and allocation of texture IDs)
855 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
857 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
859 /* Allocate new, empty texture objects */
860 for (i
= 0; i
< n
; i
++) {
861 struct gl_texture_object
*texObj
;
862 GLuint name
= first
+ i
;
864 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
866 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
867 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
871 /* insert into hash table */
872 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
877 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
882 * Check if the given texture object is bound to the current draw or
883 * read framebuffer. If so, Unbind it.
886 unbind_texobj_from_fbo(struct gl_context
*ctx
,
887 struct gl_texture_object
*texObj
)
889 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
892 for (i
= 0; i
< n
; i
++) {
893 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
896 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
897 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
898 fb
->Attachment
[j
].Texture
== texObj
) {
899 /* Vertices are already flushed by _mesa_DeleteTextures */
900 ctx
->NewState
|= _NEW_BUFFERS
;
901 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
910 * Check if the given texture object is bound to any texture image units and
911 * unbind it if so (revert to default textures).
914 unbind_texobj_from_texunits(struct gl_context
*ctx
,
915 struct gl_texture_object
*texObj
)
919 for (u
= 0; u
< Elements(ctx
->Texture
.Unit
); u
++) {
920 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
921 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
922 if (texObj
== unit
->CurrentTex
[tex
]) {
923 _mesa_reference_texobj(&unit
->CurrentTex
[tex
],
924 ctx
->Shared
->DefaultTex
[tex
]);
925 ASSERT(unit
->CurrentTex
[tex
]);
934 * Delete named textures.
936 * \param n number of textures to be deleted.
937 * \param textures array of texture IDs to be deleted.
939 * \sa glDeleteTextures().
941 * If we're about to delete a texture that's currently bound to any
942 * texture unit, unbind the texture first. Decrement the reference
943 * count on the texture object and delete it if it's zero.
944 * Recall that texture objects can be shared among several rendering
948 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
950 GET_CURRENT_CONTEXT(ctx
);
952 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
957 for (i
= 0; i
< n
; i
++) {
958 if (textures
[i
] > 0) {
959 struct gl_texture_object
*delObj
960 = _mesa_lookup_texture(ctx
, textures
[i
]);
963 _mesa_lock_texture(ctx
, delObj
);
965 /* Check if texture is bound to any framebuffer objects.
967 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
969 unbind_texobj_from_fbo(ctx
, delObj
);
971 /* Check if this texture is currently bound to any texture units.
974 unbind_texobj_from_texunits(ctx
, delObj
);
976 _mesa_unlock_texture(ctx
, delObj
);
978 ctx
->NewState
|= _NEW_TEXTURE
;
980 /* The texture _name_ is now free for re-use.
981 * Remove it from the hash table now.
983 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
984 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
985 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
987 /* Unreference the texobj. If refcount hits zero, the texture
990 _mesa_reference_texobj(&delObj
, NULL
);
998 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
999 * into the corresponding Mesa texture target index.
1000 * Note that proxy targets are not valid here.
1001 * \return TEXTURE_x_INDEX or -1 if target is invalid
1004 target_enum_to_index(GLenum target
)
1008 return TEXTURE_1D_INDEX
;
1010 return TEXTURE_2D_INDEX
;
1012 return TEXTURE_3D_INDEX
;
1013 case GL_TEXTURE_CUBE_MAP_ARB
:
1014 return TEXTURE_CUBE_INDEX
;
1015 case GL_TEXTURE_RECTANGLE_NV
:
1016 return TEXTURE_RECT_INDEX
;
1017 case GL_TEXTURE_1D_ARRAY_EXT
:
1018 return TEXTURE_1D_ARRAY_INDEX
;
1019 case GL_TEXTURE_2D_ARRAY_EXT
:
1020 return TEXTURE_2D_ARRAY_INDEX
;
1021 case GL_TEXTURE_BUFFER_ARB
:
1022 return TEXTURE_BUFFER_INDEX
;
1023 case GL_TEXTURE_EXTERNAL_OES
:
1024 return TEXTURE_EXTERNAL_INDEX
;
1032 * Bind a named texture to a texturing target.
1034 * \param target texture target.
1035 * \param texName texture name.
1037 * \sa glBindTexture().
1039 * Determines the old texture object bound and returns immediately if rebinding
1040 * the same texture. Get the current texture which is either a default texture
1041 * if name is null, a named texture from the hash, or a new texture if the
1042 * given texture name is new. Increments its reference count, binds it, and
1043 * calls dd_function_table::BindTexture. Decrements the old texture reference
1044 * count and deletes it if it reaches zero.
1047 _mesa_BindTexture( GLenum target
, GLuint texName
)
1049 GET_CURRENT_CONTEXT(ctx
);
1050 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
1051 struct gl_texture_object
*newTexObj
= NULL
;
1053 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1055 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1056 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1057 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1059 targetIndex
= target_enum_to_index(target
);
1060 if (targetIndex
< 0) {
1061 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1064 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1067 * Get pointer to new texture object (newTexObj)
1070 /* Use a default texture object */
1071 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1074 /* non-default texture object */
1075 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1077 /* error checking */
1078 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1079 /* the named texture object's target doesn't match the given target */
1080 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1081 "glBindTexture(target mismatch)" );
1084 if (newTexObj
->Target
== 0) {
1085 finish_texture_init(ctx
, target
, newTexObj
);
1089 /* if this is a new texture id, allocate a texture object now */
1090 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1092 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1096 /* and insert it into hash table */
1097 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1098 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1099 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1101 newTexObj
->Target
= target
;
1104 assert(valid_texture_object(newTexObj
));
1106 /* Check if this texture is only used by this context and is already bound.
1107 * If so, just return.
1110 GLboolean early_out
;
1111 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1112 early_out
= ((ctx
->Shared
->RefCount
== 1)
1113 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1114 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1120 /* flush before changing binding */
1121 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1123 /* Do the actual binding. The refcount on the previously bound
1124 * texture object will be decremented. It'll be deleted if the
1127 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1128 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1130 /* Pass BindTexture call to device driver */
1131 if (ctx
->Driver
.BindTexture
)
1132 ctx
->Driver
.BindTexture(ctx
, target
, newTexObj
);
1137 * Set texture priorities.
1139 * \param n number of textures.
1140 * \param texName texture names.
1141 * \param priorities corresponding texture priorities.
1143 * \sa glPrioritizeTextures().
1145 * Looks up each texture in the hash, clamps the corresponding priority between
1146 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1149 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1150 const GLclampf
*priorities
)
1152 GET_CURRENT_CONTEXT(ctx
);
1154 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1157 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1164 for (i
= 0; i
< n
; i
++) {
1165 if (texName
[i
] > 0) {
1166 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1168 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1173 ctx
->NewState
|= _NEW_TEXTURE
;
1179 * See if textures are loaded in texture memory.
1181 * \param n number of textures to query.
1182 * \param texName array with the texture names.
1183 * \param residences array which will hold the residence status.
1185 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1187 * \sa glAreTexturesResident().
1189 * Looks up each texture in the hash and calls
1190 * dd_function_table::IsTextureResident.
1192 GLboolean GLAPIENTRY
1193 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1194 GLboolean
*residences
)
1196 GET_CURRENT_CONTEXT(ctx
);
1197 GLboolean allResident
= GL_TRUE
;
1199 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1202 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1206 if (!texName
|| !residences
)
1209 for (i
= 0; i
< n
; i
++) {
1210 struct gl_texture_object
*t
;
1211 if (texName
[i
] == 0) {
1212 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1215 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1217 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1220 if (!ctx
->Driver
.IsTextureResident
||
1221 ctx
->Driver
.IsTextureResident(ctx
, t
)) {
1222 /* The texture is resident */
1224 residences
[i
] = GL_TRUE
;
1227 /* The texture is not resident */
1229 allResident
= GL_FALSE
;
1230 for (j
= 0; j
< i
; j
++)
1231 residences
[j
] = GL_TRUE
;
1233 residences
[i
] = GL_FALSE
;
1242 * See if a name corresponds to a texture.
1244 * \param texture texture name.
1246 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1249 * \sa glIsTexture().
1251 * Calls _mesa_HashLookup().
1253 GLboolean GLAPIENTRY
1254 _mesa_IsTexture( GLuint texture
)
1256 struct gl_texture_object
*t
;
1257 GET_CURRENT_CONTEXT(ctx
);
1258 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1263 t
= _mesa_lookup_texture(ctx
, texture
);
1265 /* IsTexture is true only after object has been bound once. */
1266 return t
&& t
->Target
;
1271 * Simplest implementation of texture locking: grab the shared tex
1272 * mutex. Examine the shared context state timestamp and if there has
1273 * been a change, set the appropriate bits in ctx->NewState.
1275 * This is used to deal with synchronizing things when a texture object
1276 * is used/modified by different contexts (or threads) which are sharing
1279 * See also _mesa_lock/unlock_texture() in teximage.h
1282 _mesa_lock_context_textures( struct gl_context
*ctx
)
1284 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1286 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1287 ctx
->NewState
|= _NEW_TEXTURE
;
1288 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1294 _mesa_unlock_context_textures( struct gl_context
*ctx
)
1296 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1297 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);