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;
149 * Some texture initialization can't be finished until we know which
150 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
153 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
154 struct gl_texture_object
*obj
)
156 assert(obj
->Target
== 0);
158 if (target
== GL_TEXTURE_RECTANGLE_NV
||
159 target
== GL_TEXTURE_EXTERNAL_OES
) {
160 /* have to init wrap and filter state here - kind of klunky */
161 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
162 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
163 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
164 obj
->Sampler
.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(struct gl_context
*ctx
,
187 struct gl_texture_object
*texObj
)
191 /* Set Target to an invalid value. With some assertions elsewhere
192 * we can try to detect possible use of deleted textures.
194 texObj
->Target
= 0x99;
196 /* free the texture images */
197 for (face
= 0; face
< 6; face
++) {
198 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
199 if (texObj
->Image
[face
][i
]) {
200 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
205 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
207 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
208 _glthread_DESTROY_MUTEX(texObj
->Mutex
);
210 /* free this object */
217 * Copy texture object state from one texture object to another.
218 * Use for glPush/PopAttrib.
220 * \param dest destination texture object.
221 * \param src source texture object.
224 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
225 const struct gl_texture_object
*src
)
227 dest
->Target
= src
->Target
;
228 dest
->Name
= src
->Name
;
229 dest
->Priority
= src
->Priority
;
230 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
231 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
232 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
233 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
234 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
235 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
236 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
237 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
238 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
239 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
240 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
241 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
242 dest
->BaseLevel
= src
->BaseLevel
;
243 dest
->MaxLevel
= src
->MaxLevel
;
244 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
245 dest
->_MaxLevel
= src
->_MaxLevel
;
246 dest
->_MaxLambda
= src
->_MaxLambda
;
247 dest
->GenerateMipmap
= src
->GenerateMipmap
;
248 dest
->_Complete
= src
->_Complete
;
250 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
255 * Free all texture images of the given texture object.
257 * \param ctx GL context.
258 * \param t texture object.
260 * \sa _mesa_clear_texture_image().
263 _mesa_clear_texture_object(struct gl_context
*ctx
,
264 struct gl_texture_object
*texObj
)
268 if (texObj
->Target
== 0)
271 for (i
= 0; i
< MAX_FACES
; i
++) {
272 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
273 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
275 _mesa_clear_texture_image(ctx
, texImage
);
282 * Check if the given texture object is valid by examining its Target field.
283 * For debugging only.
286 valid_texture_object(const struct gl_texture_object
*tex
)
288 switch (tex
->Target
) {
293 case GL_TEXTURE_CUBE_MAP_ARB
:
294 case GL_TEXTURE_RECTANGLE_NV
:
295 case GL_TEXTURE_1D_ARRAY_EXT
:
296 case GL_TEXTURE_2D_ARRAY_EXT
:
297 case GL_TEXTURE_BUFFER
:
298 case GL_TEXTURE_EXTERNAL_OES
:
301 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
304 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
305 tex
->Target
, tex
->Name
);
312 * Reference (or unreference) a texture object.
313 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
314 * If 'tex' is non-null, increment its refcount.
315 * This is normally only called from the _mesa_reference_texobj() macro
316 * when there's a real pointer change.
319 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
320 struct gl_texture_object
*tex
)
325 /* Unreference the old texture */
326 GLboolean deleteFlag
= GL_FALSE
;
327 struct gl_texture_object
*oldTex
= *ptr
;
329 ASSERT(valid_texture_object(oldTex
));
330 (void) valid_texture_object
; /* silence warning in release builds */
332 _glthread_LOCK_MUTEX(oldTex
->Mutex
);
333 ASSERT(oldTex
->RefCount
> 0);
336 deleteFlag
= (oldTex
->RefCount
== 0);
337 _glthread_UNLOCK_MUTEX(oldTex
->Mutex
);
340 GET_CURRENT_CONTEXT(ctx
);
342 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
344 _mesa_problem(NULL
, "Unable to delete texture, no context");
352 /* reference new texture */
353 ASSERT(valid_texture_object(tex
));
354 _glthread_LOCK_MUTEX(tex
->Mutex
);
355 if (tex
->RefCount
== 0) {
356 /* this texture's being deleted (look just above) */
357 /* Not sure this can every really happen. Warn if it does. */
358 _mesa_problem(NULL
, "referencing deleted texture object");
365 _glthread_UNLOCK_MUTEX(tex
->Mutex
);
372 * Mark a texture object as incomplete.
373 * \param t texture object
374 * \param fmt... string describing why it's incomplete (for debugging).
377 incomplete(struct gl_texture_object
*t
, const char *fmt
, ...)
384 vsnprintf(s
, sizeof(s
), fmt
, args
);
387 printf("Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
389 t
->_Complete
= GL_FALSE
;
394 * Examine a texture object to determine if it is complete.
396 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
399 * \param ctx GL context.
400 * \param t texture object.
402 * According to the texture target, verifies that each of the mipmaps is
403 * present and has the expected size.
406 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
407 struct gl_texture_object
*t
)
409 const GLint baseLevel
= t
->BaseLevel
;
410 GLint maxLog2
= 0, maxLevels
= 0;
412 t
->_Complete
= GL_TRUE
; /* be optimistic */
414 /* Detect cases where the application set the base level to an invalid
417 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
418 incomplete(t
, "base level = %d is invalid", baseLevel
);
422 /* Always need the base level image */
423 if (!t
->Image
[0][baseLevel
]) {
424 incomplete(t
, "Image[baseLevel=%d] == NULL", baseLevel
);
428 /* Check width/height/depth for zero */
429 if (t
->Image
[0][baseLevel
]->Width
== 0 ||
430 t
->Image
[0][baseLevel
]->Height
== 0 ||
431 t
->Image
[0][baseLevel
]->Depth
== 0) {
432 incomplete(t
, "texture width = 0");
436 /* Compute _MaxLevel */
437 if ((t
->Target
== GL_TEXTURE_1D
) ||
438 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
439 maxLog2
= t
->Image
[0][baseLevel
]->WidthLog2
;
440 maxLevels
= ctx
->Const
.MaxTextureLevels
;
442 else if ((t
->Target
== GL_TEXTURE_2D
) ||
443 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
444 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
445 t
->Image
[0][baseLevel
]->HeightLog2
);
446 maxLevels
= ctx
->Const
.MaxTextureLevels
;
448 else if (t
->Target
== GL_TEXTURE_3D
) {
449 GLint max
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
450 t
->Image
[0][baseLevel
]->HeightLog2
);
451 maxLog2
= MAX2(max
, (GLint
)(t
->Image
[0][baseLevel
]->DepthLog2
));
452 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
454 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
455 maxLog2
= MAX2(t
->Image
[0][baseLevel
]->WidthLog2
,
456 t
->Image
[0][baseLevel
]->HeightLog2
);
457 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
459 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
||
460 t
->Target
== GL_TEXTURE_EXTERNAL_OES
) {
461 maxLog2
= 0; /* not applicable */
462 maxLevels
= 1; /* no mipmapping */
465 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
469 ASSERT(maxLevels
> 0);
471 if (t
->MaxLevel
< t
->BaseLevel
) {
472 incomplete(t
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
473 t
->MaxLevel
, t
->BaseLevel
);
477 t
->_MaxLevel
= baseLevel
+ maxLog2
;
478 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->MaxLevel
);
479 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, maxLevels
- 1);
481 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
482 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- t
->BaseLevel
);
485 /* This texture object was created with glTexStorage1/2/3D() so we
486 * know that all the mipmap levels are the right size and all cube
487 * map faces are the same size.
488 * We don't need to do any of the additional checks below.
493 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
494 /* make sure that all six cube map level 0 images are the same size */
495 const GLuint w
= t
->Image
[0][baseLevel
]->Width2
;
496 const GLuint h
= t
->Image
[0][baseLevel
]->Height2
;
498 for (face
= 1; face
< 6; face
++) {
499 if (t
->Image
[face
][baseLevel
] == NULL
||
500 t
->Image
[face
][baseLevel
]->Width2
!= w
||
501 t
->Image
[face
][baseLevel
]->Height2
!= h
) {
502 incomplete(t
, "Cube face missing or mismatched size");
508 /* extra checking for mipmaps */
509 if (t
->Sampler
.MinFilter
!= GL_NEAREST
&& t
->Sampler
.MinFilter
!= GL_LINEAR
) {
511 * Mipmapping: determine if we have a complete set of mipmaps
514 GLint minLevel
= baseLevel
;
515 GLint maxLevel
= t
->_MaxLevel
;
517 if (minLevel
> maxLevel
) {
518 incomplete(t
, "minLevel > maxLevel");
522 /* Test dimension-independent attributes */
523 for (i
= minLevel
; i
<= maxLevel
; i
++) {
524 if (t
->Image
[0][i
]) {
525 if (t
->Image
[0][i
]->TexFormat
!= t
->Image
[0][baseLevel
]->TexFormat
) {
526 incomplete(t
, "Format[i] != Format[baseLevel]");
529 if (t
->Image
[0][i
]->Border
!= t
->Image
[0][baseLevel
]->Border
) {
530 incomplete(t
, "Border[i] != Border[baseLevel]");
536 /* Test things which depend on number of texture image dimensions */
537 if ((t
->Target
== GL_TEXTURE_1D
) ||
538 (t
->Target
== GL_TEXTURE_1D_ARRAY_EXT
)) {
539 /* Test 1-D mipmaps */
540 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
541 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
545 if (i
>= minLevel
&& i
<= maxLevel
) {
546 const struct gl_texture_image
*img
= t
->Image
[0][i
];
548 incomplete(t
, "1D Image[%d] is missing", i
);
551 if (img
->Width2
!= width
) {
552 incomplete(t
, "1D Image[%d] bad width %u", i
, img
->Width2
);
557 return; /* found smallest needed mipmap, all done! */
561 else if ((t
->Target
== GL_TEXTURE_2D
) ||
562 (t
->Target
== GL_TEXTURE_2D_ARRAY_EXT
)) {
563 /* Test 2-D mipmaps */
564 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
565 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
566 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
573 if (i
>= minLevel
&& i
<= maxLevel
) {
574 const struct gl_texture_image
*img
= t
->Image
[0][i
];
576 incomplete(t
, "2D Image[%d of %d] is missing", i
, maxLevel
);
579 if (img
->Width2
!= width
) {
580 incomplete(t
, "2D Image[%d] bad width %u", i
, img
->Width2
);
583 if (img
->Height2
!= height
) {
584 incomplete(t
, "2D Image[i] bad height %u", i
, img
->Height2
);
587 if (width
==1 && height
==1) {
588 return; /* found smallest needed mipmap, all done! */
593 else if (t
->Target
== GL_TEXTURE_3D
) {
594 /* Test 3-D mipmaps */
595 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
596 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
597 GLuint depth
= t
->Image
[0][baseLevel
]->Depth2
;
598 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
608 if (i
>= minLevel
&& i
<= maxLevel
) {
609 const struct gl_texture_image
*img
= t
->Image
[0][i
];
611 incomplete(t
, "3D Image[%d] is missing", i
);
614 if (img
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
615 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
618 if (img
->Width2
!= width
) {
619 incomplete(t
, "3D Image[%d] bad width %u", i
, img
->Width2
);
622 if (img
->Height2
!= height
) {
623 incomplete(t
, "3D Image[%d] bad height %u", i
, img
->Height2
);
626 if (img
->Depth2
!= depth
) {
627 incomplete(t
, "3D Image[%d] bad depth %u", i
, img
->Depth2
);
631 if (width
== 1 && height
== 1 && depth
== 1) {
632 return; /* found smallest needed mipmap, all done! */
636 else if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
637 /* make sure 6 cube faces are consistant */
638 GLuint width
= t
->Image
[0][baseLevel
]->Width2
;
639 GLuint height
= t
->Image
[0][baseLevel
]->Height2
;
640 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
647 if (i
>= minLevel
&& i
<= maxLevel
) {
649 for (face
= 0; face
< 6; face
++) {
650 /* check that we have images defined */
651 if (!t
->Image
[face
][i
]) {
652 incomplete(t
, "CubeMap Image[n][i] == NULL");
655 /* Don't support GL_DEPTH_COMPONENT for cube maps */
656 if (ctx
->VersionMajor
< 3 && !ctx
->Extensions
.EXT_gpu_shader4
) {
657 if (t
->Image
[face
][i
]->_BaseFormat
== GL_DEPTH_COMPONENT
) {
658 incomplete(t
, "GL_DEPTH_COMPONENT only works with 1/2D tex");
662 /* check that all six images have same size */
663 if (t
->Image
[face
][i
]->Width2
!= width
||
664 t
->Image
[face
][i
]->Height2
!= height
) {
665 incomplete(t
, "CubeMap Image[n][i] bad size");
670 if (width
== 1 && height
== 1) {
671 return; /* found smallest needed mipmap, all done! */
675 else if (t
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
676 /* XXX special checking? */
680 _mesa_problem(ctx
, "Bug in gl_test_texture_object_completeness\n");
687 * Check if the given cube map texture is "cube complete" as defined in
688 * the OpenGL specification.
691 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
693 const GLint baseLevel
= texObj
->BaseLevel
;
694 const struct gl_texture_image
*img0
, *img
;
697 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
700 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
))
703 /* check first face */
704 img0
= texObj
->Image
[0][baseLevel
];
707 img0
->Width
!= img0
->Height
)
710 /* check remaining faces vs. first face */
711 for (face
= 1; face
< 6; face
++) {
712 img
= texObj
->Image
[face
][baseLevel
];
714 img
->Width
!= img0
->Width
||
715 img
->Height
!= img0
->Height
||
716 img
->TexFormat
!= img0
->TexFormat
)
725 * Mark a texture object dirty. It forces the object to be incomplete
726 * and optionally forces the context to re-validate its state.
728 * \param ctx GL context.
729 * \param texObj texture object.
730 * \param invalidate_state also invalidate context state.
733 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
734 GLboolean invalidate_state
)
736 texObj
->_Complete
= GL_FALSE
;
737 if (invalidate_state
)
738 ctx
->NewState
|= _NEW_TEXTURE
;
743 * Return pointer to a default/fallback texture.
744 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
745 * That's the value a sampler should get when sampling from an
746 * incomplete texture.
748 struct gl_texture_object
*
749 _mesa_get_fallback_texture(struct gl_context
*ctx
)
751 if (!ctx
->Shared
->FallbackTex
) {
752 /* create fallback texture now */
753 static GLubyte texels
[8 * 8][4];
754 struct gl_texture_object
*texObj
;
755 struct gl_texture_image
*texImage
;
759 for (i
= 0; i
< 8 * 8; i
++) {
766 /* create texture object */
767 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, GL_TEXTURE_2D
);
768 assert(texObj
->RefCount
== 1);
769 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
770 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
772 /* create level[0] texture image */
773 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, 0);
775 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, GL_RGBA
, GL_RGBA
,
778 /* init the image fields */
779 _mesa_init_teximage_fields(ctx
, texImage
,
780 8, 8, 1, 0, GL_RGBA
, texFormat
);
782 ASSERT(texImage
->TexFormat
!= MESA_FORMAT_NONE
);
785 ctx
->Driver
.TexImage2D(ctx
, texImage
, GL_RGBA
,
787 GL_RGBA
, GL_UNSIGNED_BYTE
, texels
,
788 &ctx
->DefaultPacking
);
790 _mesa_test_texobj_completeness(ctx
, texObj
);
791 assert(texObj
->_Complete
);
793 ctx
->Shared
->FallbackTex
= texObj
;
795 return ctx
->Shared
->FallbackTex
;
802 /***********************************************************************/
803 /** \name API functions */
808 * Generate texture names.
810 * \param n number of texture names to be generated.
811 * \param textures an array in which will hold the generated texture names.
813 * \sa glGenTextures().
815 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
816 * IDs which are stored in \p textures. Corresponding empty texture
817 * objects are also generated.
820 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
822 GET_CURRENT_CONTEXT(ctx
);
825 ASSERT_OUTSIDE_BEGIN_END(ctx
);
828 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
836 * This must be atomic (generation and allocation of texture IDs)
838 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
840 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
842 /* Allocate new, empty texture objects */
843 for (i
= 0; i
< n
; i
++) {
844 struct gl_texture_object
*texObj
;
845 GLuint name
= first
+ i
;
847 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
849 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
850 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
854 /* insert into hash table */
855 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
860 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
865 * Check if the given texture object is bound to the current draw or
866 * read framebuffer. If so, Unbind it.
869 unbind_texobj_from_fbo(struct gl_context
*ctx
,
870 struct gl_texture_object
*texObj
)
872 const GLuint n
= (ctx
->DrawBuffer
== ctx
->ReadBuffer
) ? 1 : 2;
875 for (i
= 0; i
< n
; i
++) {
876 struct gl_framebuffer
*fb
= (i
== 0) ? ctx
->DrawBuffer
: ctx
->ReadBuffer
;
879 for (j
= 0; j
< BUFFER_COUNT
; j
++) {
880 if (fb
->Attachment
[j
].Type
== GL_TEXTURE
&&
881 fb
->Attachment
[j
].Texture
== texObj
) {
882 /* Vertices are already flushed by _mesa_DeleteTextures */
883 ctx
->NewState
|= _NEW_BUFFERS
;
884 _mesa_remove_attachment(ctx
, fb
->Attachment
+ j
);
893 * Check if the given texture object is bound to any texture image units and
894 * unbind it if so (revert to default textures).
897 unbind_texobj_from_texunits(struct gl_context
*ctx
,
898 struct gl_texture_object
*texObj
)
902 for (u
= 0; u
< Elements(ctx
->Texture
.Unit
); u
++) {
903 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
904 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
905 if (texObj
== unit
->CurrentTex
[tex
]) {
906 _mesa_reference_texobj(&unit
->CurrentTex
[tex
],
907 ctx
->Shared
->DefaultTex
[tex
]);
908 ASSERT(unit
->CurrentTex
[tex
]);
917 * Delete named textures.
919 * \param n number of textures to be deleted.
920 * \param textures array of texture IDs to be deleted.
922 * \sa glDeleteTextures().
924 * If we're about to delete a texture that's currently bound to any
925 * texture unit, unbind the texture first. Decrement the reference
926 * count on the texture object and delete it if it's zero.
927 * Recall that texture objects can be shared among several rendering
931 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
933 GET_CURRENT_CONTEXT(ctx
);
935 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex */
940 for (i
= 0; i
< n
; i
++) {
941 if (textures
[i
] > 0) {
942 struct gl_texture_object
*delObj
943 = _mesa_lookup_texture(ctx
, textures
[i
]);
946 _mesa_lock_texture(ctx
, delObj
);
948 /* Check if texture is bound to any framebuffer objects.
950 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
952 unbind_texobj_from_fbo(ctx
, delObj
);
954 /* Check if this texture is currently bound to any texture units.
957 unbind_texobj_from_texunits(ctx
, delObj
);
959 _mesa_unlock_texture(ctx
, delObj
);
961 ctx
->NewState
|= _NEW_TEXTURE
;
963 /* The texture _name_ is now free for re-use.
964 * Remove it from the hash table now.
966 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
967 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
968 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
970 /* Unreference the texobj. If refcount hits zero, the texture
973 _mesa_reference_texobj(&delObj
, NULL
);
981 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
982 * into the corresponding Mesa texture target index.
983 * Note that proxy targets are not valid here.
984 * \return TEXTURE_x_INDEX or -1 if target is invalid
987 target_enum_to_index(GLenum target
)
991 return TEXTURE_1D_INDEX
;
993 return TEXTURE_2D_INDEX
;
995 return TEXTURE_3D_INDEX
;
996 case GL_TEXTURE_CUBE_MAP_ARB
:
997 return TEXTURE_CUBE_INDEX
;
998 case GL_TEXTURE_RECTANGLE_NV
:
999 return TEXTURE_RECT_INDEX
;
1000 case GL_TEXTURE_1D_ARRAY_EXT
:
1001 return TEXTURE_1D_ARRAY_INDEX
;
1002 case GL_TEXTURE_2D_ARRAY_EXT
:
1003 return TEXTURE_2D_ARRAY_INDEX
;
1004 case GL_TEXTURE_BUFFER_ARB
:
1005 return TEXTURE_BUFFER_INDEX
;
1006 case GL_TEXTURE_EXTERNAL_OES
:
1007 return TEXTURE_EXTERNAL_INDEX
;
1015 * Bind a named texture to a texturing target.
1017 * \param target texture target.
1018 * \param texName texture name.
1020 * \sa glBindTexture().
1022 * Determines the old texture object bound and returns immediately if rebinding
1023 * the same texture. Get the current texture which is either a default texture
1024 * if name is null, a named texture from the hash, or a new texture if the
1025 * given texture name is new. Increments its reference count, binds it, and
1026 * calls dd_function_table::BindTexture. Decrements the old texture reference
1027 * count and deletes it if it reaches zero.
1030 _mesa_BindTexture( GLenum target
, GLuint texName
)
1032 GET_CURRENT_CONTEXT(ctx
);
1033 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
1034 struct gl_texture_object
*newTexObj
= NULL
;
1036 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1038 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1039 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1040 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1042 targetIndex
= target_enum_to_index(target
);
1043 if (targetIndex
< 0) {
1044 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1047 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1050 * Get pointer to new texture object (newTexObj)
1053 /* Use a default texture object */
1054 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1057 /* non-default texture object */
1058 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1060 /* error checking */
1061 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1062 /* the named texture object's target doesn't match the given target */
1063 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1064 "glBindTexture(target mismatch)" );
1067 if (newTexObj
->Target
== 0) {
1068 finish_texture_init(ctx
, target
, newTexObj
);
1072 /* if this is a new texture id, allocate a texture object now */
1073 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1075 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1079 /* and insert it into hash table */
1080 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1081 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1082 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1084 newTexObj
->Target
= target
;
1087 assert(valid_texture_object(newTexObj
));
1089 /* Check if this texture is only used by this context and is already bound.
1090 * If so, just return.
1093 GLboolean early_out
;
1094 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1095 early_out
= ((ctx
->Shared
->RefCount
== 1)
1096 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1097 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1103 /* flush before changing binding */
1104 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1106 /* Do the actual binding. The refcount on the previously bound
1107 * texture object will be decremented. It'll be deleted if the
1110 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1111 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1113 /* Pass BindTexture call to device driver */
1114 if (ctx
->Driver
.BindTexture
)
1115 ctx
->Driver
.BindTexture(ctx
, target
, newTexObj
);
1120 * Set texture priorities.
1122 * \param n number of textures.
1123 * \param texName texture names.
1124 * \param priorities corresponding texture priorities.
1126 * \sa glPrioritizeTextures().
1128 * Looks up each texture in the hash, clamps the corresponding priority between
1129 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1132 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1133 const GLclampf
*priorities
)
1135 GET_CURRENT_CONTEXT(ctx
);
1137 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1140 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1147 for (i
= 0; i
< n
; i
++) {
1148 if (texName
[i
] > 0) {
1149 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1151 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1156 ctx
->NewState
|= _NEW_TEXTURE
;
1162 * See if textures are loaded in texture memory.
1164 * \param n number of textures to query.
1165 * \param texName array with the texture names.
1166 * \param residences array which will hold the residence status.
1168 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1170 * Note: we assume all textures are always resident
1172 GLboolean GLAPIENTRY
1173 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1174 GLboolean
*residences
)
1176 GET_CURRENT_CONTEXT(ctx
);
1177 GLboolean allResident
= GL_TRUE
;
1179 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1182 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1186 if (!texName
|| !residences
)
1189 /* We only do error checking on the texture names */
1190 for (i
= 0; i
< n
; i
++) {
1191 struct gl_texture_object
*t
;
1192 if (texName
[i
] == 0) {
1193 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1196 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1198 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1208 * See if a name corresponds to a texture.
1210 * \param texture texture name.
1212 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1215 * \sa glIsTexture().
1217 * Calls _mesa_HashLookup().
1219 GLboolean GLAPIENTRY
1220 _mesa_IsTexture( GLuint texture
)
1222 struct gl_texture_object
*t
;
1223 GET_CURRENT_CONTEXT(ctx
);
1224 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1229 t
= _mesa_lookup_texture(ctx
, texture
);
1231 /* IsTexture is true only after object has been bound once. */
1232 return t
&& t
->Target
;
1237 * Simplest implementation of texture locking: grab the shared tex
1238 * mutex. Examine the shared context state timestamp and if there has
1239 * been a change, set the appropriate bits in ctx->NewState.
1241 * This is used to deal with synchronizing things when a texture object
1242 * is used/modified by different contexts (or threads) which are sharing
1245 * See also _mesa_lock/unlock_texture() in teximage.h
1248 _mesa_lock_context_textures( struct gl_context
*ctx
)
1250 _glthread_LOCK_MUTEX(ctx
->Shared
->TexMutex
);
1252 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1253 ctx
->NewState
|= _NEW_TEXTURE
;
1254 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1260 _mesa_unlock_context_textures( struct gl_context
*ctx
)
1262 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1263 _glthread_UNLOCK_MUTEX(ctx
->Shared
->TexMutex
);