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"
47 #include "util/u_memory.h"
51 /**********************************************************************/
52 /** \name Internal functions */
56 * This function checks for all valid combinations of Min and Mag filters for
57 * Float types, when extensions like OES_texture_float and
58 * OES_texture_float_linear are supported. OES_texture_float mentions support
59 * for NEAREST, NEAREST_MIPMAP_NEAREST magnification and minification filters.
60 * Mag filters like LINEAR and min filters like NEAREST_MIPMAP_LINEAR,
61 * LINEAR_MIPMAP_NEAREST and LINEAR_MIPMAP_LINEAR are only valid in case
62 * OES_texture_float_linear is supported.
64 * Returns true in case the filter is valid for given Float type else false.
67 valid_filter_for_float(const struct gl_context
*ctx
,
68 const struct gl_texture_object
*obj
)
70 switch (obj
->Sampler
.MagFilter
) {
72 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
74 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
78 case GL_NEAREST_MIPMAP_NEAREST
:
81 unreachable("Invalid mag filter");
84 switch (obj
->Sampler
.MinFilter
) {
86 case GL_NEAREST_MIPMAP_LINEAR
:
87 case GL_LINEAR_MIPMAP_NEAREST
:
88 case GL_LINEAR_MIPMAP_LINEAR
:
89 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
91 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
95 case GL_NEAREST_MIPMAP_NEAREST
:
98 unreachable("Invalid min filter");
105 * Return the gl_texture_object for a given ID.
107 struct gl_texture_object
*
108 _mesa_lookup_texture(struct gl_context
*ctx
, GLuint id
)
110 return (struct gl_texture_object
*)
111 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
115 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
116 * is not in the hash table. After calling _mesa_error, it returns NULL.
118 struct gl_texture_object
*
119 _mesa_lookup_texture_err(struct gl_context
*ctx
, GLuint id
, const char* func
)
121 struct gl_texture_object
*texObj
= NULL
;
124 texObj
= _mesa_lookup_texture(ctx
, id
); /* Returns NULL if not found. */
127 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(texture)", func
);
133 struct gl_texture_object
*
134 _mesa_lookup_texture_locked(struct gl_context
*ctx
, GLuint id
)
136 return (struct gl_texture_object
*)
137 _mesa_HashLookupLocked(ctx
->Shared
->TexObjects
, id
);
141 * Return a pointer to the current texture object for the given target
142 * on the current texture unit.
143 * Note: all <target> error checking should have been done by this point.
145 struct gl_texture_object
*
146 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
148 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
149 const GLboolean arrayTex
= ctx
->Extensions
.EXT_texture_array
;
153 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
154 case GL_PROXY_TEXTURE_1D
:
155 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
157 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
158 case GL_PROXY_TEXTURE_2D
:
159 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
161 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
162 case GL_PROXY_TEXTURE_3D
:
163 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
164 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
166 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
167 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
168 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
169 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
170 case GL_TEXTURE_CUBE_MAP
:
171 return ctx
->Extensions
.ARB_texture_cube_map
172 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
173 case GL_PROXY_TEXTURE_CUBE_MAP
:
174 return ctx
->Extensions
.ARB_texture_cube_map
175 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
176 case GL_TEXTURE_CUBE_MAP_ARRAY
:
177 return _mesa_has_texture_cube_map_array(ctx
)
178 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
179 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
180 return _mesa_has_texture_cube_map_array(ctx
)
181 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
182 case GL_TEXTURE_RECTANGLE_NV
:
183 return ctx
->Extensions
.NV_texture_rectangle
184 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
185 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
186 return ctx
->Extensions
.NV_texture_rectangle
187 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
188 case GL_TEXTURE_1D_ARRAY_EXT
:
189 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
190 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
191 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
192 case GL_TEXTURE_2D_ARRAY_EXT
:
193 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
194 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
195 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
196 case GL_TEXTURE_BUFFER
:
197 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
198 _mesa_has_OES_texture_buffer(ctx
)) ?
199 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
200 case GL_TEXTURE_EXTERNAL_OES
:
201 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
202 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
203 case GL_TEXTURE_2D_MULTISAMPLE
:
204 return ctx
->Extensions
.ARB_texture_multisample
205 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
206 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
207 return ctx
->Extensions
.ARB_texture_multisample
208 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
209 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
210 return ctx
->Extensions
.ARB_texture_multisample
211 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
212 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
213 return ctx
->Extensions
.ARB_texture_multisample
214 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
216 _mesa_problem(NULL
, "bad target in _mesa_get_current_tex_object()");
223 * Get the texture object for given target and texunit
224 * Proxy targets are accepted only allowProxyTarget is true.
225 * Return NULL if any error (and record the error).
227 struct gl_texture_object
*
228 _mesa_get_texobj_by_target_and_texunit(struct gl_context
*ctx
, GLenum target
,
229 GLuint texunit
, bool allowProxyTarget
,
232 struct gl_texture_unit
*texUnit
;
235 if (_mesa_is_proxy_texture(target
) && allowProxyTarget
) {
236 return _mesa_get_current_tex_object(ctx
, target
);
239 if (texunit
>= ctx
->Const
.MaxCombinedTextureImageUnits
) {
240 _mesa_error(ctx
, GL_INVALID_OPERATION
,
241 "%s(texunit=%d)", caller
, texunit
);
245 texUnit
= _mesa_get_tex_unit(ctx
, texunit
);
247 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
248 if (targetIndex
< 0 || targetIndex
== TEXTURE_BUFFER_INDEX
) {
249 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
252 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
254 return texUnit
->CurrentTex
[targetIndex
];
259 * Allocate and initialize a new texture object. But don't put it into the
260 * texture object hash table.
262 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
265 * \param shared the shared GL state structure to contain the texture object
266 * \param name integer name for the texture object
267 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
268 * GL_TEXTURE_CUBE_MAP or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
271 * \return pointer to new texture object.
273 struct gl_texture_object
*
274 _mesa_new_texture_object(struct gl_context
*ctx
, GLuint name
, GLenum target
)
276 struct gl_texture_object
*obj
;
278 obj
= MALLOC_STRUCT(gl_texture_object
);
282 _mesa_initialize_texture_object(ctx
, obj
, name
, target
);
288 * Initialize a new texture object to default values.
289 * \param obj the texture object
290 * \param name the texture name
291 * \param target the texture target
294 _mesa_initialize_texture_object( struct gl_context
*ctx
,
295 struct gl_texture_object
*obj
,
296 GLuint name
, GLenum target
)
298 assert(target
== 0 ||
299 target
== GL_TEXTURE_1D
||
300 target
== GL_TEXTURE_2D
||
301 target
== GL_TEXTURE_3D
||
302 target
== GL_TEXTURE_CUBE_MAP
||
303 target
== GL_TEXTURE_RECTANGLE_NV
||
304 target
== GL_TEXTURE_1D_ARRAY_EXT
||
305 target
== GL_TEXTURE_2D_ARRAY_EXT
||
306 target
== GL_TEXTURE_EXTERNAL_OES
||
307 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
308 target
== GL_TEXTURE_BUFFER
||
309 target
== GL_TEXTURE_2D_MULTISAMPLE
||
310 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
312 memset(obj
, 0, sizeof(*obj
));
313 /* init the non-zero fields */
314 simple_mtx_init(&obj
->Mutex
, mtx_plain
);
317 obj
->Target
= target
;
319 obj
->TargetIndex
= _mesa_tex_target_to_index(ctx
, target
);
322 obj
->TargetIndex
= NUM_TEXTURE_TARGETS
; /* invalid/error value */
324 obj
->Priority
= 1.0F
;
326 obj
->MaxLevel
= 1000;
328 /* must be one; no support for (YUV) planes in separate buffers */
329 obj
->RequiredTextureImageUnits
= 1;
332 if (target
== GL_TEXTURE_RECTANGLE_NV
||
333 target
== GL_TEXTURE_EXTERNAL_OES
) {
334 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
335 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
336 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
337 obj
->Sampler
.MinFilter
= GL_LINEAR
;
340 obj
->Sampler
.WrapS
= GL_REPEAT
;
341 obj
->Sampler
.WrapT
= GL_REPEAT
;
342 obj
->Sampler
.WrapR
= GL_REPEAT
;
343 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
345 obj
->Sampler
.MagFilter
= GL_LINEAR
;
346 obj
->Sampler
.MinLod
= -1000.0;
347 obj
->Sampler
.MaxLod
= 1000.0;
348 obj
->Sampler
.LodBias
= 0.0;
349 obj
->Sampler
.MaxAnisotropy
= 1.0;
350 obj
->Sampler
.CompareMode
= GL_NONE
; /* ARB_shadow */
351 obj
->Sampler
.CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
352 obj
->DepthMode
= ctx
->API
== API_OPENGL_CORE
? GL_RED
: GL_LUMINANCE
;
353 obj
->StencilSampling
= false;
354 obj
->Sampler
.CubeMapSeamless
= GL_FALSE
;
355 obj
->Sampler
.HandleAllocated
= GL_FALSE
;
356 obj
->Swizzle
[0] = GL_RED
;
357 obj
->Swizzle
[1] = GL_GREEN
;
358 obj
->Swizzle
[2] = GL_BLUE
;
359 obj
->Swizzle
[3] = GL_ALPHA
;
360 obj
->_Swizzle
= SWIZZLE_NOOP
;
361 obj
->Sampler
.sRGBDecode
= GL_DECODE_EXT
;
362 obj
->BufferObjectFormat
= GL_R8
;
363 obj
->_BufferObjectFormat
= MESA_FORMAT_R_UNORM8
;
364 obj
->ImageFormatCompatibilityType
= GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE
;
366 /* GL_ARB_bindless_texture */
367 _mesa_init_texture_handles(obj
);
372 * Some texture initialization can't be finished until we know which
373 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
376 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
377 struct gl_texture_object
*obj
, int targetIndex
)
379 GLenum filter
= GL_LINEAR
;
380 assert(obj
->Target
== 0);
382 obj
->Target
= target
;
383 obj
->TargetIndex
= targetIndex
;
384 assert(obj
->TargetIndex
< NUM_TEXTURE_TARGETS
);
387 case GL_TEXTURE_2D_MULTISAMPLE
:
388 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
392 case GL_TEXTURE_RECTANGLE_NV
:
393 case GL_TEXTURE_EXTERNAL_OES
:
394 /* have to init wrap and filter state here - kind of klunky */
395 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
396 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
397 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
398 obj
->Sampler
.MinFilter
= filter
;
399 obj
->Sampler
.MagFilter
= filter
;
400 if (ctx
->Driver
.TexParameter
) {
401 /* XXX we probably don't need to make all these calls */
402 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_S
);
403 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_T
);
404 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_R
);
405 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_MIN_FILTER
);
406 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_MAG_FILTER
);
411 /* nothing needs done */
418 * Deallocate a texture object struct. It should have already been
419 * removed from the texture object pool.
420 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
422 * \param shared the shared GL state to which the object belongs.
423 * \param texObj the texture object to delete.
426 _mesa_delete_texture_object(struct gl_context
*ctx
,
427 struct gl_texture_object
*texObj
)
431 /* Set Target to an invalid value. With some assertions elsewhere
432 * we can try to detect possible use of deleted textures.
434 texObj
->Target
= 0x99;
436 /* free the texture images */
437 for (face
= 0; face
< 6; face
++) {
438 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
439 if (texObj
->Image
[face
][i
]) {
440 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
445 /* Delete all texture/image handles. */
446 _mesa_delete_texture_handles(ctx
, texObj
);
448 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
450 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
451 simple_mtx_destroy(&texObj
->Mutex
);
455 /* free this object */
461 * Copy texture object state from one texture object to another.
462 * Use for glPush/PopAttrib.
464 * \param dest destination texture object.
465 * \param src source texture object.
468 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
469 const struct gl_texture_object
*src
)
471 dest
->Target
= src
->Target
;
472 dest
->TargetIndex
= src
->TargetIndex
;
473 dest
->Name
= src
->Name
;
474 dest
->Priority
= src
->Priority
;
475 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
476 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
477 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
478 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
479 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
480 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
481 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
482 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
483 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
484 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
485 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
486 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
487 dest
->BaseLevel
= src
->BaseLevel
;
488 dest
->MaxLevel
= src
->MaxLevel
;
489 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
490 dest
->Sampler
.CompareMode
= src
->Sampler
.CompareMode
;
491 dest
->Sampler
.CompareFunc
= src
->Sampler
.CompareFunc
;
492 dest
->Sampler
.CubeMapSeamless
= src
->Sampler
.CubeMapSeamless
;
493 dest
->DepthMode
= src
->DepthMode
;
494 dest
->StencilSampling
= src
->StencilSampling
;
495 dest
->Sampler
.sRGBDecode
= src
->Sampler
.sRGBDecode
;
496 dest
->_MaxLevel
= src
->_MaxLevel
;
497 dest
->_MaxLambda
= src
->_MaxLambda
;
498 dest
->GenerateMipmap
= src
->GenerateMipmap
;
499 dest
->_BaseComplete
= src
->_BaseComplete
;
500 dest
->_MipmapComplete
= src
->_MipmapComplete
;
501 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
502 dest
->_Swizzle
= src
->_Swizzle
;
503 dest
->_IsHalfFloat
= src
->_IsHalfFloat
;
504 dest
->_IsFloat
= src
->_IsFloat
;
506 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
511 * Free all texture images of the given texture objectm, except for
514 * \param ctx GL context.
515 * \param texObj texture object.
516 * \param retainTexImage a texture image that will \em not be freed.
518 * \sa _mesa_clear_texture_image().
521 _mesa_clear_texture_object(struct gl_context
*ctx
,
522 struct gl_texture_object
*texObj
,
523 struct gl_texture_image
*retainTexImage
)
527 if (texObj
->Target
== 0)
530 for (i
= 0; i
< MAX_FACES
; i
++) {
531 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
532 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
533 if (texImage
&& texImage
!= retainTexImage
)
534 _mesa_clear_texture_image(ctx
, texImage
);
541 * Check if the given texture object is valid by examining its Target field.
542 * For debugging only.
545 valid_texture_object(const struct gl_texture_object
*tex
)
547 switch (tex
->Target
) {
552 case GL_TEXTURE_CUBE_MAP
:
553 case GL_TEXTURE_RECTANGLE_NV
:
554 case GL_TEXTURE_1D_ARRAY_EXT
:
555 case GL_TEXTURE_2D_ARRAY_EXT
:
556 case GL_TEXTURE_BUFFER
:
557 case GL_TEXTURE_EXTERNAL_OES
:
558 case GL_TEXTURE_CUBE_MAP_ARRAY
:
559 case GL_TEXTURE_2D_MULTISAMPLE
:
560 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
563 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
566 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
567 tex
->Target
, tex
->Name
);
574 * Reference (or unreference) a texture object.
575 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
576 * If 'tex' is non-null, increment its refcount.
577 * This is normally only called from the _mesa_reference_texobj() macro
578 * when there's a real pointer change.
581 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
582 struct gl_texture_object
*tex
)
587 /* Unreference the old texture */
588 GLboolean deleteFlag
= GL_FALSE
;
589 struct gl_texture_object
*oldTex
= *ptr
;
591 assert(valid_texture_object(oldTex
));
592 (void) valid_texture_object
; /* silence warning in release builds */
594 simple_mtx_lock(&oldTex
->Mutex
);
595 assert(oldTex
->RefCount
> 0);
598 deleteFlag
= (oldTex
->RefCount
== 0);
599 simple_mtx_unlock(&oldTex
->Mutex
);
602 /* Passing in the context drastically changes the driver code for
603 * framebuffer deletion.
605 GET_CURRENT_CONTEXT(ctx
);
607 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
609 _mesa_problem(NULL
, "Unable to delete texture, no context");
617 /* reference new texture */
618 assert(valid_texture_object(tex
));
619 simple_mtx_lock(&tex
->Mutex
);
620 assert(tex
->RefCount
> 0);
624 simple_mtx_unlock(&tex
->Mutex
);
629 enum base_mipmap
{ BASE
, MIPMAP
};
633 * Mark a texture object as incomplete. There are actually three kinds of
635 * 1. "base incomplete": the base level of the texture is invalid so no
636 * texturing is possible.
637 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
638 * mipmap filtering isn't possible, but non-mipmap filtering is.
639 * 3. "texture incompleteness": some combination of texture state and
640 * sampler state renders the texture incomplete.
642 * \param t texture object
643 * \param bm either BASE or MIPMAP to indicate what's incomplete
644 * \param fmt... string describing why it's incomplete (for debugging).
647 incomplete(struct gl_texture_object
*t
, enum base_mipmap bm
,
648 const char *fmt
, ...)
650 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_TEXTURE
) {
655 vsnprintf(s
, sizeof(s
), fmt
, args
);
658 _mesa_debug(NULL
, "Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
662 t
->_BaseComplete
= GL_FALSE
;
663 t
->_MipmapComplete
= GL_FALSE
;
668 * Examine a texture object to determine if it is complete.
670 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
673 * \param ctx GL context.
674 * \param t texture object.
676 * According to the texture target, verifies that each of the mipmaps is
677 * present and has the expected size.
680 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
681 struct gl_texture_object
*t
)
683 const GLint baseLevel
= t
->BaseLevel
;
684 const struct gl_texture_image
*baseImage
;
687 /* We'll set these to FALSE if tests fail below */
688 t
->_BaseComplete
= GL_TRUE
;
689 t
->_MipmapComplete
= GL_TRUE
;
691 if (t
->Target
== GL_TEXTURE_BUFFER
) {
692 /* Buffer textures are always considered complete. The obvious case where
693 * they would be incomplete (no BO attached) is actually specced to be
694 * undefined rendering results.
699 /* Detect cases where the application set the base level to an invalid
702 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
703 incomplete(t
, BASE
, "base level = %d is invalid", baseLevel
);
707 if (t
->MaxLevel
< baseLevel
) {
708 incomplete(t
, MIPMAP
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
709 t
->MaxLevel
, baseLevel
);
713 baseImage
= t
->Image
[0][baseLevel
];
715 /* Always need the base level image */
717 incomplete(t
, BASE
, "Image[baseLevel=%d] == NULL", baseLevel
);
721 /* Check width/height/depth for zero */
722 if (baseImage
->Width
== 0 ||
723 baseImage
->Height
== 0 ||
724 baseImage
->Depth
== 0) {
725 incomplete(t
, BASE
, "texture width or height or depth = 0");
729 /* Check if the texture values are integer */
731 GLenum datatype
= _mesa_get_format_datatype(baseImage
->TexFormat
);
732 t
->_IsIntegerFormat
= datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
;
735 /* Check if the texture type is Float or HalfFloatOES and ensure Min and Mag
736 * filters are supported in this case.
738 if (_mesa_is_gles(ctx
) && !valid_filter_for_float(ctx
, t
)) {
739 incomplete(t
, BASE
, "Filter is not supported with Float types.");
743 maxLevels
= _mesa_max_texture_levels(ctx
, t
->Target
);
744 if (maxLevels
== 0) {
745 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
749 assert(maxLevels
> 0);
751 t
->_MaxLevel
= MIN3(t
->MaxLevel
,
752 /* 'p' in the GL spec */
753 (int) (baseLevel
+ baseImage
->MaxNumLevels
- 1),
754 /* 'q' in the GL spec */
758 /* Adjust max level for views: the data store may have more levels than
761 t
->_MaxLevel
= MAX2(MIN2(t
->_MaxLevel
, t
->NumLevels
- 1), 0);
764 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
765 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- baseLevel
);
768 /* This texture object was created with glTexStorage1/2/3D() so we
769 * know that all the mipmap levels are the right size and all cube
770 * map faces are the same size.
771 * We don't need to do any of the additional checks below.
776 if (t
->Target
== GL_TEXTURE_CUBE_MAP
) {
777 /* Make sure that all six cube map level 0 images are the same size and
779 * Note: we know that the image's width==height (we enforce that
780 * at glTexImage time) so we only need to test the width here.
783 assert(baseImage
->Width2
== baseImage
->Height
);
784 for (face
= 1; face
< 6; face
++) {
785 assert(t
->Image
[face
][baseLevel
] == NULL
||
786 t
->Image
[face
][baseLevel
]->Width2
==
787 t
->Image
[face
][baseLevel
]->Height2
);
788 if (t
->Image
[face
][baseLevel
] == NULL
||
789 t
->Image
[face
][baseLevel
]->Width2
!= baseImage
->Width2
) {
790 incomplete(t
, BASE
, "Cube face missing or mismatched size");
793 if (t
->Image
[face
][baseLevel
]->InternalFormat
!=
794 baseImage
->InternalFormat
) {
795 incomplete(t
, BASE
, "Cube face format mismatch");
798 if (t
->Image
[face
][baseLevel
]->Border
!= baseImage
->Border
) {
799 incomplete(t
, BASE
, "Cube face border size mismatch");
806 * Do mipmap consistency checking.
807 * Note: we don't care about the current texture sampler state here.
808 * To determine texture completeness we'll either look at _BaseComplete
809 * or _MipmapComplete depending on the current minification filter mode.
813 const GLint minLevel
= baseLevel
;
814 const GLint maxLevel
= t
->_MaxLevel
;
815 const GLuint numFaces
= _mesa_num_tex_faces(t
->Target
);
816 GLuint width
, height
, depth
, face
;
818 if (minLevel
> maxLevel
) {
819 incomplete(t
, MIPMAP
, "minLevel > maxLevel");
823 /* Get the base image's dimensions */
824 width
= baseImage
->Width2
;
825 height
= baseImage
->Height2
;
826 depth
= baseImage
->Depth2
;
828 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
829 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
831 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
832 /* Compute the expected size of image at level[i] */
836 if (height
> 1 && t
->Target
!= GL_TEXTURE_1D_ARRAY
) {
839 if (depth
> 1 && t
->Target
!= GL_TEXTURE_2D_ARRAY
840 && t
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
844 /* loop over cube faces (or single face otherwise) */
845 for (face
= 0; face
< numFaces
; face
++) {
846 if (i
>= minLevel
&& i
<= maxLevel
) {
847 const struct gl_texture_image
*img
= t
->Image
[face
][i
];
850 incomplete(t
, MIPMAP
, "TexImage[%d] is missing", i
);
853 if (img
->InternalFormat
!= baseImage
->InternalFormat
) {
854 incomplete(t
, MIPMAP
, "Format[i] != Format[baseLevel]");
857 if (img
->Border
!= baseImage
->Border
) {
858 incomplete(t
, MIPMAP
, "Border[i] != Border[baseLevel]");
861 if (img
->Width2
!= width
) {
862 incomplete(t
, MIPMAP
, "TexImage[%d] bad width %u", i
,
866 if (img
->Height2
!= height
) {
867 incomplete(t
, MIPMAP
, "TexImage[%d] bad height %u", i
,
871 if (img
->Depth2
!= depth
) {
872 incomplete(t
, MIPMAP
, "TexImage[%d] bad depth %u", i
,
879 if (width
== 1 && height
== 1 && depth
== 1) {
880 return; /* found smallest needed mipmap, all done! */
888 _mesa_cube_level_complete(const struct gl_texture_object
*texObj
,
891 const struct gl_texture_image
*img0
, *img
;
894 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
897 if ((level
< 0) || (level
>= MAX_TEXTURE_LEVELS
))
900 /* check first face */
901 img0
= texObj
->Image
[0][level
];
904 img0
->Width
!= img0
->Height
)
907 /* check remaining faces vs. first face */
908 for (face
= 1; face
< 6; face
++) {
909 img
= texObj
->Image
[face
][level
];
911 img
->Width
!= img0
->Width
||
912 img
->Height
!= img0
->Height
||
913 img
->TexFormat
!= img0
->TexFormat
)
921 * Check if the given cube map texture is "cube complete" as defined in
922 * the OpenGL specification.
925 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
927 return _mesa_cube_level_complete(texObj
, texObj
->BaseLevel
);
931 * Mark a texture object dirty. It forces the object to be incomplete
932 * and forces the context to re-validate its state.
934 * \param ctx GL context.
935 * \param texObj texture object.
938 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
940 texObj
->_BaseComplete
= GL_FALSE
;
941 texObj
->_MipmapComplete
= GL_FALSE
;
942 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
947 * Return pointer to a default/fallback texture of the given type/target.
948 * The texture is an RGBA texture with all texels = (0,0,0,1).
949 * That's the value a GLSL sampler should get when sampling from an
950 * incomplete texture.
952 struct gl_texture_object
*
953 _mesa_get_fallback_texture(struct gl_context
*ctx
, gl_texture_index tex
)
955 if (!ctx
->Shared
->FallbackTex
[tex
]) {
956 /* create fallback texture now */
957 const GLsizei width
= 1, height
= 1;
960 struct gl_texture_object
*texObj
;
961 struct gl_texture_image
*texImage
;
962 mesa_format texFormat
;
963 GLuint dims
, face
, numFaces
= 1;
966 for (face
= 0; face
< 6; face
++) {
969 texel
[4*face
+ 2] = 0x0;
970 texel
[4*face
+ 3] = 0xff;
974 case TEXTURE_2D_ARRAY_INDEX
:
976 target
= GL_TEXTURE_2D_ARRAY
;
978 case TEXTURE_1D_ARRAY_INDEX
:
980 target
= GL_TEXTURE_1D_ARRAY
;
982 case TEXTURE_CUBE_INDEX
:
984 target
= GL_TEXTURE_CUBE_MAP
;
987 case TEXTURE_3D_INDEX
:
989 target
= GL_TEXTURE_3D
;
991 case TEXTURE_RECT_INDEX
:
993 target
= GL_TEXTURE_RECTANGLE
;
995 case TEXTURE_2D_INDEX
:
997 target
= GL_TEXTURE_2D
;
999 case TEXTURE_1D_INDEX
:
1001 target
= GL_TEXTURE_1D
;
1003 case TEXTURE_BUFFER_INDEX
:
1005 target
= GL_TEXTURE_BUFFER
;
1007 case TEXTURE_CUBE_ARRAY_INDEX
:
1009 target
= GL_TEXTURE_CUBE_MAP_ARRAY
;
1012 case TEXTURE_EXTERNAL_INDEX
:
1014 target
= GL_TEXTURE_EXTERNAL_OES
;
1016 case TEXTURE_2D_MULTISAMPLE_INDEX
:
1018 target
= GL_TEXTURE_2D_MULTISAMPLE
;
1020 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
:
1022 target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
1029 /* create texture object */
1030 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, target
);
1034 assert(texObj
->RefCount
== 1);
1035 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1036 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1038 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
,
1042 /* need a loop here just for cube maps */
1043 for (face
= 0; face
< numFaces
; face
++) {
1044 const GLenum faceTarget
= _mesa_cube_face_target(target
, face
);
1046 /* initialize level[0] texture image */
1047 texImage
= _mesa_get_tex_image(ctx
, texObj
, faceTarget
, 0);
1049 _mesa_init_teximage_fields(ctx
, texImage
,
1051 (dims
> 1) ? height
: 1,
1052 (dims
> 2) ? depth
: 1,
1054 GL_RGBA
, texFormat
);
1056 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
1057 GL_RGBA
, GL_UNSIGNED_BYTE
, texel
,
1058 &ctx
->DefaultPacking
);
1061 _mesa_test_texobj_completeness(ctx
, texObj
);
1062 assert(texObj
->_BaseComplete
);
1063 assert(texObj
->_MipmapComplete
);
1065 ctx
->Shared
->FallbackTex
[tex
] = texObj
;
1067 /* Complete the driver's operation in case another context will also
1068 * use the same fallback texture. */
1069 if (ctx
->Driver
.Finish
)
1070 ctx
->Driver
.Finish(ctx
);
1072 return ctx
->Shared
->FallbackTex
[tex
];
1077 * Compute the size of the given texture object, in bytes.
1080 texture_size(const struct gl_texture_object
*texObj
)
1082 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1083 GLuint face
, level
, size
= 0;
1085 for (face
= 0; face
< numFaces
; face
++) {
1086 for (level
= 0; level
< MAX_TEXTURE_LEVELS
; level
++) {
1087 const struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
1089 GLuint sz
= _mesa_format_image_size(img
->TexFormat
, img
->Width
,
1090 img
->Height
, img
->Depth
);
1101 * Callback called from _mesa_HashWalk()
1104 count_tex_size(GLuint key
, void *data
, void *userData
)
1106 const struct gl_texture_object
*texObj
=
1107 (const struct gl_texture_object
*) data
;
1108 GLuint
*total
= (GLuint
*) userData
;
1112 *total
= *total
+ texture_size(texObj
);
1117 * Compute total size (in bytes) of all textures for the given context.
1118 * For debugging purposes.
1121 _mesa_total_texture_memory(struct gl_context
*ctx
)
1123 GLuint tgt
, total
= 0;
1125 _mesa_HashWalk(ctx
->Shared
->TexObjects
, count_tex_size
, &total
);
1127 /* plus, the default texture objects */
1128 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1129 total
+= texture_size(ctx
->Shared
->DefaultTex
[tgt
]);
1137 * Return the base format for the given texture object by looking
1138 * at the base texture image.
1139 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
1142 _mesa_texture_base_format(const struct gl_texture_object
*texObj
)
1144 const struct gl_texture_image
*texImage
= _mesa_base_tex_image(texObj
);
1146 return texImage
? texImage
->_BaseFormat
: GL_NONE
;
1150 static struct gl_texture_object
*
1151 invalidate_tex_image_error_check(struct gl_context
*ctx
, GLuint texture
,
1152 GLint level
, const char *name
)
1154 /* The GL_ARB_invalidate_subdata spec says:
1156 * "If <texture> is zero or is not the name of a texture, the error
1157 * INVALID_VALUE is generated."
1159 * This performs the error check in a different order than listed in the
1160 * spec. We have to get the texture object before we can validate the
1161 * other parameters against values in the texture object.
1163 struct gl_texture_object
*const t
= _mesa_lookup_texture(ctx
, texture
);
1164 if (texture
== 0 || t
== NULL
) {
1165 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(texture)", name
);
1169 /* The GL_ARB_invalidate_subdata spec says:
1171 * "If <level> is less than zero or greater than the base 2 logarithm
1172 * of the maximum texture width, height, or depth, the error
1173 * INVALID_VALUE is generated."
1175 if (level
< 0 || level
> t
->MaxLevel
) {
1176 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1180 /* The GL_ARB_invalidate_subdata spec says:
1182 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1183 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1184 * is not zero, the error INVALID_VALUE is generated."
1187 switch (t
->Target
) {
1188 case GL_TEXTURE_RECTANGLE
:
1189 case GL_TEXTURE_BUFFER
:
1190 case GL_TEXTURE_2D_MULTISAMPLE
:
1191 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1192 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1205 * Helper function for glCreateTextures and glGenTextures. Need this because
1206 * glCreateTextures should throw errors if target = 0. This is not exposed to
1207 * the rest of Mesa to encourage Mesa internals to use nameless textures,
1208 * which do not require expensive hash lookups.
1209 * \param target either 0 or a valid / error-checked texture target enum
1212 create_textures(struct gl_context
*ctx
, GLenum target
,
1213 GLsizei n
, GLuint
*textures
, const char *caller
)
1222 * This must be atomic (generation and allocation of texture IDs)
1224 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
1226 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
1228 /* Allocate new, empty texture objects */
1229 for (i
= 0; i
< n
; i
++) {
1230 struct gl_texture_object
*texObj
;
1231 GLuint name
= first
+ i
;
1232 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
1234 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
1235 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1239 /* insert into hash table */
1240 _mesa_HashInsertLocked(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
1245 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
1250 create_textures_err(struct gl_context
*ctx
, GLenum target
,
1251 GLsizei n
, GLuint
*textures
, const char *caller
)
1253 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1254 _mesa_debug(ctx
, "%s %d\n", caller
, n
);
1257 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", caller
);
1261 create_textures(ctx
, target
, n
, textures
, caller
);
1267 /***********************************************************************/
1268 /** \name API functions */
1273 * Generate texture names.
1275 * \param n number of texture names to be generated.
1276 * \param textures an array in which will hold the generated texture names.
1278 * \sa glGenTextures(), glCreateTextures().
1280 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1281 * IDs which are stored in \p textures. Corresponding empty texture
1282 * objects are also generated.
1285 _mesa_GenTextures_no_error(GLsizei n
, GLuint
*textures
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 create_textures(ctx
, 0, n
, textures
, "glGenTextures");
1293 _mesa_GenTextures(GLsizei n
, GLuint
*textures
)
1295 GET_CURRENT_CONTEXT(ctx
);
1296 create_textures_err(ctx
, 0, n
, textures
, "glGenTextures");
1300 * Create texture objects.
1302 * \param target the texture target for each name to be generated.
1303 * \param n number of texture names to be generated.
1304 * \param textures an array in which will hold the generated texture names.
1306 * \sa glCreateTextures(), glGenTextures().
1308 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1309 * IDs which are stored in \p textures. Corresponding empty texture
1310 * objects are also generated.
1313 _mesa_CreateTextures_no_error(GLenum target
, GLsizei n
, GLuint
*textures
)
1315 GET_CURRENT_CONTEXT(ctx
);
1316 create_textures(ctx
, target
, n
, textures
, "glCreateTextures");
1321 _mesa_CreateTextures(GLenum target
, GLsizei n
, GLuint
*textures
)
1324 GET_CURRENT_CONTEXT(ctx
);
1327 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1328 * glCreateTextures should do with invalid targets, which was probably an
1329 * oversight. This conforms to the spec for glBindTexture.
1331 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1332 if (targetIndex
< 0) {
1333 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCreateTextures(target)");
1337 create_textures_err(ctx
, target
, n
, textures
, "glCreateTextures");
1341 * Check if the given texture object is bound to the current draw or
1342 * read framebuffer. If so, Unbind it.
1345 unbind_texobj_from_fbo(struct gl_context
*ctx
,
1346 struct gl_texture_object
*texObj
)
1348 bool progress
= false;
1350 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1351 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1354 * "If a texture object is deleted while its image is attached to one
1355 * or more attachment points in the currently bound framebuffer, then
1356 * it is as if FramebufferTexture* had been called, with a texture of
1357 * zero, for each attachment point to which this image was attached in
1358 * the currently bound framebuffer. In other words, this texture image
1359 * is first detached from all attachment points in the currently bound
1360 * framebuffer. Note that the texture image is specifically not
1361 * detached from any other framebuffer objects. Detaching the texture
1362 * image from any other framebuffer objects is the responsibility of
1365 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1366 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, texObj
);
1368 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1369 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1370 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, texObj
)
1375 /* Vertices are already flushed by _mesa_DeleteTextures */
1376 ctx
->NewState
|= _NEW_BUFFERS
;
1381 * Check if the given texture object is bound to any texture image units and
1382 * unbind it if so (revert to default textures).
1385 unbind_texobj_from_texunits(struct gl_context
*ctx
,
1386 struct gl_texture_object
*texObj
)
1388 const gl_texture_index index
= texObj
->TargetIndex
;
1391 if (texObj
->Target
== 0) {
1392 /* texture was never bound */
1396 assert(index
< NUM_TEXTURE_TARGETS
);
1398 for (u
= 0; u
< ctx
->Texture
.NumCurrentTexUsed
; u
++) {
1399 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
1401 if (texObj
== unit
->CurrentTex
[index
]) {
1402 /* Bind the default texture for this unit/target */
1403 _mesa_reference_texobj(&unit
->CurrentTex
[index
],
1404 ctx
->Shared
->DefaultTex
[index
]);
1405 unit
->_BoundTextures
&= ~(1 << index
);
1412 * Check if the given texture object is bound to any shader image unit
1413 * and unbind it if that's the case.
1416 unbind_texobj_from_image_units(struct gl_context
*ctx
,
1417 struct gl_texture_object
*texObj
)
1421 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; i
++) {
1422 struct gl_image_unit
*unit
= &ctx
->ImageUnits
[i
];
1424 if (texObj
== unit
->TexObj
) {
1425 _mesa_reference_texobj(&unit
->TexObj
, NULL
);
1426 *unit
= _mesa_default_image_unit(ctx
);
1433 * Unbinds all textures bound to the given texture image unit.
1436 unbind_textures_from_unit(struct gl_context
*ctx
, GLuint unit
)
1438 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1440 while (texUnit
->_BoundTextures
) {
1441 const GLuint index
= ffs(texUnit
->_BoundTextures
) - 1;
1442 struct gl_texture_object
*texObj
= ctx
->Shared
->DefaultTex
[index
];
1444 _mesa_reference_texobj(&texUnit
->CurrentTex
[index
], texObj
);
1446 /* Pass BindTexture call to device driver */
1447 if (ctx
->Driver
.BindTexture
)
1448 ctx
->Driver
.BindTexture(ctx
, unit
, 0, texObj
);
1450 texUnit
->_BoundTextures
&= ~(1 << index
);
1451 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1457 * Delete named textures.
1459 * \param n number of textures to be deleted.
1460 * \param textures array of texture IDs to be deleted.
1462 * \sa glDeleteTextures().
1464 * If we're about to delete a texture that's currently bound to any
1465 * texture unit, unbind the texture first. Decrement the reference
1466 * count on the texture object and delete it if it's zero.
1467 * Recall that texture objects can be shared among several rendering
1471 delete_textures(struct gl_context
*ctx
, GLsizei n
, const GLuint
*textures
)
1473 FLUSH_VERTICES(ctx
, 0); /* too complex */
1478 for (GLsizei i
= 0; i
< n
; i
++) {
1479 if (textures
[i
] > 0) {
1480 struct gl_texture_object
*delObj
1481 = _mesa_lookup_texture(ctx
, textures
[i
]);
1484 _mesa_lock_texture(ctx
, delObj
);
1486 /* Check if texture is bound to any framebuffer objects.
1488 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1490 unbind_texobj_from_fbo(ctx
, delObj
);
1492 /* Check if this texture is currently bound to any texture units.
1495 unbind_texobj_from_texunits(ctx
, delObj
);
1497 /* Check if this texture is currently bound to any shader
1498 * image unit. If so, unbind it.
1499 * See section 3.9.X of GL_ARB_shader_image_load_store.
1501 unbind_texobj_from_image_units(ctx
, delObj
);
1503 /* Make all handles that reference this texture object non-resident
1504 * in the current context.
1506 _mesa_make_texture_handles_non_resident(ctx
, delObj
);
1508 _mesa_unlock_texture(ctx
, delObj
);
1510 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1512 /* The texture _name_ is now free for re-use.
1513 * Remove it from the hash table now.
1515 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
1517 /* Unreference the texobj. If refcount hits zero, the texture
1520 _mesa_reference_texobj(&delObj
, NULL
);
1527 * This deletes a texObj without altering the hash table.
1530 _mesa_delete_nameless_texture(struct gl_context
*ctx
,
1531 struct gl_texture_object
*texObj
)
1536 FLUSH_VERTICES(ctx
, 0);
1538 _mesa_lock_texture(ctx
, texObj
);
1540 /* Check if texture is bound to any framebuffer objects.
1542 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1544 unbind_texobj_from_fbo(ctx
, texObj
);
1546 /* Check if this texture is currently bound to any texture units.
1549 unbind_texobj_from_texunits(ctx
, texObj
);
1551 /* Check if this texture is currently bound to any shader
1552 * image unit. If so, unbind it.
1553 * See section 3.9.X of GL_ARB_shader_image_load_store.
1555 unbind_texobj_from_image_units(ctx
, texObj
);
1557 _mesa_unlock_texture(ctx
, texObj
);
1559 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1561 /* Unreference the texobj. If refcount hits zero, the texture
1564 _mesa_reference_texobj(&texObj
, NULL
);
1569 _mesa_DeleteTextures_no_error(GLsizei n
, const GLuint
*textures
)
1571 GET_CURRENT_CONTEXT(ctx
);
1572 delete_textures(ctx
, n
, textures
);
1577 _mesa_DeleteTextures(GLsizei n
, const GLuint
*textures
)
1579 GET_CURRENT_CONTEXT(ctx
);
1581 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1582 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1585 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n < 0)");
1589 delete_textures(ctx
, n
, textures
);
1594 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1595 * into the corresponding Mesa texture target index.
1596 * Note that proxy targets are not valid here.
1597 * \return TEXTURE_x_INDEX or -1 if target is invalid
1600 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1604 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1606 return TEXTURE_2D_INDEX
;
1608 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1609 case GL_TEXTURE_CUBE_MAP
:
1610 return ctx
->Extensions
.ARB_texture_cube_map
1611 ? TEXTURE_CUBE_INDEX
: -1;
1612 case GL_TEXTURE_RECTANGLE
:
1613 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1614 ? TEXTURE_RECT_INDEX
: -1;
1615 case GL_TEXTURE_1D_ARRAY
:
1616 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1617 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1618 case GL_TEXTURE_2D_ARRAY
:
1619 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1620 || _mesa_is_gles3(ctx
)
1621 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1622 case GL_TEXTURE_BUFFER
:
1623 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
1624 _mesa_has_OES_texture_buffer(ctx
)) ?
1625 TEXTURE_BUFFER_INDEX
: -1;
1626 case GL_TEXTURE_EXTERNAL_OES
:
1627 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1628 ? TEXTURE_EXTERNAL_INDEX
: -1;
1629 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1630 return _mesa_has_texture_cube_map_array(ctx
)
1631 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1632 case GL_TEXTURE_2D_MULTISAMPLE
:
1633 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1634 _mesa_is_gles31(ctx
)) ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1635 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1636 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1637 _mesa_is_gles31(ctx
))
1638 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1646 * Do actual texture binding. All error checking should have been done prior
1647 * to calling this function. Note that the texture target (1D, 2D, etc) is
1648 * always specified by the texObj->TargetIndex.
1650 * \param unit index of texture unit to update
1651 * \param texObj the new texture object (cannot be NULL)
1654 bind_texture_object(struct gl_context
*ctx
, unsigned unit
,
1655 struct gl_texture_object
*texObj
)
1657 struct gl_texture_unit
*texUnit
;
1660 assert(unit
< ARRAY_SIZE(ctx
->Texture
.Unit
));
1661 texUnit
= &ctx
->Texture
.Unit
[unit
];
1664 assert(valid_texture_object(texObj
));
1666 targetIndex
= texObj
->TargetIndex
;
1667 assert(targetIndex
>= 0);
1668 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1670 /* Check if this texture is only used by this context and is already bound.
1671 * If so, just return. For GL_OES_image_external, rebinding the texture
1672 * always must invalidate cached resources.
1674 if (targetIndex
!= TEXTURE_EXTERNAL_INDEX
) {
1676 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1677 early_out
= ((ctx
->Shared
->RefCount
== 1)
1678 && (texObj
== texUnit
->CurrentTex
[targetIndex
]));
1679 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1685 /* flush before changing binding */
1686 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
);
1688 /* If the refcount on the previously bound texture is decremented to
1689 * zero, it'll be deleted here.
1691 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], texObj
);
1693 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1696 if (texObj
->Name
!= 0)
1697 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1699 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1701 /* Pass BindTexture call to device driver */
1702 if (ctx
->Driver
.BindTexture
) {
1703 ctx
->Driver
.BindTexture(ctx
, unit
, texObj
->Target
, texObj
);
1708 * Light-weight bind texture for internal users
1710 * This is really just \c finish_texture_init plus \c bind_texture_object.
1711 * This is intended to be used by internal Mesa functions that use
1712 * \c _mesa_CreateTexture and need to bind textures (e.g., meta).
1715 _mesa_bind_texture(struct gl_context
*ctx
, GLenum target
,
1716 struct gl_texture_object
*tex_obj
)
1718 const GLint targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1720 assert(targetIndex
>= 0 && targetIndex
< NUM_TEXTURE_TARGETS
);
1722 if (tex_obj
->Target
== 0)
1723 finish_texture_init(ctx
, target
, tex_obj
, targetIndex
);
1725 assert(tex_obj
->Target
== target
);
1726 assert(tex_obj
->TargetIndex
== targetIndex
);
1728 bind_texture_object(ctx
, ctx
->Texture
.CurrentUnit
, tex_obj
);
1731 struct gl_texture_object
*
1732 _mesa_lookup_or_create_texture(struct gl_context
*ctx
, GLenum target
,
1733 GLuint texName
, bool no_error
, bool is_ext_dsa
,
1736 struct gl_texture_object
*newTexObj
= NULL
;
1740 if (_mesa_is_proxy_texture(target
)) {
1741 /* EXT_dsa allows proxy targets only when texName is 0 */
1743 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target = %s)", caller
,
1744 _mesa_enum_to_string(target
));
1747 return _mesa_get_current_tex_object(ctx
, target
);
1749 if (GL_TEXTURE_CUBE_MAP_POSITIVE_X
<= target
&&
1750 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
1751 target
= GL_TEXTURE_CUBE_MAP
;
1755 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1756 if (!no_error
&& targetIndex
< 0) {
1757 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target = %s)", caller
,
1758 _mesa_enum_to_string(target
));
1761 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1764 * Get pointer to new texture object (newTexObj)
1767 /* Use a default texture object */
1768 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1770 /* non-default texture object */
1771 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1773 /* error checking */
1775 newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1776 /* The named texture object's target doesn't match the
1779 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1780 "%s(target mismatch)", caller
);
1783 if (newTexObj
->Target
== 0) {
1784 finish_texture_init(ctx
, target
, newTexObj
, targetIndex
);
1787 if (!no_error
&& ctx
->API
== API_OPENGL_CORE
) {
1788 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1789 "%s(non-gen name)", caller
);
1793 /* if this is a new texture id, allocate a texture object now */
1794 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1796 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1800 /* and insert it into hash table */
1801 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1805 assert(newTexObj
->Target
== target
);
1806 assert(newTexObj
->TargetIndex
== targetIndex
);
1812 * Implement glBindTexture(). Do error checking, look-up or create a new
1813 * texture object, then bind it in the current texture unit.
1815 * \param target texture target.
1816 * \param texName texture name.
1817 * \param texunit texture unit.
1819 static ALWAYS_INLINE
void
1820 bind_texture(struct gl_context
*ctx
, GLenum target
, GLuint texName
,
1821 GLenum texunit
, bool no_error
, const char *caller
)
1823 struct gl_texture_object
*newTexObj
=
1824 _mesa_lookup_or_create_texture(ctx
, target
, texName
, no_error
, false,
1829 bind_texture_object(ctx
, texunit
, newTexObj
);
1833 _mesa_BindTexture_no_error(GLenum target
, GLuint texName
)
1835 GET_CURRENT_CONTEXT(ctx
);
1836 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, true,
1842 _mesa_BindTexture(GLenum target
, GLuint texName
)
1844 GET_CURRENT_CONTEXT(ctx
);
1846 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1847 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1848 _mesa_enum_to_string(target
), (GLint
) texName
);
1850 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, false,
1856 _mesa_BindMultiTextureEXT(GLenum texunit
, GLenum target
, GLuint texture
)
1858 GET_CURRENT_CONTEXT(ctx
);
1860 unsigned unit
= texunit
- GL_TEXTURE0
;
1862 if (texunit
< GL_TEXTURE0
|| unit
>= _mesa_max_tex_unit(ctx
)) {
1863 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindMultiTextureEXT(texunit=%s)",
1864 _mesa_enum_to_string(texunit
));
1868 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1869 _mesa_debug(ctx
, "glBindMultiTextureEXT %s %d\n",
1870 _mesa_enum_to_string(texunit
), (GLint
) texture
);
1872 bind_texture(ctx
, target
, texture
, unit
, false, "glBindMultiTextureEXT");
1877 * OpenGL 4.5 / GL_ARB_direct_state_access glBindTextureUnit().
1879 * \param unit texture unit.
1880 * \param texture texture name.
1882 * \sa glBindTexture().
1884 * If the named texture is 0, this will reset each target for the specified
1885 * texture unit to its default texture.
1886 * If the named texture is not 0 or a recognized texture name, this throws
1887 * GL_INVALID_OPERATION.
1889 static ALWAYS_INLINE
void
1890 bind_texture_unit(struct gl_context
*ctx
, GLuint unit
, GLuint texture
,
1893 struct gl_texture_object
*texObj
;
1895 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1897 * "When texture is zero, each of the targets enumerated at the
1898 * beginning of this section is reset to its default texture for the
1899 * corresponding texture image unit."
1902 unbind_textures_from_unit(ctx
, unit
);
1906 /* Get the non-default texture object */
1907 texObj
= _mesa_lookup_texture(ctx
, texture
);
1909 /* Error checking */
1911 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1912 "glBindTextureUnit(non-gen name)");
1916 if (texObj
->Target
== 0) {
1917 /* Texture object was gen'd but never bound so the target is not set */
1918 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindTextureUnit(target)");
1923 assert(valid_texture_object(texObj
));
1925 bind_texture_object(ctx
, unit
, texObj
);
1930 _mesa_BindTextureUnit_no_error(GLuint unit
, GLuint texture
)
1932 GET_CURRENT_CONTEXT(ctx
);
1933 bind_texture_unit(ctx
, unit
, texture
, true);
1938 _mesa_BindTextureUnit(GLuint unit
, GLuint texture
)
1940 GET_CURRENT_CONTEXT(ctx
);
1942 if (unit
>= _mesa_max_tex_unit(ctx
)) {
1943 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindTextureUnit(unit=%u)", unit
);
1947 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1948 _mesa_debug(ctx
, "glBindTextureUnit %s %d\n",
1949 _mesa_enum_to_string(GL_TEXTURE0
+unit
), (GLint
) texture
);
1951 bind_texture_unit(ctx
, unit
, texture
, false);
1956 * OpenGL 4.4 / GL_ARB_multi_bind glBindTextures().
1958 static ALWAYS_INLINE
void
1959 bind_textures(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
1960 const GLuint
*textures
, bool no_error
)
1965 /* Note that the error semantics for multi-bind commands differ from
1966 * those of other GL commands.
1968 * The issues section in the ARB_multi_bind spec says:
1970 * "(11) Typically, OpenGL specifies that if an error is generated by
1971 * a command, that command has no effect. This is somewhat
1972 * unfortunate for multi-bind commands, because it would require
1973 * a first pass to scan the entire list of bound objects for
1974 * errors and then a second pass to actually perform the
1975 * bindings. Should we have different error semantics?
1977 * RESOLVED: Yes. In this specification, when the parameters for
1978 * one of the <count> binding points are invalid, that binding
1979 * point is not updated and an error will be generated. However,
1980 * other binding points in the same command will be updated if
1981 * their parameters are valid and no other error occurs."
1984 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
1986 for (i
= 0; i
< count
; i
++) {
1987 if (textures
[i
] != 0) {
1988 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
1989 struct gl_texture_object
*current
= texUnit
->_Current
;
1990 struct gl_texture_object
*texObj
;
1992 if (current
&& current
->Name
== textures
[i
])
1995 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
1997 if (texObj
&& texObj
->Target
!= 0) {
1998 bind_texture_object(ctx
, first
+ i
, texObj
);
1999 } else if (!no_error
) {
2000 /* The ARB_multi_bind spec says:
2002 * "An INVALID_OPERATION error is generated if any value
2003 * in <textures> is not zero or the name of an existing
2004 * texture object (per binding)."
2006 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2007 "glBindTextures(textures[%d]=%u is not zero "
2008 "or the name of an existing texture object)",
2012 unbind_textures_from_unit(ctx
, first
+ i
);
2016 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
2018 /* Unbind all textures in the range <first> through <first>+<count>-1 */
2019 for (i
= 0; i
< count
; i
++)
2020 unbind_textures_from_unit(ctx
, first
+ i
);
2026 _mesa_BindTextures_no_error(GLuint first
, GLsizei count
, const GLuint
*textures
)
2028 GET_CURRENT_CONTEXT(ctx
);
2029 bind_textures(ctx
, first
, count
, textures
, true);
2034 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
2036 GET_CURRENT_CONTEXT(ctx
);
2038 /* The ARB_multi_bind spec says:
2040 * "An INVALID_OPERATION error is generated if <first> + <count>
2041 * is greater than the number of texture image units supported
2042 * by the implementation."
2044 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
2045 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2046 "glBindTextures(first=%u + count=%d > the value of "
2047 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
2048 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
2052 bind_textures(ctx
, first
, count
, textures
, false);
2057 * Set texture priorities.
2059 * \param n number of textures.
2060 * \param texName texture names.
2061 * \param priorities corresponding texture priorities.
2063 * \sa glPrioritizeTextures().
2065 * Looks up each texture in the hash, clamps the corresponding priority between
2066 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
2069 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
2070 const GLclampf
*priorities
)
2072 GET_CURRENT_CONTEXT(ctx
);
2075 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2076 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
2078 FLUSH_VERTICES(ctx
, 0);
2081 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
2088 for (i
= 0; i
< n
; i
++) {
2089 if (texName
[i
] > 0) {
2090 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2092 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
2097 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2103 * See if textures are loaded in texture memory.
2105 * \param n number of textures to query.
2106 * \param texName array with the texture names.
2107 * \param residences array which will hold the residence status.
2109 * \return GL_TRUE if all textures are resident and
2110 * residences is left unchanged,
2112 * Note: we assume all textures are always resident
2114 GLboolean GLAPIENTRY
2115 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
2116 GLboolean
*residences
)
2118 GET_CURRENT_CONTEXT(ctx
);
2119 GLboolean allResident
= GL_TRUE
;
2121 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2123 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2124 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
2127 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
2131 if (!texName
|| !residences
)
2134 /* We only do error checking on the texture names */
2135 for (i
= 0; i
< n
; i
++) {
2136 struct gl_texture_object
*t
;
2137 if (texName
[i
] == 0) {
2138 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2141 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2143 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2153 * See if a name corresponds to a texture.
2155 * \param texture texture name.
2157 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
2160 * \sa glIsTexture().
2162 * Calls _mesa_HashLookup().
2164 GLboolean GLAPIENTRY
2165 _mesa_IsTexture( GLuint texture
)
2167 struct gl_texture_object
*t
;
2168 GET_CURRENT_CONTEXT(ctx
);
2169 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2171 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2172 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
2177 t
= _mesa_lookup_texture(ctx
, texture
);
2179 /* IsTexture is true only after object has been bound once. */
2180 return t
&& t
->Target
;
2185 * Simplest implementation of texture locking: grab the shared tex
2186 * mutex. Examine the shared context state timestamp and if there has
2187 * been a change, set the appropriate bits in ctx->NewState.
2189 * This is used to deal with synchronizing things when a texture object
2190 * is used/modified by different contexts (or threads) which are sharing
2193 * See also _mesa_lock/unlock_texture() in teximage.h
2196 _mesa_lock_context_textures( struct gl_context
*ctx
)
2198 mtx_lock(&ctx
->Shared
->TexMutex
);
2200 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
2201 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2202 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
2208 _mesa_unlock_context_textures( struct gl_context
*ctx
)
2210 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
2211 mtx_unlock(&ctx
->Shared
->TexMutex
);
2216 _mesa_InvalidateTexSubImage_no_error(GLuint texture
, GLint level
, GLint xoffset
,
2217 GLint yoffset
, GLint zoffset
,
2218 GLsizei width
, GLsizei height
,
2226 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
2227 GLint yoffset
, GLint zoffset
, GLsizei width
,
2228 GLsizei height
, GLsizei depth
)
2230 struct gl_texture_object
*t
;
2231 struct gl_texture_image
*image
;
2232 GET_CURRENT_CONTEXT(ctx
);
2234 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2235 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
2237 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
2238 "glInvalidateTexSubImage");
2240 /* The GL_ARB_invalidate_subdata spec says:
2242 * "...the specified subregion must be between -<b> and <dim>+<b> where
2243 * <dim> is the size of the dimension of the texture image, and <b> is
2244 * the size of the border of that texture image, otherwise
2245 * INVALID_VALUE is generated (border is not applied to dimensions that
2246 * don't exist in a given texture target)."
2248 image
= t
->Image
[0][level
];
2257 /* The GL_ARB_invalidate_subdata spec says:
2259 * "For texture targets that don't have certain dimensions, this
2260 * command treats those dimensions as having a size of 1. For
2261 * example, to invalidate a portion of a two-dimensional texture,
2262 * the application would use <zoffset> equal to zero and <depth>
2265 switch (t
->Target
) {
2266 case GL_TEXTURE_BUFFER
:
2275 xBorder
= image
->Border
;
2278 imageWidth
= image
->Width
;
2282 case GL_TEXTURE_1D_ARRAY
:
2283 xBorder
= image
->Border
;
2286 imageWidth
= image
->Width
;
2287 imageHeight
= image
->Height
;
2291 case GL_TEXTURE_CUBE_MAP
:
2292 case GL_TEXTURE_RECTANGLE
:
2293 case GL_TEXTURE_2D_MULTISAMPLE
:
2294 xBorder
= image
->Border
;
2295 yBorder
= image
->Border
;
2297 imageWidth
= image
->Width
;
2298 imageHeight
= image
->Height
;
2301 case GL_TEXTURE_2D_ARRAY
:
2302 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2303 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2304 xBorder
= image
->Border
;
2305 yBorder
= image
->Border
;
2307 imageWidth
= image
->Width
;
2308 imageHeight
= image
->Height
;
2309 imageDepth
= image
->Depth
;
2312 xBorder
= image
->Border
;
2313 yBorder
= image
->Border
;
2314 zBorder
= image
->Border
;
2315 imageWidth
= image
->Width
;
2316 imageHeight
= image
->Height
;
2317 imageDepth
= image
->Depth
;
2320 assert(!"Should not get here.");
2330 if (xoffset
< -xBorder
) {
2331 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
2335 if (xoffset
+ width
> imageWidth
+ xBorder
) {
2336 _mesa_error(ctx
, GL_INVALID_VALUE
,
2337 "glInvalidateSubTexImage(xoffset+width)");
2341 if (yoffset
< -yBorder
) {
2342 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
2346 if (yoffset
+ height
> imageHeight
+ yBorder
) {
2347 _mesa_error(ctx
, GL_INVALID_VALUE
,
2348 "glInvalidateSubTexImage(yoffset+height)");
2352 if (zoffset
< -zBorder
) {
2353 _mesa_error(ctx
, GL_INVALID_VALUE
,
2354 "glInvalidateSubTexImage(zoffset)");
2358 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
2359 _mesa_error(ctx
, GL_INVALID_VALUE
,
2360 "glInvalidateSubTexImage(zoffset+depth)");
2365 /* We don't actually do anything for this yet. Just return after
2366 * validating the parameters and generating the required errors.
2373 _mesa_InvalidateTexImage_no_error(GLuint texture
, GLint level
)
2380 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
2382 GET_CURRENT_CONTEXT(ctx
);
2384 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2385 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
2387 invalidate_tex_image_error_check(ctx
, texture
, level
,
2388 "glInvalidateTexImage");
2390 /* We don't actually do anything for this yet. Just return after
2391 * validating the parameters and generating the required errors.