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.
32 #include "bufferobj.h"
40 #include "shaderimage.h"
45 #include "program/prog_instruction.h"
46 #include "texturebindless.h"
50 /**********************************************************************/
51 /** \name Internal functions */
55 * This function checks for all valid combinations of Min and Mag filters for
56 * Float types, when extensions like OES_texture_float and
57 * OES_texture_float_linear are supported. OES_texture_float mentions support
58 * for NEAREST, NEAREST_MIPMAP_NEAREST magnification and minification filters.
59 * Mag filters like LINEAR and min filters like NEAREST_MIPMAP_LINEAR,
60 * LINEAR_MIPMAP_NEAREST and LINEAR_MIPMAP_LINEAR are only valid in case
61 * OES_texture_float_linear is supported.
63 * Returns true in case the filter is valid for given Float type else false.
66 valid_filter_for_float(const struct gl_context
*ctx
,
67 const struct gl_texture_object
*obj
)
69 switch (obj
->Sampler
.MagFilter
) {
71 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
73 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
77 case GL_NEAREST_MIPMAP_NEAREST
:
80 unreachable("Invalid mag filter");
83 switch (obj
->Sampler
.MinFilter
) {
85 case GL_NEAREST_MIPMAP_LINEAR
:
86 case GL_LINEAR_MIPMAP_NEAREST
:
87 case GL_LINEAR_MIPMAP_LINEAR
:
88 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
90 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
94 case GL_NEAREST_MIPMAP_NEAREST
:
97 unreachable("Invalid min filter");
104 * Return the gl_texture_object for a given ID.
106 struct gl_texture_object
*
107 _mesa_lookup_texture(struct gl_context
*ctx
, GLuint id
)
109 return (struct gl_texture_object
*)
110 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
114 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
115 * is not in the hash table. After calling _mesa_error, it returns NULL.
117 struct gl_texture_object
*
118 _mesa_lookup_texture_err(struct gl_context
*ctx
, GLuint id
, const char* func
)
120 struct gl_texture_object
*texObj
= NULL
;
123 texObj
= _mesa_lookup_texture(ctx
, id
); /* Returns NULL if not found. */
126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(texture)", func
);
132 struct gl_texture_object
*
133 _mesa_lookup_texture_locked(struct gl_context
*ctx
, GLuint id
)
135 return (struct gl_texture_object
*)
136 _mesa_HashLookupLocked(ctx
->Shared
->TexObjects
, id
);
140 * Return a pointer to the current texture object for the given target
141 * on the current texture unit.
142 * Note: all <target> error checking should have been done by this point.
144 struct gl_texture_object
*
145 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
147 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
148 const GLboolean arrayTex
= ctx
->Extensions
.EXT_texture_array
;
152 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
153 case GL_PROXY_TEXTURE_1D
:
154 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
156 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
157 case GL_PROXY_TEXTURE_2D
:
158 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
160 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
161 case GL_PROXY_TEXTURE_3D
:
162 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
163 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
164 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
165 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
166 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
167 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
168 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
169 case GL_TEXTURE_CUBE_MAP
:
170 return ctx
->Extensions
.ARB_texture_cube_map
171 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
172 case GL_PROXY_TEXTURE_CUBE_MAP
:
173 return ctx
->Extensions
.ARB_texture_cube_map
174 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
175 case GL_TEXTURE_CUBE_MAP_ARRAY
:
176 return _mesa_has_texture_cube_map_array(ctx
)
177 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
178 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
179 return _mesa_has_texture_cube_map_array(ctx
)
180 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
181 case GL_TEXTURE_RECTANGLE_NV
:
182 return ctx
->Extensions
.NV_texture_rectangle
183 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
184 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
185 return ctx
->Extensions
.NV_texture_rectangle
186 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
187 case GL_TEXTURE_1D_ARRAY_EXT
:
188 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
189 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
190 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
191 case GL_TEXTURE_2D_ARRAY_EXT
:
192 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
193 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
194 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
195 case GL_TEXTURE_BUFFER
:
196 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
197 _mesa_has_OES_texture_buffer(ctx
)) ?
198 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
199 case GL_TEXTURE_EXTERNAL_OES
:
200 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
201 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
202 case GL_TEXTURE_2D_MULTISAMPLE
:
203 return ctx
->Extensions
.ARB_texture_multisample
204 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
205 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
206 return ctx
->Extensions
.ARB_texture_multisample
207 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
208 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
209 return ctx
->Extensions
.ARB_texture_multisample
210 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
211 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
212 return ctx
->Extensions
.ARB_texture_multisample
213 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
215 _mesa_problem(NULL
, "bad target in _mesa_get_current_tex_object()");
222 * Get the texture object for given target and texunit
223 * Proxy targets are accepted only allowProxyTarget is true.
224 * Return NULL if any error (and record the error).
226 struct gl_texture_object
*
227 _mesa_get_texobj_by_target_and_texunit(struct gl_context
*ctx
, GLenum target
,
228 GLuint texunit
, bool allowProxyTarget
,
231 struct gl_texture_unit
*texUnit
;
234 if (_mesa_is_proxy_texture(target
) && allowProxyTarget
) {
235 return _mesa_get_current_tex_object(ctx
, target
);
238 if (texunit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
239 _mesa_error(ctx
, GL_INVALID_OPERATION
,
240 "%s(texunit=%d)", caller
, texunit
);
244 texUnit
= _mesa_get_tex_unit(ctx
, texunit
);
246 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
247 if (targetIndex
< 0 || targetIndex
== TEXTURE_BUFFER_INDEX
) {
248 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
251 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
253 return texUnit
->CurrentTex
[targetIndex
];
258 * Allocate and initialize a new texture object. But don't put it into the
259 * texture object hash table.
261 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
264 * \param shared the shared GL state structure to contain the texture object
265 * \param name integer name for the texture object
266 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
267 * GL_TEXTURE_CUBE_MAP or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
270 * \return pointer to new texture object.
272 struct gl_texture_object
*
273 _mesa_new_texture_object(struct gl_context
*ctx
, GLuint name
, GLenum target
)
275 struct gl_texture_object
*obj
;
277 obj
= MALLOC_STRUCT(gl_texture_object
);
281 _mesa_initialize_texture_object(ctx
, obj
, name
, target
);
287 * Initialize a new texture object to default values.
288 * \param obj the texture object
289 * \param name the texture name
290 * \param target the texture target
293 _mesa_initialize_texture_object( struct gl_context
*ctx
,
294 struct gl_texture_object
*obj
,
295 GLuint name
, GLenum target
)
297 assert(target
== 0 ||
298 target
== GL_TEXTURE_1D
||
299 target
== GL_TEXTURE_2D
||
300 target
== GL_TEXTURE_3D
||
301 target
== GL_TEXTURE_CUBE_MAP
||
302 target
== GL_TEXTURE_RECTANGLE_NV
||
303 target
== GL_TEXTURE_1D_ARRAY_EXT
||
304 target
== GL_TEXTURE_2D_ARRAY_EXT
||
305 target
== GL_TEXTURE_EXTERNAL_OES
||
306 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
307 target
== GL_TEXTURE_BUFFER
||
308 target
== GL_TEXTURE_2D_MULTISAMPLE
||
309 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
311 GLenum filter
= GL_LINEAR
;
313 memset(obj
, 0, sizeof(*obj
));
314 /* init the non-zero fields */
315 simple_mtx_init(&obj
->Mutex
, mtx_plain
);
318 obj
->Target
= target
;
320 obj
->TargetIndex
= _mesa_tex_target_to_index(ctx
, target
);
323 obj
->TargetIndex
= NUM_TEXTURE_TARGETS
; /* invalid/error value */
325 obj
->Priority
= 1.0F
;
327 obj
->MaxLevel
= 1000;
329 /* must be one; no support for (YUV) planes in separate buffers */
330 obj
->RequiredTextureImageUnits
= 1;
334 case GL_TEXTURE_2D_MULTISAMPLE
:
335 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
339 case GL_TEXTURE_RECTANGLE_NV
:
340 case GL_TEXTURE_EXTERNAL_OES
:
341 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
342 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
343 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
344 obj
->Sampler
.MinFilter
= filter
;
345 obj
->Sampler
.MagFilter
= filter
;
349 obj
->Sampler
.WrapS
= GL_REPEAT
;
350 obj
->Sampler
.WrapT
= GL_REPEAT
;
351 obj
->Sampler
.WrapR
= GL_REPEAT
;
352 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
353 obj
->Sampler
.MagFilter
= GL_LINEAR
;
357 obj
->Sampler
.MinLod
= -1000.0;
358 obj
->Sampler
.MaxLod
= 1000.0;
359 obj
->Sampler
.LodBias
= 0.0;
360 obj
->Sampler
.MaxAnisotropy
= 1.0;
361 obj
->Sampler
.CompareMode
= GL_NONE
; /* ARB_shadow */
362 obj
->Sampler
.CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
363 obj
->DepthMode
= ctx
->API
== API_OPENGL_CORE
? GL_RED
: GL_LUMINANCE
;
364 obj
->StencilSampling
= false;
365 obj
->Sampler
.CubeMapSeamless
= GL_FALSE
;
366 obj
->Sampler
.HandleAllocated
= GL_FALSE
;
367 obj
->Swizzle
[0] = GL_RED
;
368 obj
->Swizzle
[1] = GL_GREEN
;
369 obj
->Swizzle
[2] = GL_BLUE
;
370 obj
->Swizzle
[3] = GL_ALPHA
;
371 obj
->_Swizzle
= SWIZZLE_NOOP
;
372 obj
->Sampler
.sRGBDecode
= GL_DECODE_EXT
;
373 obj
->BufferObjectFormat
= GL_R8
;
374 obj
->_BufferObjectFormat
= MESA_FORMAT_R_UNORM8
;
375 obj
->ImageFormatCompatibilityType
= GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE
;
377 /* GL_ARB_bindless_texture */
378 _mesa_init_texture_handles(obj
);
383 * Some texture initialization can't be finished until we know which
384 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
387 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
388 struct gl_texture_object
*obj
, int targetIndex
)
390 GLenum filter
= GL_LINEAR
;
391 assert(obj
->Target
== 0);
393 obj
->Target
= target
;
394 obj
->TargetIndex
= targetIndex
;
395 assert(obj
->TargetIndex
< NUM_TEXTURE_TARGETS
);
398 case GL_TEXTURE_2D_MULTISAMPLE
:
399 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
403 case GL_TEXTURE_RECTANGLE_NV
:
404 case GL_TEXTURE_EXTERNAL_OES
:
405 /* have to init wrap and filter state here - kind of klunky */
406 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
407 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
408 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
409 obj
->Sampler
.MinFilter
= filter
;
410 obj
->Sampler
.MagFilter
= filter
;
411 if (ctx
->Driver
.TexParameter
) {
412 /* XXX we probably don't need to make all these calls */
413 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_S
);
414 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_T
);
415 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_R
);
416 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_MIN_FILTER
);
417 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_MAG_FILTER
);
422 /* nothing needs done */
429 * Deallocate a texture object struct. It should have already been
430 * removed from the texture object pool.
431 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
433 * \param shared the shared GL state to which the object belongs.
434 * \param texObj the texture object to delete.
437 _mesa_delete_texture_object(struct gl_context
*ctx
,
438 struct gl_texture_object
*texObj
)
442 /* Set Target to an invalid value. With some assertions elsewhere
443 * we can try to detect possible use of deleted textures.
445 texObj
->Target
= 0x99;
447 /* free the texture images */
448 for (face
= 0; face
< 6; face
++) {
449 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
450 if (texObj
->Image
[face
][i
]) {
451 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
456 /* Delete all texture/image handles. */
457 _mesa_delete_texture_handles(ctx
, texObj
);
459 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
461 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
462 simple_mtx_destroy(&texObj
->Mutex
);
466 /* free this object */
472 * Copy texture object state from one texture object to another.
473 * Use for glPush/PopAttrib.
475 * \param dest destination texture object.
476 * \param src source texture object.
479 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
480 const struct gl_texture_object
*src
)
482 dest
->Target
= src
->Target
;
483 dest
->TargetIndex
= src
->TargetIndex
;
484 dest
->Name
= src
->Name
;
485 dest
->Priority
= src
->Priority
;
486 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
487 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
488 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
489 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
490 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
491 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
492 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
493 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
494 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
495 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
496 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
497 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
498 dest
->BaseLevel
= src
->BaseLevel
;
499 dest
->MaxLevel
= src
->MaxLevel
;
500 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
501 dest
->Sampler
.CompareMode
= src
->Sampler
.CompareMode
;
502 dest
->Sampler
.CompareFunc
= src
->Sampler
.CompareFunc
;
503 dest
->Sampler
.CubeMapSeamless
= src
->Sampler
.CubeMapSeamless
;
504 dest
->DepthMode
= src
->DepthMode
;
505 dest
->StencilSampling
= src
->StencilSampling
;
506 dest
->Sampler
.sRGBDecode
= src
->Sampler
.sRGBDecode
;
507 dest
->_MaxLevel
= src
->_MaxLevel
;
508 dest
->_MaxLambda
= src
->_MaxLambda
;
509 dest
->GenerateMipmap
= src
->GenerateMipmap
;
510 dest
->_BaseComplete
= src
->_BaseComplete
;
511 dest
->_MipmapComplete
= src
->_MipmapComplete
;
512 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
513 dest
->_Swizzle
= src
->_Swizzle
;
514 dest
->_IsHalfFloat
= src
->_IsHalfFloat
;
515 dest
->_IsFloat
= src
->_IsFloat
;
517 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
522 * Free all texture images of the given texture objectm, except for
525 * \param ctx GL context.
526 * \param texObj texture object.
527 * \param retainTexImage a texture image that will \em not be freed.
529 * \sa _mesa_clear_texture_image().
532 _mesa_clear_texture_object(struct gl_context
*ctx
,
533 struct gl_texture_object
*texObj
,
534 struct gl_texture_image
*retainTexImage
)
538 if (texObj
->Target
== 0)
541 for (i
= 0; i
< MAX_FACES
; i
++) {
542 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
543 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
544 if (texImage
&& texImage
!= retainTexImage
)
545 _mesa_clear_texture_image(ctx
, texImage
);
552 * Check if the given texture object is valid by examining its Target field.
553 * For debugging only.
556 valid_texture_object(const struct gl_texture_object
*tex
)
558 switch (tex
->Target
) {
563 case GL_TEXTURE_CUBE_MAP
:
564 case GL_TEXTURE_RECTANGLE_NV
:
565 case GL_TEXTURE_1D_ARRAY_EXT
:
566 case GL_TEXTURE_2D_ARRAY_EXT
:
567 case GL_TEXTURE_BUFFER
:
568 case GL_TEXTURE_EXTERNAL_OES
:
569 case GL_TEXTURE_CUBE_MAP_ARRAY
:
570 case GL_TEXTURE_2D_MULTISAMPLE
:
571 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
574 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
577 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
578 tex
->Target
, tex
->Name
);
585 * Reference (or unreference) a texture object.
586 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
587 * If 'tex' is non-null, increment its refcount.
588 * This is normally only called from the _mesa_reference_texobj() macro
589 * when there's a real pointer change.
592 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
593 struct gl_texture_object
*tex
)
598 /* Unreference the old texture */
599 GLboolean deleteFlag
= GL_FALSE
;
600 struct gl_texture_object
*oldTex
= *ptr
;
602 assert(valid_texture_object(oldTex
));
603 (void) valid_texture_object
; /* silence warning in release builds */
605 simple_mtx_lock(&oldTex
->Mutex
);
606 assert(oldTex
->RefCount
> 0);
609 deleteFlag
= (oldTex
->RefCount
== 0);
610 simple_mtx_unlock(&oldTex
->Mutex
);
613 /* Passing in the context drastically changes the driver code for
614 * framebuffer deletion.
616 GET_CURRENT_CONTEXT(ctx
);
618 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
620 _mesa_problem(NULL
, "Unable to delete texture, no context");
628 /* reference new texture */
629 assert(valid_texture_object(tex
));
630 simple_mtx_lock(&tex
->Mutex
);
631 assert(tex
->RefCount
> 0);
635 simple_mtx_unlock(&tex
->Mutex
);
640 enum base_mipmap
{ BASE
, MIPMAP
};
644 * Mark a texture object as incomplete. There are actually three kinds of
646 * 1. "base incomplete": the base level of the texture is invalid so no
647 * texturing is possible.
648 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
649 * mipmap filtering isn't possible, but non-mipmap filtering is.
650 * 3. "texture incompleteness": some combination of texture state and
651 * sampler state renders the texture incomplete.
653 * \param t texture object
654 * \param bm either BASE or MIPMAP to indicate what's incomplete
655 * \param fmt... string describing why it's incomplete (for debugging).
658 incomplete(struct gl_texture_object
*t
, enum base_mipmap bm
,
659 const char *fmt
, ...)
661 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_TEXTURE
) {
666 vsnprintf(s
, sizeof(s
), fmt
, args
);
669 _mesa_debug(NULL
, "Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
673 t
->_BaseComplete
= GL_FALSE
;
674 t
->_MipmapComplete
= GL_FALSE
;
679 * Examine a texture object to determine if it is complete.
681 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
684 * \param ctx GL context.
685 * \param t texture object.
687 * According to the texture target, verifies that each of the mipmaps is
688 * present and has the expected size.
691 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
692 struct gl_texture_object
*t
)
694 const GLint baseLevel
= t
->BaseLevel
;
695 const struct gl_texture_image
*baseImage
;
698 /* We'll set these to FALSE if tests fail below */
699 t
->_BaseComplete
= GL_TRUE
;
700 t
->_MipmapComplete
= GL_TRUE
;
702 if (t
->Target
== GL_TEXTURE_BUFFER
) {
703 /* Buffer textures are always considered complete. The obvious case where
704 * they would be incomplete (no BO attached) is actually specced to be
705 * undefined rendering results.
710 /* Detect cases where the application set the base level to an invalid
713 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
714 incomplete(t
, BASE
, "base level = %d is invalid", baseLevel
);
718 if (t
->MaxLevel
< baseLevel
) {
719 incomplete(t
, MIPMAP
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
720 t
->MaxLevel
, baseLevel
);
724 baseImage
= t
->Image
[0][baseLevel
];
726 /* Always need the base level image */
728 incomplete(t
, BASE
, "Image[baseLevel=%d] == NULL", baseLevel
);
732 /* Check width/height/depth for zero */
733 if (baseImage
->Width
== 0 ||
734 baseImage
->Height
== 0 ||
735 baseImage
->Depth
== 0) {
736 incomplete(t
, BASE
, "texture width or height or depth = 0");
740 /* Check if the texture values are integer */
742 GLenum datatype
= _mesa_get_format_datatype(baseImage
->TexFormat
);
743 t
->_IsIntegerFormat
= datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
;
746 /* Check if the texture type is Float or HalfFloatOES and ensure Min and Mag
747 * filters are supported in this case.
749 if (_mesa_is_gles(ctx
) && !valid_filter_for_float(ctx
, t
)) {
750 incomplete(t
, BASE
, "Filter is not supported with Float types.");
754 maxLevels
= _mesa_max_texture_levels(ctx
, t
->Target
);
755 if (maxLevels
== 0) {
756 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
760 assert(maxLevels
> 0);
762 t
->_MaxLevel
= MIN3(t
->MaxLevel
,
763 /* 'p' in the GL spec */
764 (int) (baseLevel
+ baseImage
->MaxNumLevels
- 1),
765 /* 'q' in the GL spec */
769 /* Adjust max level for views: the data store may have more levels than
772 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->NumLevels
- 1);
775 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
776 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- baseLevel
);
779 /* This texture object was created with glTexStorage1/2/3D() so we
780 * know that all the mipmap levels are the right size and all cube
781 * map faces are the same size.
782 * We don't need to do any of the additional checks below.
787 if (t
->Target
== GL_TEXTURE_CUBE_MAP
) {
788 /* Make sure that all six cube map level 0 images are the same size and
790 * Note: we know that the image's width==height (we enforce that
791 * at glTexImage time) so we only need to test the width here.
794 assert(baseImage
->Width2
== baseImage
->Height
);
795 for (face
= 1; face
< 6; face
++) {
796 assert(t
->Image
[face
][baseLevel
] == NULL
||
797 t
->Image
[face
][baseLevel
]->Width2
==
798 t
->Image
[face
][baseLevel
]->Height2
);
799 if (t
->Image
[face
][baseLevel
] == NULL
||
800 t
->Image
[face
][baseLevel
]->Width2
!= baseImage
->Width2
) {
801 incomplete(t
, BASE
, "Cube face missing or mismatched size");
804 if (t
->Image
[face
][baseLevel
]->InternalFormat
!=
805 baseImage
->InternalFormat
) {
806 incomplete(t
, BASE
, "Cube face format mismatch");
809 if (t
->Image
[face
][baseLevel
]->Border
!= baseImage
->Border
) {
810 incomplete(t
, BASE
, "Cube face border size mismatch");
817 * Do mipmap consistency checking.
818 * Note: we don't care about the current texture sampler state here.
819 * To determine texture completeness we'll either look at _BaseComplete
820 * or _MipmapComplete depending on the current minification filter mode.
824 const GLint minLevel
= baseLevel
;
825 const GLint maxLevel
= t
->_MaxLevel
;
826 const GLuint numFaces
= _mesa_num_tex_faces(t
->Target
);
827 GLuint width
, height
, depth
, face
;
829 if (minLevel
> maxLevel
) {
830 incomplete(t
, MIPMAP
, "minLevel > maxLevel");
834 /* Get the base image's dimensions */
835 width
= baseImage
->Width2
;
836 height
= baseImage
->Height2
;
837 depth
= baseImage
->Depth2
;
839 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
840 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
842 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
843 /* Compute the expected size of image at level[i] */
847 if (height
> 1 && t
->Target
!= GL_TEXTURE_1D_ARRAY
) {
850 if (depth
> 1 && t
->Target
!= GL_TEXTURE_2D_ARRAY
851 && t
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
855 /* loop over cube faces (or single face otherwise) */
856 for (face
= 0; face
< numFaces
; face
++) {
857 if (i
>= minLevel
&& i
<= maxLevel
) {
858 const struct gl_texture_image
*img
= t
->Image
[face
][i
];
861 incomplete(t
, MIPMAP
, "TexImage[%d] is missing", i
);
864 if (img
->InternalFormat
!= baseImage
->InternalFormat
) {
865 incomplete(t
, MIPMAP
, "Format[i] != Format[baseLevel]");
868 if (img
->Border
!= baseImage
->Border
) {
869 incomplete(t
, MIPMAP
, "Border[i] != Border[baseLevel]");
872 if (img
->Width2
!= width
) {
873 incomplete(t
, MIPMAP
, "TexImage[%d] bad width %u", i
,
877 if (img
->Height2
!= height
) {
878 incomplete(t
, MIPMAP
, "TexImage[%d] bad height %u", i
,
882 if (img
->Depth2
!= depth
) {
883 incomplete(t
, MIPMAP
, "TexImage[%d] bad depth %u", i
,
890 if (width
== 1 && height
== 1 && depth
== 1) {
891 return; /* found smallest needed mipmap, all done! */
899 _mesa_cube_level_complete(const struct gl_texture_object
*texObj
,
902 const struct gl_texture_image
*img0
, *img
;
905 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
908 if ((level
< 0) || (level
>= MAX_TEXTURE_LEVELS
))
911 /* check first face */
912 img0
= texObj
->Image
[0][level
];
915 img0
->Width
!= img0
->Height
)
918 /* check remaining faces vs. first face */
919 for (face
= 1; face
< 6; face
++) {
920 img
= texObj
->Image
[face
][level
];
922 img
->Width
!= img0
->Width
||
923 img
->Height
!= img0
->Height
||
924 img
->TexFormat
!= img0
->TexFormat
)
932 * Check if the given cube map texture is "cube complete" as defined in
933 * the OpenGL specification.
936 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
938 return _mesa_cube_level_complete(texObj
, texObj
->BaseLevel
);
942 * Mark a texture object dirty. It forces the object to be incomplete
943 * and forces the context to re-validate its state.
945 * \param ctx GL context.
946 * \param texObj texture object.
949 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
951 texObj
->_BaseComplete
= GL_FALSE
;
952 texObj
->_MipmapComplete
= GL_FALSE
;
953 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
958 * Return pointer to a default/fallback texture of the given type/target.
959 * The texture is an RGBA texture with all texels = (0,0,0,1).
960 * That's the value a GLSL sampler should get when sampling from an
961 * incomplete texture.
963 struct gl_texture_object
*
964 _mesa_get_fallback_texture(struct gl_context
*ctx
, gl_texture_index tex
)
966 if (!ctx
->Shared
->FallbackTex
[tex
]) {
967 /* create fallback texture now */
968 const GLsizei width
= 1, height
= 1;
971 struct gl_texture_object
*texObj
;
972 struct gl_texture_image
*texImage
;
973 mesa_format texFormat
;
974 GLuint dims
, face
, numFaces
= 1;
977 for (face
= 0; face
< 6; face
++) {
980 texel
[4*face
+ 2] = 0x0;
981 texel
[4*face
+ 3] = 0xff;
985 case TEXTURE_2D_ARRAY_INDEX
:
987 target
= GL_TEXTURE_2D_ARRAY
;
989 case TEXTURE_1D_ARRAY_INDEX
:
991 target
= GL_TEXTURE_1D_ARRAY
;
993 case TEXTURE_CUBE_INDEX
:
995 target
= GL_TEXTURE_CUBE_MAP
;
998 case TEXTURE_3D_INDEX
:
1000 target
= GL_TEXTURE_3D
;
1002 case TEXTURE_RECT_INDEX
:
1004 target
= GL_TEXTURE_RECTANGLE
;
1006 case TEXTURE_2D_INDEX
:
1008 target
= GL_TEXTURE_2D
;
1010 case TEXTURE_1D_INDEX
:
1012 target
= GL_TEXTURE_1D
;
1014 case TEXTURE_BUFFER_INDEX
:
1016 target
= GL_TEXTURE_BUFFER
;
1018 case TEXTURE_CUBE_ARRAY_INDEX
:
1020 target
= GL_TEXTURE_CUBE_MAP_ARRAY
;
1023 case TEXTURE_EXTERNAL_INDEX
:
1025 target
= GL_TEXTURE_EXTERNAL_OES
;
1027 case TEXTURE_2D_MULTISAMPLE_INDEX
:
1029 target
= GL_TEXTURE_2D_MULTISAMPLE
;
1031 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
:
1033 target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
1040 /* create texture object */
1041 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, target
);
1045 assert(texObj
->RefCount
== 1);
1046 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1047 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1049 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
,
1053 /* need a loop here just for cube maps */
1054 for (face
= 0; face
< numFaces
; face
++) {
1055 const GLenum faceTarget
= _mesa_cube_face_target(target
, face
);
1057 /* initialize level[0] texture image */
1058 texImage
= _mesa_get_tex_image(ctx
, texObj
, faceTarget
, 0);
1060 _mesa_init_teximage_fields(ctx
, texImage
,
1062 (dims
> 1) ? height
: 1,
1063 (dims
> 2) ? depth
: 1,
1065 GL_RGBA
, texFormat
);
1067 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
1068 GL_RGBA
, GL_UNSIGNED_BYTE
, texel
,
1069 &ctx
->DefaultPacking
);
1072 _mesa_test_texobj_completeness(ctx
, texObj
);
1073 assert(texObj
->_BaseComplete
);
1074 assert(texObj
->_MipmapComplete
);
1076 ctx
->Shared
->FallbackTex
[tex
] = texObj
;
1078 /* Complete the driver's operation in case another context will also
1079 * use the same fallback texture. */
1080 if (ctx
->Driver
.Finish
)
1081 ctx
->Driver
.Finish(ctx
);
1083 return ctx
->Shared
->FallbackTex
[tex
];
1088 * Compute the size of the given texture object, in bytes.
1091 texture_size(const struct gl_texture_object
*texObj
)
1093 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1094 GLuint face
, level
, size
= 0;
1096 for (face
= 0; face
< numFaces
; face
++) {
1097 for (level
= 0; level
< MAX_TEXTURE_LEVELS
; level
++) {
1098 const struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
1100 GLuint sz
= _mesa_format_image_size(img
->TexFormat
, img
->Width
,
1101 img
->Height
, img
->Depth
);
1112 * Callback called from _mesa_HashWalk()
1115 count_tex_size(GLuint key
, void *data
, void *userData
)
1117 const struct gl_texture_object
*texObj
=
1118 (const struct gl_texture_object
*) data
;
1119 GLuint
*total
= (GLuint
*) userData
;
1123 *total
= *total
+ texture_size(texObj
);
1128 * Compute total size (in bytes) of all textures for the given context.
1129 * For debugging purposes.
1132 _mesa_total_texture_memory(struct gl_context
*ctx
)
1134 GLuint tgt
, total
= 0;
1136 _mesa_HashWalk(ctx
->Shared
->TexObjects
, count_tex_size
, &total
);
1138 /* plus, the default texture objects */
1139 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1140 total
+= texture_size(ctx
->Shared
->DefaultTex
[tgt
]);
1148 * Return the base format for the given texture object by looking
1149 * at the base texture image.
1150 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
1153 _mesa_texture_base_format(const struct gl_texture_object
*texObj
)
1155 const struct gl_texture_image
*texImage
= _mesa_base_tex_image(texObj
);
1157 return texImage
? texImage
->_BaseFormat
: GL_NONE
;
1161 static struct gl_texture_object
*
1162 invalidate_tex_image_error_check(struct gl_context
*ctx
, GLuint texture
,
1163 GLint level
, const char *name
)
1165 /* The GL_ARB_invalidate_subdata spec says:
1167 * "If <texture> is zero or is not the name of a texture, the error
1168 * INVALID_VALUE is generated."
1170 * This performs the error check in a different order than listed in the
1171 * spec. We have to get the texture object before we can validate the
1172 * other parameters against values in the texture object.
1174 struct gl_texture_object
*const t
= _mesa_lookup_texture(ctx
, texture
);
1175 if (texture
== 0 || t
== NULL
) {
1176 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(texture)", name
);
1180 /* The GL_ARB_invalidate_subdata spec says:
1182 * "If <level> is less than zero or greater than the base 2 logarithm
1183 * of the maximum texture width, height, or depth, the error
1184 * INVALID_VALUE is generated."
1186 if (level
< 0 || level
> t
->MaxLevel
) {
1187 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1191 /* The GL_ARB_invalidate_subdata spec says:
1193 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1194 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1195 * is not zero, the error INVALID_VALUE is generated."
1198 switch (t
->Target
) {
1199 case GL_TEXTURE_RECTANGLE
:
1200 case GL_TEXTURE_BUFFER
:
1201 case GL_TEXTURE_2D_MULTISAMPLE
:
1202 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1203 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1216 * Helper function for glCreateTextures and glGenTextures. Need this because
1217 * glCreateTextures should throw errors if target = 0. This is not exposed to
1218 * the rest of Mesa to encourage Mesa internals to use nameless textures,
1219 * which do not require expensive hash lookups.
1220 * \param target either 0 or a valid / error-checked texture target enum
1223 create_textures(struct gl_context
*ctx
, GLenum target
,
1224 GLsizei n
, GLuint
*textures
, const char *caller
)
1233 * This must be atomic (generation and allocation of texture IDs)
1235 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
1237 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
1239 /* Allocate new, empty texture objects */
1240 for (i
= 0; i
< n
; i
++) {
1241 struct gl_texture_object
*texObj
;
1242 GLuint name
= first
+ i
;
1243 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
1245 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
1246 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1250 /* insert into hash table */
1251 _mesa_HashInsertLocked(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
1256 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
1261 create_textures_err(struct gl_context
*ctx
, GLenum target
,
1262 GLsizei n
, GLuint
*textures
, const char *caller
)
1264 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1265 _mesa_debug(ctx
, "%s %d\n", caller
, n
);
1268 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
1272 create_textures(ctx
, target
, n
, textures
, caller
);
1278 /***********************************************************************/
1279 /** \name API functions */
1284 * Generate texture names.
1286 * \param n number of texture names to be generated.
1287 * \param textures an array in which will hold the generated texture names.
1289 * \sa glGenTextures(), glCreateTextures().
1291 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1292 * IDs which are stored in \p textures. Corresponding empty texture
1293 * objects are also generated.
1296 _mesa_GenTextures_no_error(GLsizei n
, GLuint
*textures
)
1298 GET_CURRENT_CONTEXT(ctx
);
1299 create_textures(ctx
, 0, n
, textures
, "glGenTextures");
1304 _mesa_GenTextures(GLsizei n
, GLuint
*textures
)
1306 GET_CURRENT_CONTEXT(ctx
);
1307 create_textures_err(ctx
, 0, n
, textures
, "glGenTextures");
1311 * Create texture objects.
1313 * \param target the texture target for each name to be generated.
1314 * \param n number of texture names to be generated.
1315 * \param textures an array in which will hold the generated texture names.
1317 * \sa glCreateTextures(), glGenTextures().
1319 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1320 * IDs which are stored in \p textures. Corresponding empty texture
1321 * objects are also generated.
1324 _mesa_CreateTextures_no_error(GLenum target
, GLsizei n
, GLuint
*textures
)
1326 GET_CURRENT_CONTEXT(ctx
);
1327 create_textures(ctx
, target
, n
, textures
, "glCreateTextures");
1332 _mesa_CreateTextures(GLenum target
, GLsizei n
, GLuint
*textures
)
1335 GET_CURRENT_CONTEXT(ctx
);
1338 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1339 * glCreateTextures should do with invalid targets, which was probably an
1340 * oversight. This conforms to the spec for glBindTexture.
1342 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1343 if (targetIndex
< 0) {
1344 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCreateTextures(target)");
1348 create_textures_err(ctx
, target
, n
, textures
, "glCreateTextures");
1352 * Check if the given texture object is bound to the current draw or
1353 * read framebuffer. If so, Unbind it.
1356 unbind_texobj_from_fbo(struct gl_context
*ctx
,
1357 struct gl_texture_object
*texObj
)
1359 bool progress
= false;
1361 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1362 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1365 * "If a texture object is deleted while its image is attached to one
1366 * or more attachment points in the currently bound framebuffer, then
1367 * it is as if FramebufferTexture* had been called, with a texture of
1368 * zero, for each attachment point to which this image was attached in
1369 * the currently bound framebuffer. In other words, this texture image
1370 * is first detached from all attachment points in the currently bound
1371 * framebuffer. Note that the texture image is specifically not
1372 * detached from any other framebuffer objects. Detaching the texture
1373 * image from any other framebuffer objects is the responsibility of
1376 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1377 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, texObj
);
1379 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1380 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1381 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, texObj
)
1386 /* Vertices are already flushed by _mesa_DeleteTextures */
1387 ctx
->NewState
|= _NEW_BUFFERS
;
1392 * Check if the given texture object is bound to any texture image units and
1393 * unbind it if so (revert to default textures).
1396 unbind_texobj_from_texunits(struct gl_context
*ctx
,
1397 struct gl_texture_object
*texObj
)
1399 const gl_texture_index index
= texObj
->TargetIndex
;
1402 if (texObj
->Target
== 0) {
1403 /* texture was never bound */
1407 assert(index
< NUM_TEXTURE_TARGETS
);
1409 for (u
= 0; u
< ctx
->Texture
.NumCurrentTexUsed
; u
++) {
1410 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
1412 if (texObj
== unit
->CurrentTex
[index
]) {
1413 /* Bind the default texture for this unit/target */
1414 _mesa_reference_texobj(&unit
->CurrentTex
[index
],
1415 ctx
->Shared
->DefaultTex
[index
]);
1416 unit
->_BoundTextures
&= ~(1 << index
);
1423 * Check if the given texture object is bound to any shader image unit
1424 * and unbind it if that's the case.
1427 unbind_texobj_from_image_units(struct gl_context
*ctx
,
1428 struct gl_texture_object
*texObj
)
1432 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; i
++) {
1433 struct gl_image_unit
*unit
= &ctx
->ImageUnits
[i
];
1435 if (texObj
== unit
->TexObj
) {
1436 _mesa_reference_texobj(&unit
->TexObj
, NULL
);
1437 *unit
= _mesa_default_image_unit(ctx
);
1444 * Unbinds all textures bound to the given texture image unit.
1447 unbind_textures_from_unit(struct gl_context
*ctx
, GLuint unit
)
1449 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1451 while (texUnit
->_BoundTextures
) {
1452 const GLuint index
= ffs(texUnit
->_BoundTextures
) - 1;
1453 struct gl_texture_object
*texObj
= ctx
->Shared
->DefaultTex
[index
];
1455 _mesa_reference_texobj(&texUnit
->CurrentTex
[index
], texObj
);
1457 /* Pass BindTexture call to device driver */
1458 if (ctx
->Driver
.BindTexture
)
1459 ctx
->Driver
.BindTexture(ctx
, unit
, 0, texObj
);
1461 texUnit
->_BoundTextures
&= ~(1 << index
);
1462 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1468 * Delete named textures.
1470 * \param n number of textures to be deleted.
1471 * \param textures array of texture IDs to be deleted.
1473 * \sa glDeleteTextures().
1475 * If we're about to delete a texture that's currently bound to any
1476 * texture unit, unbind the texture first. Decrement the reference
1477 * count on the texture object and delete it if it's zero.
1478 * Recall that texture objects can be shared among several rendering
1482 delete_textures(struct gl_context
*ctx
, GLsizei n
, const GLuint
*textures
)
1484 FLUSH_VERTICES(ctx
, 0); /* too complex */
1489 for (GLsizei i
= 0; i
< n
; i
++) {
1490 if (textures
[i
] > 0) {
1491 struct gl_texture_object
*delObj
1492 = _mesa_lookup_texture(ctx
, textures
[i
]);
1495 _mesa_lock_texture(ctx
, delObj
);
1497 /* Check if texture is bound to any framebuffer objects.
1499 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1501 unbind_texobj_from_fbo(ctx
, delObj
);
1503 /* Check if this texture is currently bound to any texture units.
1506 unbind_texobj_from_texunits(ctx
, delObj
);
1508 /* Check if this texture is currently bound to any shader
1509 * image unit. If so, unbind it.
1510 * See section 3.9.X of GL_ARB_shader_image_load_store.
1512 unbind_texobj_from_image_units(ctx
, delObj
);
1514 /* Make all handles that reference this texture object non-resident
1515 * in the current context.
1517 _mesa_make_texture_handles_non_resident(ctx
, delObj
);
1519 _mesa_unlock_texture(ctx
, delObj
);
1521 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1523 /* The texture _name_ is now free for re-use.
1524 * Remove it from the hash table now.
1526 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
1528 /* Unreference the texobj. If refcount hits zero, the texture
1531 _mesa_reference_texobj(&delObj
, NULL
);
1538 * This deletes a texObj without altering the hash table.
1541 _mesa_delete_nameless_texture(struct gl_context
*ctx
,
1542 struct gl_texture_object
*texObj
)
1547 FLUSH_VERTICES(ctx
, 0);
1549 _mesa_lock_texture(ctx
, texObj
);
1551 /* Check if texture is bound to any framebuffer objects.
1553 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1555 unbind_texobj_from_fbo(ctx
, texObj
);
1557 /* Check if this texture is currently bound to any texture units.
1560 unbind_texobj_from_texunits(ctx
, texObj
);
1562 /* Check if this texture is currently bound to any shader
1563 * image unit. If so, unbind it.
1564 * See section 3.9.X of GL_ARB_shader_image_load_store.
1566 unbind_texobj_from_image_units(ctx
, texObj
);
1568 _mesa_unlock_texture(ctx
, texObj
);
1570 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1572 /* Unreference the texobj. If refcount hits zero, the texture
1575 _mesa_reference_texobj(&texObj
, NULL
);
1580 _mesa_DeleteTextures_no_error(GLsizei n
, const GLuint
*textures
)
1582 GET_CURRENT_CONTEXT(ctx
);
1583 delete_textures(ctx
, n
, textures
);
1588 _mesa_DeleteTextures(GLsizei n
, const GLuint
*textures
)
1590 GET_CURRENT_CONTEXT(ctx
);
1592 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1593 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1596 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n < 0)");
1600 delete_textures(ctx
, n
, textures
);
1605 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1606 * into the corresponding Mesa texture target index.
1607 * Note that proxy targets are not valid here.
1608 * \return TEXTURE_x_INDEX or -1 if target is invalid
1611 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1615 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1617 return TEXTURE_2D_INDEX
;
1619 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1620 case GL_TEXTURE_CUBE_MAP
:
1621 return ctx
->Extensions
.ARB_texture_cube_map
1622 ? TEXTURE_CUBE_INDEX
: -1;
1623 case GL_TEXTURE_RECTANGLE
:
1624 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1625 ? TEXTURE_RECT_INDEX
: -1;
1626 case GL_TEXTURE_1D_ARRAY
:
1627 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1628 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1629 case GL_TEXTURE_2D_ARRAY
:
1630 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1631 || _mesa_is_gles3(ctx
)
1632 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1633 case GL_TEXTURE_BUFFER
:
1634 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
1635 _mesa_has_OES_texture_buffer(ctx
)) ?
1636 TEXTURE_BUFFER_INDEX
: -1;
1637 case GL_TEXTURE_EXTERNAL_OES
:
1638 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1639 ? TEXTURE_EXTERNAL_INDEX
: -1;
1640 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1641 return _mesa_has_texture_cube_map_array(ctx
)
1642 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1643 case GL_TEXTURE_2D_MULTISAMPLE
:
1644 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1645 _mesa_is_gles31(ctx
)) ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1646 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1647 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1648 _mesa_is_gles31(ctx
))
1649 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1657 * Do actual texture binding. All error checking should have been done prior
1658 * to calling this function. Note that the texture target (1D, 2D, etc) is
1659 * always specified by the texObj->TargetIndex.
1661 * \param unit index of texture unit to update
1662 * \param texObj the new texture object (cannot be NULL)
1665 bind_texture_object(struct gl_context
*ctx
, unsigned unit
,
1666 struct gl_texture_object
*texObj
)
1668 struct gl_texture_unit
*texUnit
;
1671 assert(unit
< ARRAY_SIZE(ctx
->Texture
.Unit
));
1672 texUnit
= &ctx
->Texture
.Unit
[unit
];
1675 assert(valid_texture_object(texObj
));
1677 targetIndex
= texObj
->TargetIndex
;
1678 assert(targetIndex
>= 0);
1679 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1681 /* Check if this texture is only used by this context and is already bound.
1682 * If so, just return. For GL_OES_image_external, rebinding the texture
1683 * always must invalidate cached resources.
1685 if (targetIndex
!= TEXTURE_EXTERNAL_INDEX
) {
1687 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1688 early_out
= ((ctx
->Shared
->RefCount
== 1)
1689 && (texObj
== texUnit
->CurrentTex
[targetIndex
]));
1690 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1696 /* flush before changing binding */
1697 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
);
1699 /* If the refcount on the previously bound texture is decremented to
1700 * zero, it'll be deleted here.
1702 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], texObj
);
1704 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1707 if (texObj
->Name
!= 0)
1708 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1710 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1712 /* Pass BindTexture call to device driver */
1713 if (ctx
->Driver
.BindTexture
) {
1714 ctx
->Driver
.BindTexture(ctx
, unit
, texObj
->Target
, texObj
);
1719 * Light-weight bind texture for internal users
1721 * This is really just \c finish_texture_init plus \c bind_texture_object.
1722 * This is intended to be used by internal Mesa functions that use
1723 * \c _mesa_CreateTexture and need to bind textures (e.g., meta).
1726 _mesa_bind_texture(struct gl_context
*ctx
, GLenum target
,
1727 struct gl_texture_object
*tex_obj
)
1729 const GLint targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1731 assert(targetIndex
>= 0 && targetIndex
< NUM_TEXTURE_TARGETS
);
1733 if (tex_obj
->Target
== 0)
1734 finish_texture_init(ctx
, target
, tex_obj
, targetIndex
);
1736 assert(tex_obj
->Target
== target
);
1737 assert(tex_obj
->TargetIndex
== targetIndex
);
1739 bind_texture_object(ctx
, ctx
->Texture
.CurrentUnit
, tex_obj
);
1742 struct gl_texture_object
*
1743 _mesa_lookup_or_create_texture(struct gl_context
*ctx
, GLenum target
,
1744 GLuint texName
, bool no_error
, bool is_ext_dsa
,
1747 struct gl_texture_object
*newTexObj
= NULL
;
1751 if (_mesa_is_proxy_texture(target
)) {
1752 /* EXT_dsa allows proxy targets only when texName is 0 */
1754 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target = %s)", caller
,
1755 _mesa_enum_to_string(target
));
1758 return _mesa_get_current_tex_object(ctx
, target
);
1760 if (GL_TEXTURE_CUBE_MAP_POSITIVE_X
<= target
&&
1761 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
1762 target
= GL_TEXTURE_CUBE_MAP
;
1766 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1767 if (!no_error
&& targetIndex
< 0) {
1768 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target = %s)", caller
,
1769 _mesa_enum_to_string(target
));
1772 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1775 * Get pointer to new texture object (newTexObj)
1778 /* Use a default texture object */
1779 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1781 /* non-default texture object */
1782 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1784 /* error checking */
1786 newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1787 /* The named texture object's target doesn't match the
1790 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1791 "%s(target mismatch)", caller
);
1794 if (newTexObj
->Target
== 0) {
1795 finish_texture_init(ctx
, target
, newTexObj
, targetIndex
);
1798 if (!no_error
&& ctx
->API
== API_OPENGL_CORE
) {
1799 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1800 "%s(non-gen name)", caller
);
1804 /* if this is a new texture id, allocate a texture object now */
1805 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1807 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1811 /* and insert it into hash table */
1812 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1816 assert(newTexObj
->Target
== target
);
1817 assert(newTexObj
->TargetIndex
== targetIndex
);
1823 * Implement glBindTexture(). Do error checking, look-up or create a new
1824 * texture object, then bind it in the current texture unit.
1826 * \param target texture target.
1827 * \param texName texture name.
1828 * \param texunit texture unit.
1830 static ALWAYS_INLINE
void
1831 bind_texture(struct gl_context
*ctx
, GLenum target
, GLuint texName
,
1832 GLenum texunit
, bool no_error
, const char *caller
)
1834 struct gl_texture_object
*newTexObj
=
1835 _mesa_lookup_or_create_texture(ctx
, target
, texName
, no_error
, false,
1840 bind_texture_object(ctx
, texunit
, newTexObj
);
1844 _mesa_BindTexture_no_error(GLenum target
, GLuint texName
)
1846 GET_CURRENT_CONTEXT(ctx
);
1847 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, true,
1853 _mesa_BindTexture(GLenum target
, GLuint texName
)
1855 GET_CURRENT_CONTEXT(ctx
);
1857 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1858 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1859 _mesa_enum_to_string(target
), (GLint
) texName
);
1861 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, false,
1867 _mesa_BindMultiTextureEXT(GLenum texunit
, GLenum target
, GLuint texture
)
1869 GET_CURRENT_CONTEXT(ctx
);
1871 unsigned unit
= texunit
- GL_TEXTURE0
;
1873 if (texunit
< GL_TEXTURE0
|| unit
>= _mesa_max_tex_unit(ctx
)) {
1874 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindMultiTextureEXT(texunit=%s)",
1875 _mesa_enum_to_string(texunit
));
1879 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1880 _mesa_debug(ctx
, "glBindMultiTextureEXT %s %d\n",
1881 _mesa_enum_to_string(texunit
), (GLint
) texture
);
1883 bind_texture(ctx
, target
, texture
, unit
, false, "glBindMultiTextureEXT");
1888 * OpenGL 4.5 / GL_ARB_direct_state_access glBindTextureUnit().
1890 * \param unit texture unit.
1891 * \param texture texture name.
1893 * \sa glBindTexture().
1895 * If the named texture is 0, this will reset each target for the specified
1896 * texture unit to its default texture.
1897 * If the named texture is not 0 or a recognized texture name, this throws
1898 * GL_INVALID_OPERATION.
1900 static ALWAYS_INLINE
void
1901 bind_texture_unit(struct gl_context
*ctx
, GLuint unit
, GLuint texture
,
1904 struct gl_texture_object
*texObj
;
1906 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1908 * "When texture is zero, each of the targets enumerated at the
1909 * beginning of this section is reset to its default texture for the
1910 * corresponding texture image unit."
1913 unbind_textures_from_unit(ctx
, unit
);
1917 /* Get the non-default texture object */
1918 texObj
= _mesa_lookup_texture(ctx
, texture
);
1920 /* Error checking */
1922 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1923 "glBindTextureUnit(non-gen name)");
1927 if (texObj
->Target
== 0) {
1928 /* Texture object was gen'd but never bound so the target is not set */
1929 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindTextureUnit(target)");
1934 assert(valid_texture_object(texObj
));
1936 bind_texture_object(ctx
, unit
, texObj
);
1941 _mesa_BindTextureUnit_no_error(GLuint unit
, GLuint texture
)
1943 GET_CURRENT_CONTEXT(ctx
);
1944 bind_texture_unit(ctx
, unit
, texture
, true);
1949 _mesa_BindTextureUnit(GLuint unit
, GLuint texture
)
1951 GET_CURRENT_CONTEXT(ctx
);
1953 if (unit
>= _mesa_max_tex_unit(ctx
)) {
1954 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindTextureUnit(unit=%u)", unit
);
1958 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1959 _mesa_debug(ctx
, "glBindTextureUnit %s %d\n",
1960 _mesa_enum_to_string(GL_TEXTURE0
+unit
), (GLint
) texture
);
1962 bind_texture_unit(ctx
, unit
, texture
, false);
1967 * OpenGL 4.4 / GL_ARB_multi_bind glBindTextures().
1969 static ALWAYS_INLINE
void
1970 bind_textures(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
1971 const GLuint
*textures
, bool no_error
)
1976 /* Note that the error semantics for multi-bind commands differ from
1977 * those of other GL commands.
1979 * The issues section in the ARB_multi_bind spec says:
1981 * "(11) Typically, OpenGL specifies that if an error is generated by
1982 * a command, that command has no effect. This is somewhat
1983 * unfortunate for multi-bind commands, because it would require
1984 * a first pass to scan the entire list of bound objects for
1985 * errors and then a second pass to actually perform the
1986 * bindings. Should we have different error semantics?
1988 * RESOLVED: Yes. In this specification, when the parameters for
1989 * one of the <count> binding points are invalid, that binding
1990 * point is not updated and an error will be generated. However,
1991 * other binding points in the same command will be updated if
1992 * their parameters are valid and no other error occurs."
1995 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
1997 for (i
= 0; i
< count
; i
++) {
1998 if (textures
[i
] != 0) {
1999 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
2000 struct gl_texture_object
*current
= texUnit
->_Current
;
2001 struct gl_texture_object
*texObj
;
2003 if (current
&& current
->Name
== textures
[i
])
2006 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
2008 if (texObj
&& texObj
->Target
!= 0) {
2009 bind_texture_object(ctx
, first
+ i
, texObj
);
2010 } else if (!no_error
) {
2011 /* The ARB_multi_bind spec says:
2013 * "An INVALID_OPERATION error is generated if any value
2014 * in <textures> is not zero or the name of an existing
2015 * texture object (per binding)."
2017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2018 "glBindTextures(textures[%d]=%u is not zero "
2019 "or the name of an existing texture object)",
2023 unbind_textures_from_unit(ctx
, first
+ i
);
2027 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
2029 /* Unbind all textures in the range <first> through <first>+<count>-1 */
2030 for (i
= 0; i
< count
; i
++)
2031 unbind_textures_from_unit(ctx
, first
+ i
);
2037 _mesa_BindTextures_no_error(GLuint first
, GLsizei count
, const GLuint
*textures
)
2039 GET_CURRENT_CONTEXT(ctx
);
2040 bind_textures(ctx
, first
, count
, textures
, true);
2045 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
2047 GET_CURRENT_CONTEXT(ctx
);
2049 /* The ARB_multi_bind spec says:
2051 * "An INVALID_OPERATION error is generated if <first> + <count>
2052 * is greater than the number of texture image units supported
2053 * by the implementation."
2055 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
2056 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2057 "glBindTextures(first=%u + count=%d > the value of "
2058 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
2059 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
2063 bind_textures(ctx
, first
, count
, textures
, false);
2068 * Set texture priorities.
2070 * \param n number of textures.
2071 * \param texName texture names.
2072 * \param priorities corresponding texture priorities.
2074 * \sa glPrioritizeTextures().
2076 * Looks up each texture in the hash, clamps the corresponding priority between
2077 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
2080 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
2081 const GLclampf
*priorities
)
2083 GET_CURRENT_CONTEXT(ctx
);
2086 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2087 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
2089 FLUSH_VERTICES(ctx
, 0);
2092 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
2099 for (i
= 0; i
< n
; i
++) {
2100 if (texName
[i
] > 0) {
2101 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2103 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
2108 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2114 * See if textures are loaded in texture memory.
2116 * \param n number of textures to query.
2117 * \param texName array with the texture names.
2118 * \param residences array which will hold the residence status.
2120 * \return GL_TRUE if all textures are resident and
2121 * residences is left unchanged,
2123 * Note: we assume all textures are always resident
2125 GLboolean GLAPIENTRY
2126 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
2127 GLboolean
*residences
)
2129 GET_CURRENT_CONTEXT(ctx
);
2130 GLboolean allResident
= GL_TRUE
;
2132 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2134 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2135 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
2138 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
2142 if (!texName
|| !residences
)
2145 /* We only do error checking on the texture names */
2146 for (i
= 0; i
< n
; i
++) {
2147 struct gl_texture_object
*t
;
2148 if (texName
[i
] == 0) {
2149 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2152 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2154 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2164 * See if a name corresponds to a texture.
2166 * \param texture texture name.
2168 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
2171 * \sa glIsTexture().
2173 * Calls _mesa_HashLookup().
2175 GLboolean GLAPIENTRY
2176 _mesa_IsTexture( GLuint texture
)
2178 struct gl_texture_object
*t
;
2179 GET_CURRENT_CONTEXT(ctx
);
2180 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2182 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2183 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
2188 t
= _mesa_lookup_texture(ctx
, texture
);
2190 /* IsTexture is true only after object has been bound once. */
2191 return t
&& t
->Target
;
2196 * Simplest implementation of texture locking: grab the shared tex
2197 * mutex. Examine the shared context state timestamp and if there has
2198 * been a change, set the appropriate bits in ctx->NewState.
2200 * This is used to deal with synchronizing things when a texture object
2201 * is used/modified by different contexts (or threads) which are sharing
2204 * See also _mesa_lock/unlock_texture() in teximage.h
2207 _mesa_lock_context_textures( struct gl_context
*ctx
)
2209 mtx_lock(&ctx
->Shared
->TexMutex
);
2211 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
2212 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2213 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
2219 _mesa_unlock_context_textures( struct gl_context
*ctx
)
2221 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
2222 mtx_unlock(&ctx
->Shared
->TexMutex
);
2227 _mesa_InvalidateTexSubImage_no_error(GLuint texture
, GLint level
, GLint xoffset
,
2228 GLint yoffset
, GLint zoffset
,
2229 GLsizei width
, GLsizei height
,
2237 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
2238 GLint yoffset
, GLint zoffset
, GLsizei width
,
2239 GLsizei height
, GLsizei depth
)
2241 struct gl_texture_object
*t
;
2242 struct gl_texture_image
*image
;
2243 GET_CURRENT_CONTEXT(ctx
);
2245 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2246 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
2248 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
2249 "glInvalidateTexSubImage");
2251 /* The GL_ARB_invalidate_subdata spec says:
2253 * "...the specified subregion must be between -<b> and <dim>+<b> where
2254 * <dim> is the size of the dimension of the texture image, and <b> is
2255 * the size of the border of that texture image, otherwise
2256 * INVALID_VALUE is generated (border is not applied to dimensions that
2257 * don't exist in a given texture target)."
2259 image
= t
->Image
[0][level
];
2268 /* The GL_ARB_invalidate_subdata spec says:
2270 * "For texture targets that don't have certain dimensions, this
2271 * command treats those dimensions as having a size of 1. For
2272 * example, to invalidate a portion of a two-dimensional texture,
2273 * the application would use <zoffset> equal to zero and <depth>
2276 switch (t
->Target
) {
2277 case GL_TEXTURE_BUFFER
:
2286 xBorder
= image
->Border
;
2289 imageWidth
= image
->Width
;
2293 case GL_TEXTURE_1D_ARRAY
:
2294 xBorder
= image
->Border
;
2297 imageWidth
= image
->Width
;
2298 imageHeight
= image
->Height
;
2302 case GL_TEXTURE_CUBE_MAP
:
2303 case GL_TEXTURE_RECTANGLE
:
2304 case GL_TEXTURE_2D_MULTISAMPLE
:
2305 xBorder
= image
->Border
;
2306 yBorder
= image
->Border
;
2308 imageWidth
= image
->Width
;
2309 imageHeight
= image
->Height
;
2312 case GL_TEXTURE_2D_ARRAY
:
2313 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2314 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2315 xBorder
= image
->Border
;
2316 yBorder
= image
->Border
;
2318 imageWidth
= image
->Width
;
2319 imageHeight
= image
->Height
;
2320 imageDepth
= image
->Depth
;
2323 xBorder
= image
->Border
;
2324 yBorder
= image
->Border
;
2325 zBorder
= image
->Border
;
2326 imageWidth
= image
->Width
;
2327 imageHeight
= image
->Height
;
2328 imageDepth
= image
->Depth
;
2331 assert(!"Should not get here.");
2341 if (xoffset
< -xBorder
) {
2342 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
2346 if (xoffset
+ width
> imageWidth
+ xBorder
) {
2347 _mesa_error(ctx
, GL_INVALID_VALUE
,
2348 "glInvalidateSubTexImage(xoffset+width)");
2352 if (yoffset
< -yBorder
) {
2353 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
2357 if (yoffset
+ height
> imageHeight
+ yBorder
) {
2358 _mesa_error(ctx
, GL_INVALID_VALUE
,
2359 "glInvalidateSubTexImage(yoffset+height)");
2363 if (zoffset
< -zBorder
) {
2364 _mesa_error(ctx
, GL_INVALID_VALUE
,
2365 "glInvalidateSubTexImage(zoffset)");
2369 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
2370 _mesa_error(ctx
, GL_INVALID_VALUE
,
2371 "glInvalidateSubTexImage(zoffset+depth)");
2376 /* We don't actually do anything for this yet. Just return after
2377 * validating the parameters and generating the required errors.
2384 _mesa_InvalidateTexImage_no_error(GLuint texture
, GLint level
)
2391 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
2393 GET_CURRENT_CONTEXT(ctx
);
2395 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2396 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
2398 invalidate_tex_image_error_check(ctx
, texture
, level
,
2399 "glInvalidateTexImage");
2401 /* We don't actually do anything for this yet. Just return after
2402 * validating the parameters and generating the required errors.