3 * Texture object management.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
31 #include "bufferobj.h"
44 #include "program/prog_instruction.h"
48 /**********************************************************************/
49 /** \name Internal functions */
54 * Return the gl_texture_object for a given ID.
56 struct gl_texture_object
*
57 _mesa_lookup_texture(struct gl_context
*ctx
, GLuint id
)
59 return (struct gl_texture_object
*)
60 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
64 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
65 * is not in the hash table. After calling _mesa_error, it returns NULL.
67 struct gl_texture_object
*
68 _mesa_lookup_texture_err(struct gl_context
*ctx
, GLuint id
, const char* func
)
70 struct gl_texture_object
*texObj
;
72 texObj
= _mesa_lookup_texture(ctx
, id
); /* Returns NULL if not found. */
75 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(texture)", func
);
81 _mesa_begin_texture_lookups(struct gl_context
*ctx
)
83 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
88 _mesa_end_texture_lookups(struct gl_context
*ctx
)
90 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
94 struct gl_texture_object
*
95 _mesa_lookup_texture_locked(struct gl_context
*ctx
, GLuint id
)
97 return (struct gl_texture_object
*)
98 _mesa_HashLookupLocked(ctx
->Shared
->TexObjects
, id
);
103 * Allocate and initialize a new texture object. But don't put it into the
104 * texture object hash table.
106 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
109 * \param shared the shared GL state structure to contain the texture object
110 * \param name integer name for the texture object
111 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
112 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
115 * \return pointer to new texture object.
117 struct gl_texture_object
*
118 _mesa_new_texture_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
120 struct gl_texture_object
*obj
;
122 obj
= MALLOC_STRUCT(gl_texture_object
);
123 _mesa_initialize_texture_object(ctx
, obj
, name
, target
);
129 * Initialize a new texture object to default values.
130 * \param obj the texture object
131 * \param name the texture name
132 * \param target the texture target
135 _mesa_initialize_texture_object( struct gl_context
*ctx
,
136 struct gl_texture_object
*obj
,
137 GLuint name
, GLenum target
)
139 ASSERT(target
== 0 ||
140 target
== GL_TEXTURE_1D
||
141 target
== GL_TEXTURE_2D
||
142 target
== GL_TEXTURE_3D
||
143 target
== GL_TEXTURE_CUBE_MAP_ARB
||
144 target
== GL_TEXTURE_RECTANGLE_NV
||
145 target
== GL_TEXTURE_1D_ARRAY_EXT
||
146 target
== GL_TEXTURE_2D_ARRAY_EXT
||
147 target
== GL_TEXTURE_EXTERNAL_OES
||
148 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
149 target
== GL_TEXTURE_BUFFER
||
150 target
== GL_TEXTURE_2D_MULTISAMPLE
||
151 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
153 memset(obj
, 0, sizeof(*obj
));
154 /* init the non-zero fields */
155 mtx_init(&obj
->Mutex
, mtx_plain
);
158 obj
->Target
= target
;
159 obj
->Priority
= 1.0F
;
161 obj
->MaxLevel
= 1000;
163 /* must be one; no support for (YUV) planes in separate buffers */
164 obj
->RequiredTextureImageUnits
= 1;
167 if (target
== GL_TEXTURE_RECTANGLE_NV
||
168 target
== GL_TEXTURE_EXTERNAL_OES
) {
169 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
170 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
171 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
172 obj
->Sampler
.MinFilter
= GL_LINEAR
;
175 obj
->Sampler
.WrapS
= GL_REPEAT
;
176 obj
->Sampler
.WrapT
= GL_REPEAT
;
177 obj
->Sampler
.WrapR
= GL_REPEAT
;
178 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
180 obj
->Sampler
.MagFilter
= GL_LINEAR
;
181 obj
->Sampler
.MinLod
= -1000.0;
182 obj
->Sampler
.MaxLod
= 1000.0;
183 obj
->Sampler
.LodBias
= 0.0;
184 obj
->Sampler
.MaxAnisotropy
= 1.0;
185 obj
->Sampler
.CompareMode
= GL_NONE
; /* ARB_shadow */
186 obj
->Sampler
.CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
187 obj
->DepthMode
= ctx
->API
== API_OPENGL_CORE
? GL_RED
: GL_LUMINANCE
;
188 obj
->StencilSampling
= false;
189 obj
->Sampler
.CubeMapSeamless
= GL_FALSE
;
190 obj
->Swizzle
[0] = GL_RED
;
191 obj
->Swizzle
[1] = GL_GREEN
;
192 obj
->Swizzle
[2] = GL_BLUE
;
193 obj
->Swizzle
[3] = GL_ALPHA
;
194 obj
->_Swizzle
= SWIZZLE_NOOP
;
195 obj
->Sampler
.sRGBDecode
= GL_DECODE_EXT
;
196 obj
->BufferObjectFormat
= GL_R8
;
197 obj
->_BufferObjectFormat
= MESA_FORMAT_R_UNORM8
;
198 obj
->ImageFormatCompatibilityType
= GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE
;
203 * Some texture initialization can't be finished until we know which
204 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
207 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
208 struct gl_texture_object
*obj
)
210 GLenum filter
= GL_LINEAR
;
211 assert(obj
->Target
== 0);
214 case GL_TEXTURE_2D_MULTISAMPLE
:
215 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
219 case GL_TEXTURE_RECTANGLE_NV
:
220 case GL_TEXTURE_EXTERNAL_OES
:
221 /* have to init wrap and filter state here - kind of klunky */
222 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
223 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
224 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
225 obj
->Sampler
.MinFilter
= filter
;
226 obj
->Sampler
.MagFilter
= filter
;
227 if (ctx
->Driver
.TexParameter
) {
228 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
229 const GLfloat fparam_filter
[1] = {(GLfloat
) filter
};
230 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
231 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
232 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
233 ctx
->Driver
.TexParameter(ctx
, obj
,
234 GL_TEXTURE_MIN_FILTER
, fparam_filter
);
235 ctx
->Driver
.TexParameter(ctx
, obj
,
236 GL_TEXTURE_MAG_FILTER
, fparam_filter
);
241 /* nothing needs done */
248 * Deallocate a texture object struct. It should have already been
249 * removed from the texture object pool.
250 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
252 * \param shared the shared GL state to which the object belongs.
253 * \param texObj the texture object to delete.
256 _mesa_delete_texture_object(struct gl_context
*ctx
,
257 struct gl_texture_object
*texObj
)
261 /* Set Target to an invalid value. With some assertions elsewhere
262 * we can try to detect possible use of deleted textures.
264 texObj
->Target
= 0x99;
266 /* free the texture images */
267 for (face
= 0; face
< 6; face
++) {
268 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
269 if (texObj
->Image
[face
][i
]) {
270 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
275 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
277 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
278 mtx_destroy(&texObj
->Mutex
);
282 /* free this object */
288 * Copy texture object state from one texture object to another.
289 * Use for glPush/PopAttrib.
291 * \param dest destination texture object.
292 * \param src source texture object.
295 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
296 const struct gl_texture_object
*src
)
298 dest
->Target
= src
->Target
;
299 dest
->TargetIndex
= src
->TargetIndex
;
300 dest
->Name
= src
->Name
;
301 dest
->Priority
= src
->Priority
;
302 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
303 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
304 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
305 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
306 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
307 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
308 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
309 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
310 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
311 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
312 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
313 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
314 dest
->BaseLevel
= src
->BaseLevel
;
315 dest
->MaxLevel
= src
->MaxLevel
;
316 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
317 dest
->Sampler
.CompareMode
= src
->Sampler
.CompareMode
;
318 dest
->Sampler
.CompareFunc
= src
->Sampler
.CompareFunc
;
319 dest
->Sampler
.CubeMapSeamless
= src
->Sampler
.CubeMapSeamless
;
320 dest
->DepthMode
= src
->DepthMode
;
321 dest
->StencilSampling
= src
->StencilSampling
;
322 dest
->Sampler
.sRGBDecode
= src
->Sampler
.sRGBDecode
;
323 dest
->_MaxLevel
= src
->_MaxLevel
;
324 dest
->_MaxLambda
= src
->_MaxLambda
;
325 dest
->GenerateMipmap
= src
->GenerateMipmap
;
326 dest
->_BaseComplete
= src
->_BaseComplete
;
327 dest
->_MipmapComplete
= src
->_MipmapComplete
;
328 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
329 dest
->_Swizzle
= src
->_Swizzle
;
331 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
336 * Free all texture images of the given texture object.
338 * \param ctx GL context.
339 * \param t texture object.
341 * \sa _mesa_clear_texture_image().
344 _mesa_clear_texture_object(struct gl_context
*ctx
,
345 struct gl_texture_object
*texObj
)
349 if (texObj
->Target
== 0)
352 for (i
= 0; i
< MAX_FACES
; i
++) {
353 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
354 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
356 _mesa_clear_texture_image(ctx
, texImage
);
363 * Check if the given texture object is valid by examining its Target field.
364 * For debugging only.
367 valid_texture_object(const struct gl_texture_object
*tex
)
369 switch (tex
->Target
) {
374 case GL_TEXTURE_CUBE_MAP_ARB
:
375 case GL_TEXTURE_RECTANGLE_NV
:
376 case GL_TEXTURE_1D_ARRAY_EXT
:
377 case GL_TEXTURE_2D_ARRAY_EXT
:
378 case GL_TEXTURE_BUFFER
:
379 case GL_TEXTURE_EXTERNAL_OES
:
380 case GL_TEXTURE_CUBE_MAP_ARRAY
:
381 case GL_TEXTURE_2D_MULTISAMPLE
:
382 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
385 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
388 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
389 tex
->Target
, tex
->Name
);
396 * Reference (or unreference) a texture object.
397 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
398 * If 'tex' is non-null, increment its refcount.
399 * This is normally only called from the _mesa_reference_texobj() macro
400 * when there's a real pointer change.
403 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
404 struct gl_texture_object
*tex
)
409 /* Unreference the old texture */
410 GLboolean deleteFlag
= GL_FALSE
;
411 struct gl_texture_object
*oldTex
= *ptr
;
413 ASSERT(valid_texture_object(oldTex
));
414 (void) valid_texture_object
; /* silence warning in release builds */
416 mtx_lock(&oldTex
->Mutex
);
417 ASSERT(oldTex
->RefCount
> 0);
420 deleteFlag
= (oldTex
->RefCount
== 0);
421 mtx_unlock(&oldTex
->Mutex
);
424 GET_CURRENT_CONTEXT(ctx
);
426 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
428 _mesa_problem(NULL
, "Unable to delete texture, no context");
436 /* reference new texture */
437 ASSERT(valid_texture_object(tex
));
438 mtx_lock(&tex
->Mutex
);
439 if (tex
->RefCount
== 0) {
440 /* this texture's being deleted (look just above) */
441 /* Not sure this can every really happen. Warn if it does. */
442 _mesa_problem(NULL
, "referencing deleted texture object");
449 mtx_unlock(&tex
->Mutex
);
454 enum base_mipmap
{ BASE
, MIPMAP
};
458 * Mark a texture object as incomplete. There are actually three kinds of
460 * 1. "base incomplete": the base level of the texture is invalid so no
461 * texturing is possible.
462 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
463 * mipmap filtering isn't possible, but non-mipmap filtering is.
464 * 3. "texture incompleteness": some combination of texture state and
465 * sampler state renders the texture incomplete.
467 * \param t texture object
468 * \param bm either BASE or MIPMAP to indicate what's incomplete
469 * \param fmt... string describing why it's incomplete (for debugging).
472 incomplete(struct gl_texture_object
*t
, enum base_mipmap bm
,
473 const char *fmt
, ...)
475 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_TEXTURE
) {
480 vsnprintf(s
, sizeof(s
), fmt
, args
);
483 _mesa_debug(NULL
, "Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
487 t
->_BaseComplete
= GL_FALSE
;
488 t
->_MipmapComplete
= GL_FALSE
;
493 * Examine a texture object to determine if it is complete.
495 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
498 * \param ctx GL context.
499 * \param t texture object.
501 * According to the texture target, verifies that each of the mipmaps is
502 * present and has the expected size.
505 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
506 struct gl_texture_object
*t
)
508 const GLint baseLevel
= t
->BaseLevel
;
509 const struct gl_texture_image
*baseImage
;
512 /* We'll set these to FALSE if tests fail below */
513 t
->_BaseComplete
= GL_TRUE
;
514 t
->_MipmapComplete
= GL_TRUE
;
516 if (t
->Target
== GL_TEXTURE_BUFFER
) {
517 /* Buffer textures are always considered complete. The obvious case where
518 * they would be incomplete (no BO attached) is actually specced to be
519 * undefined rendering results.
524 /* Detect cases where the application set the base level to an invalid
527 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
528 incomplete(t
, BASE
, "base level = %d is invalid", baseLevel
);
532 if (t
->MaxLevel
< baseLevel
) {
533 incomplete(t
, MIPMAP
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
534 t
->MaxLevel
, baseLevel
);
538 baseImage
= t
->Image
[0][baseLevel
];
540 /* Always need the base level image */
542 incomplete(t
, BASE
, "Image[baseLevel=%d] == NULL", baseLevel
);
546 /* Check width/height/depth for zero */
547 if (baseImage
->Width
== 0 ||
548 baseImage
->Height
== 0 ||
549 baseImage
->Depth
== 0) {
550 incomplete(t
, BASE
, "texture width or height or depth = 0");
554 /* Check if the texture values are integer */
556 GLenum datatype
= _mesa_get_format_datatype(baseImage
->TexFormat
);
557 t
->_IsIntegerFormat
= datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
;
560 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
561 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
565 case GL_TEXTURE_1D_ARRAY_EXT
:
566 maxLevels
= ctx
->Const
.MaxTextureLevels
;
569 case GL_TEXTURE_2D_ARRAY_EXT
:
570 maxLevels
= ctx
->Const
.MaxTextureLevels
;
573 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
575 case GL_TEXTURE_CUBE_MAP_ARB
:
576 case GL_TEXTURE_CUBE_MAP_ARRAY
:
577 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
579 case GL_TEXTURE_RECTANGLE_NV
:
580 case GL_TEXTURE_BUFFER
:
581 case GL_TEXTURE_EXTERNAL_OES
:
582 case GL_TEXTURE_2D_MULTISAMPLE
:
583 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
584 maxLevels
= 1; /* no mipmapping */
587 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
591 ASSERT(maxLevels
> 0);
593 t
->_MaxLevel
= MIN3(t
->MaxLevel
,
594 /* 'p' in the GL spec */
595 (int) (baseLevel
+ baseImage
->MaxNumLevels
- 1),
596 /* 'q' in the GL spec */
600 /* Adjust max level for views: the data store may have more levels than
603 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->NumLevels
- 1);
606 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
607 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- baseLevel
);
610 /* This texture object was created with glTexStorage1/2/3D() so we
611 * know that all the mipmap levels are the right size and all cube
612 * map faces are the same size.
613 * We don't need to do any of the additional checks below.
618 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
619 /* Make sure that all six cube map level 0 images are the same size.
620 * Note: we know that the image's width==height (we enforce that
621 * at glTexImage time) so we only need to test the width here.
624 assert(baseImage
->Width2
== baseImage
->Height
);
625 for (face
= 1; face
< 6; face
++) {
626 assert(t
->Image
[face
][baseLevel
] == NULL
||
627 t
->Image
[face
][baseLevel
]->Width2
==
628 t
->Image
[face
][baseLevel
]->Height2
);
629 if (t
->Image
[face
][baseLevel
] == NULL
||
630 t
->Image
[face
][baseLevel
]->Width2
!= baseImage
->Width2
) {
631 incomplete(t
, BASE
, "Cube face missing or mismatched size");
638 * Do mipmap consistency checking.
639 * Note: we don't care about the current texture sampler state here.
640 * To determine texture completeness we'll either look at _BaseComplete
641 * or _MipmapComplete depending on the current minification filter mode.
645 const GLint minLevel
= baseLevel
;
646 const GLint maxLevel
= t
->_MaxLevel
;
647 const GLuint numFaces
= _mesa_num_tex_faces(t
->Target
);
648 GLuint width
, height
, depth
, face
;
650 if (minLevel
> maxLevel
) {
651 incomplete(t
, MIPMAP
, "minLevel > maxLevel");
655 /* Get the base image's dimensions */
656 width
= baseImage
->Width2
;
657 height
= baseImage
->Height2
;
658 depth
= baseImage
->Depth2
;
660 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
661 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
663 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
664 /* Compute the expected size of image at level[i] */
668 if (height
> 1 && t
->Target
!= GL_TEXTURE_1D_ARRAY
) {
671 if (depth
> 1 && t
->Target
!= GL_TEXTURE_2D_ARRAY
672 && t
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
676 /* loop over cube faces (or single face otherwise) */
677 for (face
= 0; face
< numFaces
; face
++) {
678 if (i
>= minLevel
&& i
<= maxLevel
) {
679 const struct gl_texture_image
*img
= t
->Image
[face
][i
];
682 incomplete(t
, MIPMAP
, "TexImage[%d] is missing", i
);
685 if (img
->TexFormat
!= baseImage
->TexFormat
) {
686 incomplete(t
, MIPMAP
, "Format[i] != Format[baseLevel]");
689 if (img
->Border
!= baseImage
->Border
) {
690 incomplete(t
, MIPMAP
, "Border[i] != Border[baseLevel]");
693 if (img
->Width2
!= width
) {
694 incomplete(t
, MIPMAP
, "TexImage[%d] bad width %u", i
,
698 if (img
->Height2
!= height
) {
699 incomplete(t
, MIPMAP
, "TexImage[%d] bad height %u", i
,
703 if (img
->Depth2
!= depth
) {
704 incomplete(t
, MIPMAP
, "TexImage[%d] bad depth %u", i
,
709 /* Extra checks for cube textures */
711 /* check that cube faces are the same size */
712 if (img
->Width2
!= t
->Image
[0][i
]->Width2
||
713 img
->Height2
!= t
->Image
[0][i
]->Height2
) {
714 incomplete(t
, MIPMAP
, "CubeMap Image[n][i] bad size");
721 if (width
== 1 && height
== 1 && depth
== 1) {
722 return; /* found smallest needed mipmap, all done! */
730 * Check if the given cube map texture is "cube complete" as defined in
731 * the OpenGL specification.
734 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
736 const GLint baseLevel
= texObj
->BaseLevel
;
737 const struct gl_texture_image
*img0
, *img
;
740 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
743 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
))
746 /* check first face */
747 img0
= texObj
->Image
[0][baseLevel
];
750 img0
->Width
!= img0
->Height
)
753 /* check remaining faces vs. first face */
754 for (face
= 1; face
< 6; face
++) {
755 img
= texObj
->Image
[face
][baseLevel
];
757 img
->Width
!= img0
->Width
||
758 img
->Height
!= img0
->Height
||
759 img
->TexFormat
!= img0
->TexFormat
)
768 * Mark a texture object dirty. It forces the object to be incomplete
769 * and forces the context to re-validate its state.
771 * \param ctx GL context.
772 * \param texObj texture object.
775 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
777 texObj
->_BaseComplete
= GL_FALSE
;
778 texObj
->_MipmapComplete
= GL_FALSE
;
779 ctx
->NewState
|= _NEW_TEXTURE
;
784 * Return pointer to a default/fallback texture of the given type/target.
785 * The texture is an RGBA texture with all texels = (0,0,0,1).
786 * That's the value a GLSL sampler should get when sampling from an
787 * incomplete texture.
789 struct gl_texture_object
*
790 _mesa_get_fallback_texture(struct gl_context
*ctx
, gl_texture_index tex
)
792 if (!ctx
->Shared
->FallbackTex
[tex
]) {
793 /* create fallback texture now */
794 const GLsizei width
= 1, height
= 1;
797 struct gl_texture_object
*texObj
;
798 struct gl_texture_image
*texImage
;
799 mesa_format texFormat
;
800 GLuint dims
, face
, numFaces
= 1;
803 for (face
= 0; face
< 6; face
++) {
806 texel
[4*face
+ 2] = 0x0;
807 texel
[4*face
+ 3] = 0xff;
811 case TEXTURE_2D_ARRAY_INDEX
:
813 target
= GL_TEXTURE_2D_ARRAY
;
815 case TEXTURE_1D_ARRAY_INDEX
:
817 target
= GL_TEXTURE_1D_ARRAY
;
819 case TEXTURE_CUBE_INDEX
:
821 target
= GL_TEXTURE_CUBE_MAP
;
824 case TEXTURE_3D_INDEX
:
826 target
= GL_TEXTURE_3D
;
828 case TEXTURE_RECT_INDEX
:
830 target
= GL_TEXTURE_RECTANGLE
;
832 case TEXTURE_2D_INDEX
:
834 target
= GL_TEXTURE_2D
;
836 case TEXTURE_1D_INDEX
:
838 target
= GL_TEXTURE_1D
;
840 case TEXTURE_BUFFER_INDEX
:
842 target
= GL_TEXTURE_BUFFER
;
844 case TEXTURE_CUBE_ARRAY_INDEX
:
846 target
= GL_TEXTURE_CUBE_MAP_ARRAY
;
849 case TEXTURE_EXTERNAL_INDEX
:
851 target
= GL_TEXTURE_EXTERNAL_OES
;
853 case TEXTURE_2D_MULTISAMPLE_INDEX
:
855 target
= GL_TEXTURE_2D_MULTISAMPLE
;
857 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
:
859 target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
866 /* create texture object */
867 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, target
);
871 assert(texObj
->RefCount
== 1);
872 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
873 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
875 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
,
879 /* need a loop here just for cube maps */
880 for (face
= 0; face
< numFaces
; face
++) {
883 if (target
== GL_TEXTURE_CUBE_MAP
)
884 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
;
888 /* initialize level[0] texture image */
889 texImage
= _mesa_get_tex_image(ctx
, texObj
, faceTarget
, 0);
891 _mesa_init_teximage_fields(ctx
, texImage
,
893 (dims
> 1) ? height
: 1,
894 (dims
> 2) ? depth
: 1,
898 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
899 GL_RGBA
, GL_UNSIGNED_BYTE
, texel
,
900 &ctx
->DefaultPacking
);
903 _mesa_test_texobj_completeness(ctx
, texObj
);
904 assert(texObj
->_BaseComplete
);
905 assert(texObj
->_MipmapComplete
);
907 ctx
->Shared
->FallbackTex
[tex
] = texObj
;
909 return ctx
->Shared
->FallbackTex
[tex
];
914 * Compute the size of the given texture object, in bytes.
917 texture_size(const struct gl_texture_object
*texObj
)
919 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
920 GLuint face
, level
, size
= 0;
922 for (face
= 0; face
< numFaces
; face
++) {
923 for (level
= 0; level
< MAX_TEXTURE_LEVELS
; level
++) {
924 const struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
926 GLuint sz
= _mesa_format_image_size(img
->TexFormat
, img
->Width
,
927 img
->Height
, img
->Depth
);
938 * Callback called from _mesa_HashWalk()
941 count_tex_size(GLuint key
, void *data
, void *userData
)
943 const struct gl_texture_object
*texObj
=
944 (const struct gl_texture_object
*) data
;
945 GLuint
*total
= (GLuint
*) userData
;
949 *total
= *total
+ texture_size(texObj
);
954 * Compute total size (in bytes) of all textures for the given context.
955 * For debugging purposes.
958 _mesa_total_texture_memory(struct gl_context
*ctx
)
960 GLuint tgt
, total
= 0;
962 _mesa_HashWalk(ctx
->Shared
->TexObjects
, count_tex_size
, &total
);
964 /* plus, the default texture objects */
965 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
966 total
+= texture_size(ctx
->Shared
->DefaultTex
[tgt
]);
974 * Return the base format for the given texture object by looking
975 * at the base texture image.
976 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
979 _mesa_texture_base_format(const struct gl_texture_object
*texObj
)
981 const struct gl_texture_image
*texImage
= _mesa_base_tex_image(texObj
);
983 return texImage
? texImage
->_BaseFormat
: GL_NONE
;
987 static struct gl_texture_object
*
988 invalidate_tex_image_error_check(struct gl_context
*ctx
, GLuint texture
,
989 GLint level
, const char *name
)
991 /* The GL_ARB_invalidate_subdata spec says:
993 * "If <texture> is zero or is not the name of a texture, the error
994 * INVALID_VALUE is generated."
996 * This performs the error check in a different order than listed in the
997 * spec. We have to get the texture object before we can validate the
998 * other parameters against values in the texture object.
1000 struct gl_texture_object
*const t
= _mesa_lookup_texture(ctx
, texture
);
1001 if (texture
== 0 || t
== NULL
) {
1002 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(texture)", name
);
1006 /* The GL_ARB_invalidate_subdata spec says:
1008 * "If <level> is less than zero or greater than the base 2 logarithm
1009 * of the maximum texture width, height, or depth, the error
1010 * INVALID_VALUE is generated."
1012 if (level
< 0 || level
> t
->MaxLevel
) {
1013 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1017 /* The GL_ARB_invalidate_subdata spec says:
1019 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1020 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1021 * is not zero, the error INVALID_VALUE is generated."
1024 switch (t
->Target
) {
1025 case GL_TEXTURE_RECTANGLE
:
1026 case GL_TEXTURE_BUFFER
:
1027 case GL_TEXTURE_2D_MULTISAMPLE
:
1028 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1029 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1043 /***********************************************************************/
1044 /** \name API functions */
1049 * Generate texture names.
1051 * \param n number of texture names to be generated.
1052 * \param textures an array in which will hold the generated texture names.
1054 * \sa glGenTextures().
1056 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1057 * IDs which are stored in \p textures. Corresponding empty texture
1058 * objects are also generated.
1061 _mesa_GenTextures( GLsizei n
, GLuint
*textures
)
1063 GET_CURRENT_CONTEXT(ctx
);
1067 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1068 _mesa_debug(ctx
, "glGenTextures %d\n", n
);
1071 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGenTextures" );
1079 * This must be atomic (generation and allocation of texture IDs)
1081 mtx_lock(&ctx
->Shared
->Mutex
);
1083 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
1085 /* Allocate new, empty texture objects */
1086 for (i
= 0; i
< n
; i
++) {
1087 struct gl_texture_object
*texObj
;
1088 GLuint name
= first
+ i
;
1090 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
1092 mtx_unlock(&ctx
->Shared
->Mutex
);
1093 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenTextures");
1097 /* insert into hash table */
1098 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
1103 mtx_unlock(&ctx
->Shared
->Mutex
);
1108 * Check if the given texture object is bound to the current draw or
1109 * read framebuffer. If so, Unbind it.
1112 unbind_texobj_from_fbo(struct gl_context
*ctx
,
1113 struct gl_texture_object
*texObj
)
1115 bool progress
= false;
1117 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1118 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1121 * "If a texture object is deleted while its image is attached to one
1122 * or more attachment points in the currently bound framebuffer, then
1123 * it is as if FramebufferTexture* had been called, with a texture of
1124 * zero, for each attachment point to which this image was attached in
1125 * the currently bound framebuffer. In other words, this texture image
1126 * is first detached from all attachment points in the currently bound
1127 * framebuffer. Note that the texture image is specifically not
1128 * detached from any other framebuffer objects. Detaching the texture
1129 * image from any other framebuffer objects is the responsibility of
1132 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1133 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, texObj
);
1135 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1136 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1137 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, texObj
)
1142 /* Vertices are already flushed by _mesa_DeleteTextures */
1143 ctx
->NewState
|= _NEW_BUFFERS
;
1148 * Check if the given texture object is bound to any texture image units and
1149 * unbind it if so (revert to default textures).
1152 unbind_texobj_from_texunits(struct gl_context
*ctx
,
1153 struct gl_texture_object
*texObj
)
1155 const gl_texture_index index
= texObj
->TargetIndex
;
1158 if (texObj
->Target
== 0)
1161 for (u
= 0; u
< ctx
->Texture
.NumCurrentTexUsed
; u
++) {
1162 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
1164 if (texObj
== unit
->CurrentTex
[index
]) {
1165 /* Bind the default texture for this unit/target */
1166 _mesa_reference_texobj(&unit
->CurrentTex
[index
],
1167 ctx
->Shared
->DefaultTex
[index
]);
1168 unit
->_BoundTextures
&= ~(1 << index
);
1175 * Check if the given texture object is bound to any shader image unit
1176 * and unbind it if that's the case.
1179 unbind_texobj_from_image_units(struct gl_context
*ctx
,
1180 struct gl_texture_object
*texObj
)
1184 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; i
++) {
1185 struct gl_image_unit
*unit
= &ctx
->ImageUnits
[i
];
1187 if (texObj
== unit
->TexObj
)
1188 _mesa_reference_texobj(&unit
->TexObj
, NULL
);
1194 * Unbinds all textures bound to the given texture image unit.
1197 unbind_textures_from_unit(struct gl_context
*ctx
, GLuint unit
)
1199 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1201 while (texUnit
->_BoundTextures
) {
1202 const GLuint index
= ffs(texUnit
->_BoundTextures
) - 1;
1203 struct gl_texture_object
*texObj
= ctx
->Shared
->DefaultTex
[index
];
1205 _mesa_reference_texobj(&texUnit
->CurrentTex
[index
], texObj
);
1207 /* Pass BindTexture call to device driver */
1208 if (ctx
->Driver
.BindTexture
)
1209 ctx
->Driver
.BindTexture(ctx
, unit
, 0, texObj
);
1211 texUnit
->_BoundTextures
&= ~(1 << index
);
1212 ctx
->NewState
|= _NEW_TEXTURE
;
1218 * Delete named textures.
1220 * \param n number of textures to be deleted.
1221 * \param textures array of texture IDs to be deleted.
1223 * \sa glDeleteTextures().
1225 * If we're about to delete a texture that's currently bound to any
1226 * texture unit, unbind the texture first. Decrement the reference
1227 * count on the texture object and delete it if it's zero.
1228 * Recall that texture objects can be shared among several rendering
1232 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
1234 GET_CURRENT_CONTEXT(ctx
);
1237 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1238 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1240 FLUSH_VERTICES(ctx
, 0); /* too complex */
1245 for (i
= 0; i
< n
; i
++) {
1246 if (textures
[i
] > 0) {
1247 struct gl_texture_object
*delObj
1248 = _mesa_lookup_texture(ctx
, textures
[i
]);
1251 _mesa_lock_texture(ctx
, delObj
);
1253 /* Check if texture is bound to any framebuffer objects.
1255 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1257 unbind_texobj_from_fbo(ctx
, delObj
);
1259 /* Check if this texture is currently bound to any texture units.
1262 unbind_texobj_from_texunits(ctx
, delObj
);
1264 /* Check if this texture is currently bound to any shader
1265 * image unit. If so, unbind it.
1266 * See section 3.9.X of GL_ARB_shader_image_load_store.
1268 unbind_texobj_from_image_units(ctx
, delObj
);
1270 _mesa_unlock_texture(ctx
, delObj
);
1272 ctx
->NewState
|= _NEW_TEXTURE
;
1274 /* The texture _name_ is now free for re-use.
1275 * Remove it from the hash table now.
1277 mtx_lock(&ctx
->Shared
->Mutex
);
1278 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
1279 mtx_unlock(&ctx
->Shared
->Mutex
);
1281 /* Unreference the texobj. If refcount hits zero, the texture
1284 _mesa_reference_texobj(&delObj
, NULL
);
1292 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1293 * into the corresponding Mesa texture target index.
1294 * Note that proxy targets are not valid here.
1295 * \return TEXTURE_x_INDEX or -1 if target is invalid
1298 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1302 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1304 return TEXTURE_2D_INDEX
;
1306 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1307 case GL_TEXTURE_CUBE_MAP
:
1308 return ctx
->Extensions
.ARB_texture_cube_map
1309 ? TEXTURE_CUBE_INDEX
: -1;
1310 case GL_TEXTURE_RECTANGLE
:
1311 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1312 ? TEXTURE_RECT_INDEX
: -1;
1313 case GL_TEXTURE_1D_ARRAY
:
1314 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1315 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1316 case GL_TEXTURE_2D_ARRAY
:
1317 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1318 || _mesa_is_gles3(ctx
)
1319 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1320 case GL_TEXTURE_BUFFER
:
1321 return ctx
->API
== API_OPENGL_CORE
&&
1322 ctx
->Extensions
.ARB_texture_buffer_object
?
1323 TEXTURE_BUFFER_INDEX
: -1;
1324 case GL_TEXTURE_EXTERNAL_OES
:
1325 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1326 ? TEXTURE_EXTERNAL_INDEX
: -1;
1327 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1328 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_cube_map_array
1329 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1330 case GL_TEXTURE_2D_MULTISAMPLE
:
1331 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1332 ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1333 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1334 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1335 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1343 * Bind a named texture to a texturing target.
1345 * \param target texture target.
1346 * \param texName texture name.
1348 * \sa glBindTexture().
1350 * Determines the old texture object bound and returns immediately if rebinding
1351 * the same texture. Get the current texture which is either a default texture
1352 * if name is null, a named texture from the hash, or a new texture if the
1353 * given texture name is new. Increments its reference count, binds it, and
1354 * calls dd_function_table::BindTexture. Decrements the old texture reference
1355 * count and deletes it if it reaches zero.
1358 _mesa_BindTexture( GLenum target
, GLuint texName
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
1362 struct gl_texture_object
*newTexObj
= NULL
;
1365 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1366 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1367 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1369 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1370 if (targetIndex
< 0) {
1371 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1374 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1377 * Get pointer to new texture object (newTexObj)
1380 /* Use a default texture object */
1381 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1384 /* non-default texture object */
1385 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1387 /* error checking */
1388 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1389 /* The named texture object's target doesn't match the
1392 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1393 "glBindTexture(target mismatch)" );
1396 if (newTexObj
->Target
== 0) {
1397 finish_texture_init(ctx
, target
, newTexObj
);
1401 if (ctx
->API
== API_OPENGL_CORE
) {
1402 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1403 "glBindTexture(non-gen name)");
1407 /* if this is a new texture id, allocate a texture object now */
1408 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1410 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1414 /* and insert it into hash table */
1415 mtx_lock(&ctx
->Shared
->Mutex
);
1416 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1417 mtx_unlock(&ctx
->Shared
->Mutex
);
1419 newTexObj
->Target
= target
;
1420 newTexObj
->TargetIndex
= targetIndex
;
1423 assert(valid_texture_object(newTexObj
));
1425 /* Check if this texture is only used by this context and is already bound.
1426 * If so, just return.
1429 GLboolean early_out
;
1430 mtx_lock(&ctx
->Shared
->Mutex
);
1431 early_out
= ((ctx
->Shared
->RefCount
== 1)
1432 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1433 mtx_unlock(&ctx
->Shared
->Mutex
);
1439 /* flush before changing binding */
1440 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1442 /* Do the actual binding. The refcount on the previously bound
1443 * texture object will be decremented. It'll be deleted if the
1446 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1447 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1448 ctx
->Texture
.CurrentUnit
+ 1);
1449 ASSERT(texUnit
->CurrentTex
[targetIndex
]);
1452 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1454 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1456 /* Pass BindTexture call to device driver */
1457 if (ctx
->Driver
.BindTexture
)
1458 ctx
->Driver
.BindTexture(ctx
, ctx
->Texture
.CurrentUnit
, target
, newTexObj
);
1463 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
1465 GET_CURRENT_CONTEXT(ctx
);
1468 /* The ARB_multi_bind spec says:
1470 * "An INVALID_OPERATION error is generated if <first> + <count>
1471 * is greater than the number of texture image units supported
1472 * by the implementation."
1474 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
1475 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1476 "glBindTextures(first=%u + count=%d > the value of "
1477 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
1478 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
1482 /* Flush before changing bindings */
1483 FLUSH_VERTICES(ctx
, 0);
1485 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1489 /* Note that the error semantics for multi-bind commands differ from
1490 * those of other GL commands.
1492 * The issues section in the ARB_multi_bind spec says:
1494 * "(11) Typically, OpenGL specifies that if an error is generated by
1495 * a command, that command has no effect. This is somewhat
1496 * unfortunate for multi-bind commands, because it would require
1497 * a first pass to scan the entire list of bound objects for
1498 * errors and then a second pass to actually perform the
1499 * bindings. Should we have different error semantics?
1501 * RESOLVED: Yes. In this specification, when the parameters for
1502 * one of the <count> binding points are invalid, that binding
1503 * point is not updated and an error will be generated. However,
1504 * other binding points in the same command will be updated if
1505 * their parameters are valid and no other error occurs."
1508 _mesa_begin_texture_lookups(ctx
);
1510 for (i
= 0; i
< count
; i
++) {
1511 if (textures
[i
] != 0) {
1512 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
1513 struct gl_texture_object
*current
= texUnit
->_Current
;
1514 struct gl_texture_object
*texObj
;
1516 if (current
&& current
->Name
== textures
[i
])
1519 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
1521 if (texObj
&& texObj
->Target
!= 0) {
1522 const gl_texture_index targetIndex
= texObj
->TargetIndex
;
1524 if (texUnit
->CurrentTex
[targetIndex
] != texObj
) {
1525 /* Do the actual binding. The refcount on the previously
1526 * bound texture object will be decremented. It will be
1527 * deleted if the count hits zero.
1529 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
],
1532 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1533 ctx
->NewState
|= _NEW_TEXTURE
;
1535 /* Pass the BindTexture call to the device driver */
1536 if (ctx
->Driver
.BindTexture
)
1537 ctx
->Driver
.BindTexture(ctx
, first
+ i
,
1538 texObj
->Target
, texObj
);
1541 /* The ARB_multi_bind spec says:
1543 * "An INVALID_OPERATION error is generated if any value
1544 * in <textures> is not zero or the name of an existing
1545 * texture object (per binding)."
1547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1548 "glBindTextures(textures[%d]=%u is not zero "
1549 "or the name of an existing texture object)",
1553 unbind_textures_from_unit(ctx
, first
+ i
);
1557 _mesa_end_texture_lookups(ctx
);
1559 /* Unbind all textures in the range <first> through <first>+<count>-1 */
1560 for (i
= 0; i
< count
; i
++)
1561 unbind_textures_from_unit(ctx
, first
+ i
);
1567 * Set texture priorities.
1569 * \param n number of textures.
1570 * \param texName texture names.
1571 * \param priorities corresponding texture priorities.
1573 * \sa glPrioritizeTextures().
1575 * Looks up each texture in the hash, clamps the corresponding priority between
1576 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1579 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1580 const GLclampf
*priorities
)
1582 GET_CURRENT_CONTEXT(ctx
);
1585 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1586 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
1588 FLUSH_VERTICES(ctx
, 0);
1591 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1598 for (i
= 0; i
< n
; i
++) {
1599 if (texName
[i
] > 0) {
1600 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1602 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1607 ctx
->NewState
|= _NEW_TEXTURE
;
1613 * See if textures are loaded in texture memory.
1615 * \param n number of textures to query.
1616 * \param texName array with the texture names.
1617 * \param residences array which will hold the residence status.
1619 * \return GL_TRUE if all textures are resident and
1620 * residences is left unchanged,
1622 * Note: we assume all textures are always resident
1624 GLboolean GLAPIENTRY
1625 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
1626 GLboolean
*residences
)
1628 GET_CURRENT_CONTEXT(ctx
);
1629 GLboolean allResident
= GL_TRUE
;
1631 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1633 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1634 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
1637 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
1641 if (!texName
|| !residences
)
1644 /* We only do error checking on the texture names */
1645 for (i
= 0; i
< n
; i
++) {
1646 struct gl_texture_object
*t
;
1647 if (texName
[i
] == 0) {
1648 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1651 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1653 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
1663 * See if a name corresponds to a texture.
1665 * \param texture texture name.
1667 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1670 * \sa glIsTexture().
1672 * Calls _mesa_HashLookup().
1674 GLboolean GLAPIENTRY
1675 _mesa_IsTexture( GLuint texture
)
1677 struct gl_texture_object
*t
;
1678 GET_CURRENT_CONTEXT(ctx
);
1679 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1681 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1682 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
1687 t
= _mesa_lookup_texture(ctx
, texture
);
1689 /* IsTexture is true only after object has been bound once. */
1690 return t
&& t
->Target
;
1695 * Simplest implementation of texture locking: grab the shared tex
1696 * mutex. Examine the shared context state timestamp and if there has
1697 * been a change, set the appropriate bits in ctx->NewState.
1699 * This is used to deal with synchronizing things when a texture object
1700 * is used/modified by different contexts (or threads) which are sharing
1703 * See also _mesa_lock/unlock_texture() in teximage.h
1706 _mesa_lock_context_textures( struct gl_context
*ctx
)
1708 mtx_lock(&ctx
->Shared
->TexMutex
);
1710 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
1711 ctx
->NewState
|= _NEW_TEXTURE
;
1712 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
1718 _mesa_unlock_context_textures( struct gl_context
*ctx
)
1720 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
1721 mtx_unlock(&ctx
->Shared
->TexMutex
);
1726 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
1727 GLint yoffset
, GLint zoffset
, GLsizei width
,
1728 GLsizei height
, GLsizei depth
)
1730 struct gl_texture_object
*t
;
1731 struct gl_texture_image
*image
;
1732 GET_CURRENT_CONTEXT(ctx
);
1734 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1735 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
1737 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
1738 "glInvalidateTexSubImage");
1740 /* The GL_ARB_invalidate_subdata spec says:
1742 * "...the specified subregion must be between -<b> and <dim>+<b> where
1743 * <dim> is the size of the dimension of the texture image, and <b> is
1744 * the size of the border of that texture image, otherwise
1745 * INVALID_VALUE is generated (border is not applied to dimensions that
1746 * don't exist in a given texture target)."
1748 image
= t
->Image
[0][level
];
1757 /* The GL_ARB_invalidate_subdata spec says:
1759 * "For texture targets that don't have certain dimensions, this
1760 * command treats those dimensions as having a size of 1. For
1761 * example, to invalidate a portion of a two-dimensional texture,
1762 * the application would use <zoffset> equal to zero and <depth>
1765 switch (t
->Target
) {
1766 case GL_TEXTURE_BUFFER
:
1775 xBorder
= image
->Border
;
1778 imageWidth
= image
->Width
;
1782 case GL_TEXTURE_1D_ARRAY
:
1783 xBorder
= image
->Border
;
1786 imageWidth
= image
->Width
;
1787 imageHeight
= image
->Height
;
1791 case GL_TEXTURE_CUBE_MAP
:
1792 case GL_TEXTURE_RECTANGLE
:
1793 case GL_TEXTURE_2D_MULTISAMPLE
:
1794 xBorder
= image
->Border
;
1795 yBorder
= image
->Border
;
1797 imageWidth
= image
->Width
;
1798 imageHeight
= image
->Height
;
1801 case GL_TEXTURE_2D_ARRAY
:
1802 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1803 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1804 xBorder
= image
->Border
;
1805 yBorder
= image
->Border
;
1807 imageWidth
= image
->Width
;
1808 imageHeight
= image
->Height
;
1809 imageDepth
= image
->Depth
;
1812 xBorder
= image
->Border
;
1813 yBorder
= image
->Border
;
1814 zBorder
= image
->Border
;
1815 imageWidth
= image
->Width
;
1816 imageHeight
= image
->Height
;
1817 imageDepth
= image
->Depth
;
1820 assert(!"Should not get here.");
1830 if (xoffset
< -xBorder
) {
1831 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
1835 if (xoffset
+ width
> imageWidth
+ xBorder
) {
1836 _mesa_error(ctx
, GL_INVALID_VALUE
,
1837 "glInvalidateSubTexImage(xoffset+width)");
1841 if (yoffset
< -yBorder
) {
1842 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
1846 if (yoffset
+ height
> imageHeight
+ yBorder
) {
1847 _mesa_error(ctx
, GL_INVALID_VALUE
,
1848 "glInvalidateSubTexImage(yoffset+height)");
1852 if (zoffset
< -zBorder
) {
1853 _mesa_error(ctx
, GL_INVALID_VALUE
,
1854 "glInvalidateSubTexImage(zoffset)");
1858 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
1859 _mesa_error(ctx
, GL_INVALID_VALUE
,
1860 "glInvalidateSubTexImage(zoffset+depth)");
1865 /* We don't actually do anything for this yet. Just return after
1866 * validating the parameters and generating the required errors.
1873 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
1875 GET_CURRENT_CONTEXT(ctx
);
1877 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1878 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
1880 invalidate_tex_image_error_check(ctx
, texture
, level
,
1881 "glInvalidateTexImage");
1883 /* We don't actually do anything for this yet. Just return after
1884 * validating the parameters and generating the required errors.