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 if (ctx
->Driver
.TextureRemovedFromShared
) {
1518 ctx
->Driver
.TextureRemovedFromShared(ctx
, delObj
);
1521 /* Unreference the texobj. If refcount hits zero, the texture
1524 _mesa_reference_texobj(&delObj
, NULL
);
1531 * This deletes a texObj without altering the hash table.
1534 _mesa_delete_nameless_texture(struct gl_context
*ctx
,
1535 struct gl_texture_object
*texObj
)
1540 FLUSH_VERTICES(ctx
, 0);
1542 _mesa_lock_texture(ctx
, texObj
);
1544 /* Check if texture is bound to any framebuffer objects.
1546 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1548 unbind_texobj_from_fbo(ctx
, texObj
);
1550 /* Check if this texture is currently bound to any texture units.
1553 unbind_texobj_from_texunits(ctx
, texObj
);
1555 /* Check if this texture is currently bound to any shader
1556 * image unit. If so, unbind it.
1557 * See section 3.9.X of GL_ARB_shader_image_load_store.
1559 unbind_texobj_from_image_units(ctx
, texObj
);
1561 _mesa_unlock_texture(ctx
, texObj
);
1563 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
1565 /* Unreference the texobj. If refcount hits zero, the texture
1568 _mesa_reference_texobj(&texObj
, NULL
);
1573 _mesa_DeleteTextures_no_error(GLsizei n
, const GLuint
*textures
)
1575 GET_CURRENT_CONTEXT(ctx
);
1576 delete_textures(ctx
, n
, textures
);
1581 _mesa_DeleteTextures(GLsizei n
, const GLuint
*textures
)
1583 GET_CURRENT_CONTEXT(ctx
);
1585 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1586 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1589 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n < 0)");
1593 delete_textures(ctx
, n
, textures
);
1598 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1599 * into the corresponding Mesa texture target index.
1600 * Note that proxy targets are not valid here.
1601 * \return TEXTURE_x_INDEX or -1 if target is invalid
1604 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1608 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1610 return TEXTURE_2D_INDEX
;
1612 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1613 case GL_TEXTURE_CUBE_MAP
:
1614 return ctx
->Extensions
.ARB_texture_cube_map
1615 ? TEXTURE_CUBE_INDEX
: -1;
1616 case GL_TEXTURE_RECTANGLE
:
1617 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1618 ? TEXTURE_RECT_INDEX
: -1;
1619 case GL_TEXTURE_1D_ARRAY
:
1620 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1621 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1622 case GL_TEXTURE_2D_ARRAY
:
1623 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1624 || _mesa_is_gles3(ctx
)
1625 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1626 case GL_TEXTURE_BUFFER
:
1627 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
1628 _mesa_has_OES_texture_buffer(ctx
)) ?
1629 TEXTURE_BUFFER_INDEX
: -1;
1630 case GL_TEXTURE_EXTERNAL_OES
:
1631 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1632 ? TEXTURE_EXTERNAL_INDEX
: -1;
1633 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1634 return _mesa_has_texture_cube_map_array(ctx
)
1635 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1636 case GL_TEXTURE_2D_MULTISAMPLE
:
1637 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1638 _mesa_is_gles31(ctx
)) ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1639 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1640 return ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
) ||
1641 _mesa_is_gles31(ctx
))
1642 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1650 * Do actual texture binding. All error checking should have been done prior
1651 * to calling this function. Note that the texture target (1D, 2D, etc) is
1652 * always specified by the texObj->TargetIndex.
1654 * \param unit index of texture unit to update
1655 * \param texObj the new texture object (cannot be NULL)
1658 bind_texture_object(struct gl_context
*ctx
, unsigned unit
,
1659 struct gl_texture_object
*texObj
)
1661 struct gl_texture_unit
*texUnit
;
1664 assert(unit
< ARRAY_SIZE(ctx
->Texture
.Unit
));
1665 texUnit
= &ctx
->Texture
.Unit
[unit
];
1668 assert(valid_texture_object(texObj
));
1670 targetIndex
= texObj
->TargetIndex
;
1671 assert(targetIndex
>= 0);
1672 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1674 /* Check if this texture is only used by this context and is already bound.
1675 * If so, just return. For GL_OES_image_external, rebinding the texture
1676 * always must invalidate cached resources.
1678 if (targetIndex
!= TEXTURE_EXTERNAL_INDEX
) {
1680 simple_mtx_lock(&ctx
->Shared
->Mutex
);
1681 early_out
= ((ctx
->Shared
->RefCount
== 1)
1682 && (texObj
== texUnit
->CurrentTex
[targetIndex
]));
1683 simple_mtx_unlock(&ctx
->Shared
->Mutex
);
1689 /* flush before changing binding */
1690 FLUSH_VERTICES(ctx
, _NEW_TEXTURE_OBJECT
);
1692 /* If the refcount on the previously bound texture is decremented to
1693 * zero, it'll be deleted here.
1695 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], texObj
);
1697 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1700 if (texObj
->Name
!= 0)
1701 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1703 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1705 /* Pass BindTexture call to device driver */
1706 if (ctx
->Driver
.BindTexture
) {
1707 ctx
->Driver
.BindTexture(ctx
, unit
, texObj
->Target
, texObj
);
1712 * Light-weight bind texture for internal users
1714 * This is really just \c finish_texture_init plus \c bind_texture_object.
1715 * This is intended to be used by internal Mesa functions that use
1716 * \c _mesa_CreateTexture and need to bind textures (e.g., meta).
1719 _mesa_bind_texture(struct gl_context
*ctx
, GLenum target
,
1720 struct gl_texture_object
*tex_obj
)
1722 const GLint targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1724 assert(targetIndex
>= 0 && targetIndex
< NUM_TEXTURE_TARGETS
);
1726 if (tex_obj
->Target
== 0)
1727 finish_texture_init(ctx
, target
, tex_obj
, targetIndex
);
1729 assert(tex_obj
->Target
== target
);
1730 assert(tex_obj
->TargetIndex
== targetIndex
);
1732 bind_texture_object(ctx
, ctx
->Texture
.CurrentUnit
, tex_obj
);
1735 struct gl_texture_object
*
1736 _mesa_lookup_or_create_texture(struct gl_context
*ctx
, GLenum target
,
1737 GLuint texName
, bool no_error
, bool is_ext_dsa
,
1740 struct gl_texture_object
*newTexObj
= NULL
;
1744 if (_mesa_is_proxy_texture(target
)) {
1745 /* EXT_dsa allows proxy targets only when texName is 0 */
1747 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target = %s)", caller
,
1748 _mesa_enum_to_string(target
));
1751 return _mesa_get_current_tex_object(ctx
, target
);
1753 if (GL_TEXTURE_CUBE_MAP_POSITIVE_X
<= target
&&
1754 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
1755 target
= GL_TEXTURE_CUBE_MAP
;
1759 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1760 if (!no_error
&& targetIndex
< 0) {
1761 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target = %s)", caller
,
1762 _mesa_enum_to_string(target
));
1765 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1768 * Get pointer to new texture object (newTexObj)
1771 /* Use a default texture object */
1772 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1774 /* non-default texture object */
1775 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1777 /* error checking */
1779 newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1780 /* The named texture object's target doesn't match the
1783 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1784 "%s(target mismatch)", caller
);
1787 if (newTexObj
->Target
== 0) {
1788 finish_texture_init(ctx
, target
, newTexObj
, targetIndex
);
1791 if (!no_error
&& ctx
->API
== API_OPENGL_CORE
) {
1792 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1793 "%s(non-gen name)", caller
);
1797 /* if this is a new texture id, allocate a texture object now */
1798 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1800 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1804 /* and insert it into hash table */
1805 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1809 assert(newTexObj
->Target
== target
);
1810 assert(newTexObj
->TargetIndex
== targetIndex
);
1816 * Implement glBindTexture(). Do error checking, look-up or create a new
1817 * texture object, then bind it in the current texture unit.
1819 * \param target texture target.
1820 * \param texName texture name.
1821 * \param texunit texture unit.
1823 static ALWAYS_INLINE
void
1824 bind_texture(struct gl_context
*ctx
, GLenum target
, GLuint texName
,
1825 GLenum texunit
, bool no_error
, const char *caller
)
1827 struct gl_texture_object
*newTexObj
=
1828 _mesa_lookup_or_create_texture(ctx
, target
, texName
, no_error
, false,
1833 bind_texture_object(ctx
, texunit
, newTexObj
);
1837 _mesa_BindTexture_no_error(GLenum target
, GLuint texName
)
1839 GET_CURRENT_CONTEXT(ctx
);
1840 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, true,
1846 _mesa_BindTexture(GLenum target
, GLuint texName
)
1848 GET_CURRENT_CONTEXT(ctx
);
1850 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1851 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1852 _mesa_enum_to_string(target
), (GLint
) texName
);
1854 bind_texture(ctx
, target
, texName
, ctx
->Texture
.CurrentUnit
, false,
1860 _mesa_BindMultiTextureEXT(GLenum texunit
, GLenum target
, GLuint texture
)
1862 GET_CURRENT_CONTEXT(ctx
);
1864 unsigned unit
= texunit
- GL_TEXTURE0
;
1866 if (texunit
< GL_TEXTURE0
|| unit
>= _mesa_max_tex_unit(ctx
)) {
1867 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindMultiTextureEXT(texunit=%s)",
1868 _mesa_enum_to_string(texunit
));
1872 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1873 _mesa_debug(ctx
, "glBindMultiTextureEXT %s %d\n",
1874 _mesa_enum_to_string(texunit
), (GLint
) texture
);
1876 bind_texture(ctx
, target
, texture
, unit
, false, "glBindMultiTextureEXT");
1881 * OpenGL 4.5 / GL_ARB_direct_state_access glBindTextureUnit().
1883 * \param unit texture unit.
1884 * \param texture texture name.
1886 * \sa glBindTexture().
1888 * If the named texture is 0, this will reset each target for the specified
1889 * texture unit to its default texture.
1890 * If the named texture is not 0 or a recognized texture name, this throws
1891 * GL_INVALID_OPERATION.
1893 static ALWAYS_INLINE
void
1894 bind_texture_unit(struct gl_context
*ctx
, GLuint unit
, GLuint texture
,
1897 struct gl_texture_object
*texObj
;
1899 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1901 * "When texture is zero, each of the targets enumerated at the
1902 * beginning of this section is reset to its default texture for the
1903 * corresponding texture image unit."
1906 unbind_textures_from_unit(ctx
, unit
);
1910 /* Get the non-default texture object */
1911 texObj
= _mesa_lookup_texture(ctx
, texture
);
1913 /* Error checking */
1915 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1916 "glBindTextureUnit(non-gen name)");
1920 if (texObj
->Target
== 0) {
1921 /* Texture object was gen'd but never bound so the target is not set */
1922 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindTextureUnit(target)");
1927 assert(valid_texture_object(texObj
));
1929 bind_texture_object(ctx
, unit
, texObj
);
1934 _mesa_BindTextureUnit_no_error(GLuint unit
, GLuint texture
)
1936 GET_CURRENT_CONTEXT(ctx
);
1937 bind_texture_unit(ctx
, unit
, texture
, true);
1942 _mesa_BindTextureUnit(GLuint unit
, GLuint texture
)
1944 GET_CURRENT_CONTEXT(ctx
);
1946 if (unit
>= _mesa_max_tex_unit(ctx
)) {
1947 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindTextureUnit(unit=%u)", unit
);
1951 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1952 _mesa_debug(ctx
, "glBindTextureUnit %s %d\n",
1953 _mesa_enum_to_string(GL_TEXTURE0
+unit
), (GLint
) texture
);
1955 bind_texture_unit(ctx
, unit
, texture
, false);
1960 * OpenGL 4.4 / GL_ARB_multi_bind glBindTextures().
1962 static ALWAYS_INLINE
void
1963 bind_textures(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
1964 const GLuint
*textures
, bool no_error
)
1969 /* Note that the error semantics for multi-bind commands differ from
1970 * those of other GL commands.
1972 * The issues section in the ARB_multi_bind spec says:
1974 * "(11) Typically, OpenGL specifies that if an error is generated by
1975 * a command, that command has no effect. This is somewhat
1976 * unfortunate for multi-bind commands, because it would require
1977 * a first pass to scan the entire list of bound objects for
1978 * errors and then a second pass to actually perform the
1979 * bindings. Should we have different error semantics?
1981 * RESOLVED: Yes. In this specification, when the parameters for
1982 * one of the <count> binding points are invalid, that binding
1983 * point is not updated and an error will be generated. However,
1984 * other binding points in the same command will be updated if
1985 * their parameters are valid and no other error occurs."
1988 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
1990 for (i
= 0; i
< count
; i
++) {
1991 if (textures
[i
] != 0) {
1992 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
1993 struct gl_texture_object
*current
= texUnit
->_Current
;
1994 struct gl_texture_object
*texObj
;
1996 if (current
&& current
->Name
== textures
[i
])
1999 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
2001 if (texObj
&& texObj
->Target
!= 0) {
2002 bind_texture_object(ctx
, first
+ i
, texObj
);
2003 } else if (!no_error
) {
2004 /* The ARB_multi_bind spec says:
2006 * "An INVALID_OPERATION error is generated if any value
2007 * in <textures> is not zero or the name of an existing
2008 * texture object (per binding)."
2010 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2011 "glBindTextures(textures[%d]=%u is not zero "
2012 "or the name of an existing texture object)",
2016 unbind_textures_from_unit(ctx
, first
+ i
);
2020 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
2022 /* Unbind all textures in the range <first> through <first>+<count>-1 */
2023 for (i
= 0; i
< count
; i
++)
2024 unbind_textures_from_unit(ctx
, first
+ i
);
2030 _mesa_BindTextures_no_error(GLuint first
, GLsizei count
, const GLuint
*textures
)
2032 GET_CURRENT_CONTEXT(ctx
);
2033 bind_textures(ctx
, first
, count
, textures
, true);
2038 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
2040 GET_CURRENT_CONTEXT(ctx
);
2042 /* The ARB_multi_bind spec says:
2044 * "An INVALID_OPERATION error is generated if <first> + <count>
2045 * is greater than the number of texture image units supported
2046 * by the implementation."
2048 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
2049 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2050 "glBindTextures(first=%u + count=%d > the value of "
2051 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
2052 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
2056 bind_textures(ctx
, first
, count
, textures
, false);
2061 * Set texture priorities.
2063 * \param n number of textures.
2064 * \param texName texture names.
2065 * \param priorities corresponding texture priorities.
2067 * \sa glPrioritizeTextures().
2069 * Looks up each texture in the hash, clamps the corresponding priority between
2070 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
2073 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
2074 const GLclampf
*priorities
)
2076 GET_CURRENT_CONTEXT(ctx
);
2079 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2080 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
2082 FLUSH_VERTICES(ctx
, 0);
2085 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
2092 for (i
= 0; i
< n
; i
++) {
2093 if (texName
[i
] > 0) {
2094 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2096 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
2101 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2107 * See if textures are loaded in texture memory.
2109 * \param n number of textures to query.
2110 * \param texName array with the texture names.
2111 * \param residences array which will hold the residence status.
2113 * \return GL_TRUE if all textures are resident and
2114 * residences is left unchanged,
2116 * Note: we assume all textures are always resident
2118 GLboolean GLAPIENTRY
2119 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
2120 GLboolean
*residences
)
2122 GET_CURRENT_CONTEXT(ctx
);
2123 GLboolean allResident
= GL_TRUE
;
2125 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2127 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2128 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
2131 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
2135 if (!texName
|| !residences
)
2138 /* We only do error checking on the texture names */
2139 for (i
= 0; i
< n
; i
++) {
2140 struct gl_texture_object
*t
;
2141 if (texName
[i
] == 0) {
2142 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2145 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2147 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2157 * See if a name corresponds to a texture.
2159 * \param texture texture name.
2161 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
2164 * \sa glIsTexture().
2166 * Calls _mesa_HashLookup().
2168 GLboolean GLAPIENTRY
2169 _mesa_IsTexture( GLuint texture
)
2171 struct gl_texture_object
*t
;
2172 GET_CURRENT_CONTEXT(ctx
);
2173 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2175 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2176 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
2181 t
= _mesa_lookup_texture(ctx
, texture
);
2183 /* IsTexture is true only after object has been bound once. */
2184 return t
&& t
->Target
;
2189 * Simplest implementation of texture locking: grab the shared tex
2190 * mutex. Examine the shared context state timestamp and if there has
2191 * been a change, set the appropriate bits in ctx->NewState.
2193 * This is used to deal with synchronizing things when a texture object
2194 * is used/modified by different contexts (or threads) which are sharing
2197 * See also _mesa_lock/unlock_texture() in teximage.h
2200 _mesa_lock_context_textures( struct gl_context
*ctx
)
2202 mtx_lock(&ctx
->Shared
->TexMutex
);
2204 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
2205 ctx
->NewState
|= _NEW_TEXTURE_OBJECT
;
2206 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
2212 _mesa_unlock_context_textures( struct gl_context
*ctx
)
2214 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
2215 mtx_unlock(&ctx
->Shared
->TexMutex
);
2220 _mesa_InvalidateTexSubImage_no_error(GLuint texture
, GLint level
, GLint xoffset
,
2221 GLint yoffset
, GLint zoffset
,
2222 GLsizei width
, GLsizei height
,
2230 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
2231 GLint yoffset
, GLint zoffset
, GLsizei width
,
2232 GLsizei height
, GLsizei depth
)
2234 struct gl_texture_object
*t
;
2235 struct gl_texture_image
*image
;
2236 GET_CURRENT_CONTEXT(ctx
);
2238 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2239 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
2241 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
2242 "glInvalidateTexSubImage");
2244 /* The GL_ARB_invalidate_subdata spec says:
2246 * "...the specified subregion must be between -<b> and <dim>+<b> where
2247 * <dim> is the size of the dimension of the texture image, and <b> is
2248 * the size of the border of that texture image, otherwise
2249 * INVALID_VALUE is generated (border is not applied to dimensions that
2250 * don't exist in a given texture target)."
2252 image
= t
->Image
[0][level
];
2261 /* The GL_ARB_invalidate_subdata spec says:
2263 * "For texture targets that don't have certain dimensions, this
2264 * command treats those dimensions as having a size of 1. For
2265 * example, to invalidate a portion of a two-dimensional texture,
2266 * the application would use <zoffset> equal to zero and <depth>
2269 switch (t
->Target
) {
2270 case GL_TEXTURE_BUFFER
:
2279 xBorder
= image
->Border
;
2282 imageWidth
= image
->Width
;
2286 case GL_TEXTURE_1D_ARRAY
:
2287 xBorder
= image
->Border
;
2290 imageWidth
= image
->Width
;
2291 imageHeight
= image
->Height
;
2295 case GL_TEXTURE_CUBE_MAP
:
2296 case GL_TEXTURE_RECTANGLE
:
2297 case GL_TEXTURE_2D_MULTISAMPLE
:
2298 xBorder
= image
->Border
;
2299 yBorder
= image
->Border
;
2301 imageWidth
= image
->Width
;
2302 imageHeight
= image
->Height
;
2305 case GL_TEXTURE_2D_ARRAY
:
2306 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2307 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2308 xBorder
= image
->Border
;
2309 yBorder
= image
->Border
;
2311 imageWidth
= image
->Width
;
2312 imageHeight
= image
->Height
;
2313 imageDepth
= image
->Depth
;
2316 xBorder
= image
->Border
;
2317 yBorder
= image
->Border
;
2318 zBorder
= image
->Border
;
2319 imageWidth
= image
->Width
;
2320 imageHeight
= image
->Height
;
2321 imageDepth
= image
->Depth
;
2324 assert(!"Should not get here.");
2334 if (xoffset
< -xBorder
) {
2335 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
2339 if (xoffset
+ width
> imageWidth
+ xBorder
) {
2340 _mesa_error(ctx
, GL_INVALID_VALUE
,
2341 "glInvalidateSubTexImage(xoffset+width)");
2345 if (yoffset
< -yBorder
) {
2346 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
2350 if (yoffset
+ height
> imageHeight
+ yBorder
) {
2351 _mesa_error(ctx
, GL_INVALID_VALUE
,
2352 "glInvalidateSubTexImage(yoffset+height)");
2356 if (zoffset
< -zBorder
) {
2357 _mesa_error(ctx
, GL_INVALID_VALUE
,
2358 "glInvalidateSubTexImage(zoffset)");
2362 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
2363 _mesa_error(ctx
, GL_INVALID_VALUE
,
2364 "glInvalidateSubTexImage(zoffset+depth)");
2369 /* We don't actually do anything for this yet. Just return after
2370 * validating the parameters and generating the required errors.
2377 _mesa_InvalidateTexImage_no_error(GLuint texture
, GLint level
)
2384 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
2386 GET_CURRENT_CONTEXT(ctx
);
2388 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2389 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
2391 invalidate_tex_image_error_check(ctx
, texture
, level
,
2392 "glInvalidateTexImage");
2394 /* We don't actually do anything for this yet. Just return after
2395 * validating the parameters and generating the required errors.