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"
44 #include "program/prog_instruction.h"
48 /**********************************************************************/
49 /** \name Internal functions */
53 * This function checks for all valid combinations of Min and Mag filters for
54 * Float types, when extensions like OES_texture_float and
55 * OES_texture_float_linear are supported. OES_texture_float mentions support
56 * for NEAREST, NEAREST_MIPMAP_NEAREST magnification and minification filters.
57 * Mag filters like LINEAR and min filters like NEAREST_MIPMAP_LINEAR,
58 * LINEAR_MIPMAP_NEAREST and LINEAR_MIPMAP_LINEAR are only valid in case
59 * OES_texture_float_linear is supported.
61 * Returns true in case the filter is valid for given Float type else false.
64 valid_filter_for_float(const struct gl_context
*ctx
,
65 const struct gl_texture_object
*obj
)
67 switch (obj
->Sampler
.MagFilter
) {
69 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
71 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
75 case GL_NEAREST_MIPMAP_NEAREST
:
78 unreachable("Invalid mag filter");
81 switch (obj
->Sampler
.MinFilter
) {
83 case GL_NEAREST_MIPMAP_LINEAR
:
84 case GL_LINEAR_MIPMAP_NEAREST
:
85 case GL_LINEAR_MIPMAP_LINEAR
:
86 if (obj
->_IsHalfFloat
&& !ctx
->Extensions
.OES_texture_half_float_linear
) {
88 } else if (obj
->_IsFloat
&& !ctx
->Extensions
.OES_texture_float_linear
) {
92 case GL_NEAREST_MIPMAP_NEAREST
:
95 unreachable("Invalid min filter");
102 * Return the gl_texture_object for a given ID.
104 struct gl_texture_object
*
105 _mesa_lookup_texture(struct gl_context
*ctx
, GLuint id
)
107 return (struct gl_texture_object
*)
108 _mesa_HashLookup(ctx
->Shared
->TexObjects
, id
);
112 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
113 * is not in the hash table. After calling _mesa_error, it returns NULL.
115 struct gl_texture_object
*
116 _mesa_lookup_texture_err(struct gl_context
*ctx
, GLuint id
, const char* func
)
118 struct gl_texture_object
*texObj
;
120 texObj
= _mesa_lookup_texture(ctx
, id
); /* Returns NULL if not found. */
123 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(texture)", func
);
129 _mesa_begin_texture_lookups(struct gl_context
*ctx
)
131 _mesa_HashLockMutex(ctx
->Shared
->TexObjects
);
136 _mesa_end_texture_lookups(struct gl_context
*ctx
)
138 _mesa_HashUnlockMutex(ctx
->Shared
->TexObjects
);
142 struct gl_texture_object
*
143 _mesa_lookup_texture_locked(struct gl_context
*ctx
, GLuint id
)
145 return (struct gl_texture_object
*)
146 _mesa_HashLookupLocked(ctx
->Shared
->TexObjects
, id
);
150 * Return a pointer to the current texture object for the given target
151 * on the current texture unit.
152 * Note: all <target> error checking should have been done by this point.
154 struct gl_texture_object
*
155 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
157 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
158 const GLboolean arrayTex
= ctx
->Extensions
.EXT_texture_array
;
162 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
163 case GL_PROXY_TEXTURE_1D
:
164 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
166 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
167 case GL_PROXY_TEXTURE_2D
:
168 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
170 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
171 case GL_PROXY_TEXTURE_3D
:
172 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
173 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
174 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
175 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
176 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
177 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
178 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
179 case GL_TEXTURE_CUBE_MAP_ARB
:
180 return ctx
->Extensions
.ARB_texture_cube_map
181 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
182 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
183 return ctx
->Extensions
.ARB_texture_cube_map
184 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
185 case GL_TEXTURE_CUBE_MAP_ARRAY
:
186 return ctx
->Extensions
.ARB_texture_cube_map_array
187 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
188 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
189 return ctx
->Extensions
.ARB_texture_cube_map_array
190 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
191 case GL_TEXTURE_RECTANGLE_NV
:
192 return ctx
->Extensions
.NV_texture_rectangle
193 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
194 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
195 return ctx
->Extensions
.NV_texture_rectangle
196 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
197 case GL_TEXTURE_1D_ARRAY_EXT
:
198 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
199 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
200 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
201 case GL_TEXTURE_2D_ARRAY_EXT
:
202 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
203 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
204 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
205 case GL_TEXTURE_BUFFER
:
206 return ctx
->API
== API_OPENGL_CORE
&&
207 ctx
->Extensions
.ARB_texture_buffer_object
?
208 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
209 case GL_TEXTURE_EXTERNAL_OES
:
210 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
211 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
212 case GL_TEXTURE_2D_MULTISAMPLE
:
213 return ctx
->Extensions
.ARB_texture_multisample
214 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
215 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
216 return ctx
->Extensions
.ARB_texture_multisample
217 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
218 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
219 return ctx
->Extensions
.ARB_texture_multisample
220 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
221 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
222 return ctx
->Extensions
.ARB_texture_multisample
223 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
225 _mesa_problem(NULL
, "bad target in _mesa_get_current_tex_object()");
232 * Allocate and initialize a new texture object. But don't put it into the
233 * texture object hash table.
235 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
238 * \param shared the shared GL state structure to contain the texture object
239 * \param name integer name for the texture object
240 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
241 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
244 * \return pointer to new texture object.
246 struct gl_texture_object
*
247 _mesa_new_texture_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
249 struct gl_texture_object
*obj
;
251 obj
= MALLOC_STRUCT(gl_texture_object
);
252 _mesa_initialize_texture_object(ctx
, obj
, name
, target
);
258 * Initialize a new texture object to default values.
259 * \param obj the texture object
260 * \param name the texture name
261 * \param target the texture target
264 _mesa_initialize_texture_object( struct gl_context
*ctx
,
265 struct gl_texture_object
*obj
,
266 GLuint name
, GLenum target
)
268 assert(target
== 0 ||
269 target
== GL_TEXTURE_1D
||
270 target
== GL_TEXTURE_2D
||
271 target
== GL_TEXTURE_3D
||
272 target
== GL_TEXTURE_CUBE_MAP_ARB
||
273 target
== GL_TEXTURE_RECTANGLE_NV
||
274 target
== GL_TEXTURE_1D_ARRAY_EXT
||
275 target
== GL_TEXTURE_2D_ARRAY_EXT
||
276 target
== GL_TEXTURE_EXTERNAL_OES
||
277 target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
278 target
== GL_TEXTURE_BUFFER
||
279 target
== GL_TEXTURE_2D_MULTISAMPLE
||
280 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
282 memset(obj
, 0, sizeof(*obj
));
283 /* init the non-zero fields */
284 mtx_init(&obj
->Mutex
, mtx_plain
);
287 obj
->Target
= target
;
288 obj
->Priority
= 1.0F
;
290 obj
->MaxLevel
= 1000;
292 /* must be one; no support for (YUV) planes in separate buffers */
293 obj
->RequiredTextureImageUnits
= 1;
296 if (target
== GL_TEXTURE_RECTANGLE_NV
||
297 target
== GL_TEXTURE_EXTERNAL_OES
) {
298 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
299 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
300 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
301 obj
->Sampler
.MinFilter
= GL_LINEAR
;
304 obj
->Sampler
.WrapS
= GL_REPEAT
;
305 obj
->Sampler
.WrapT
= GL_REPEAT
;
306 obj
->Sampler
.WrapR
= GL_REPEAT
;
307 obj
->Sampler
.MinFilter
= GL_NEAREST_MIPMAP_LINEAR
;
309 obj
->Sampler
.MagFilter
= GL_LINEAR
;
310 obj
->Sampler
.MinLod
= -1000.0;
311 obj
->Sampler
.MaxLod
= 1000.0;
312 obj
->Sampler
.LodBias
= 0.0;
313 obj
->Sampler
.MaxAnisotropy
= 1.0;
314 obj
->Sampler
.CompareMode
= GL_NONE
; /* ARB_shadow */
315 obj
->Sampler
.CompareFunc
= GL_LEQUAL
; /* ARB_shadow */
316 obj
->DepthMode
= ctx
->API
== API_OPENGL_CORE
? GL_RED
: GL_LUMINANCE
;
317 obj
->StencilSampling
= false;
318 obj
->Sampler
.CubeMapSeamless
= GL_FALSE
;
319 obj
->Swizzle
[0] = GL_RED
;
320 obj
->Swizzle
[1] = GL_GREEN
;
321 obj
->Swizzle
[2] = GL_BLUE
;
322 obj
->Swizzle
[3] = GL_ALPHA
;
323 obj
->_Swizzle
= SWIZZLE_NOOP
;
324 obj
->Sampler
.sRGBDecode
= GL_DECODE_EXT
;
325 obj
->BufferObjectFormat
= GL_R8
;
326 obj
->_BufferObjectFormat
= MESA_FORMAT_R_UNORM8
;
327 obj
->ImageFormatCompatibilityType
= GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE
;
332 * Some texture initialization can't be finished until we know which
333 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
336 finish_texture_init(struct gl_context
*ctx
, GLenum target
,
337 struct gl_texture_object
*obj
)
339 GLenum filter
= GL_LINEAR
;
340 assert(obj
->Target
== 0);
343 case GL_TEXTURE_2D_MULTISAMPLE
:
344 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
348 case GL_TEXTURE_RECTANGLE_NV
:
349 case GL_TEXTURE_EXTERNAL_OES
:
350 /* have to init wrap and filter state here - kind of klunky */
351 obj
->Sampler
.WrapS
= GL_CLAMP_TO_EDGE
;
352 obj
->Sampler
.WrapT
= GL_CLAMP_TO_EDGE
;
353 obj
->Sampler
.WrapR
= GL_CLAMP_TO_EDGE
;
354 obj
->Sampler
.MinFilter
= filter
;
355 obj
->Sampler
.MagFilter
= filter
;
356 if (ctx
->Driver
.TexParameter
) {
357 static const GLfloat fparam_wrap
[1] = {(GLfloat
) GL_CLAMP_TO_EDGE
};
358 const GLfloat fparam_filter
[1] = {(GLfloat
) filter
};
359 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_S
, fparam_wrap
);
360 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_T
, fparam_wrap
);
361 ctx
->Driver
.TexParameter(ctx
, obj
, GL_TEXTURE_WRAP_R
, fparam_wrap
);
362 ctx
->Driver
.TexParameter(ctx
, obj
,
363 GL_TEXTURE_MIN_FILTER
, fparam_filter
);
364 ctx
->Driver
.TexParameter(ctx
, obj
,
365 GL_TEXTURE_MAG_FILTER
, fparam_filter
);
370 /* nothing needs done */
377 * Deallocate a texture object struct. It should have already been
378 * removed from the texture object pool.
379 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
381 * \param shared the shared GL state to which the object belongs.
382 * \param texObj the texture object to delete.
385 _mesa_delete_texture_object(struct gl_context
*ctx
,
386 struct gl_texture_object
*texObj
)
390 /* Set Target to an invalid value. With some assertions elsewhere
391 * we can try to detect possible use of deleted textures.
393 texObj
->Target
= 0x99;
395 /* free the texture images */
396 for (face
= 0; face
< 6; face
++) {
397 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
398 if (texObj
->Image
[face
][i
]) {
399 ctx
->Driver
.DeleteTextureImage(ctx
, texObj
->Image
[face
][i
]);
404 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, NULL
);
406 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
407 mtx_destroy(&texObj
->Mutex
);
411 /* free this object */
417 * Copy texture object state from one texture object to another.
418 * Use for glPush/PopAttrib.
420 * \param dest destination texture object.
421 * \param src source texture object.
424 _mesa_copy_texture_object( struct gl_texture_object
*dest
,
425 const struct gl_texture_object
*src
)
427 dest
->Target
= src
->Target
;
428 dest
->TargetIndex
= src
->TargetIndex
;
429 dest
->Name
= src
->Name
;
430 dest
->Priority
= src
->Priority
;
431 dest
->Sampler
.BorderColor
.f
[0] = src
->Sampler
.BorderColor
.f
[0];
432 dest
->Sampler
.BorderColor
.f
[1] = src
->Sampler
.BorderColor
.f
[1];
433 dest
->Sampler
.BorderColor
.f
[2] = src
->Sampler
.BorderColor
.f
[2];
434 dest
->Sampler
.BorderColor
.f
[3] = src
->Sampler
.BorderColor
.f
[3];
435 dest
->Sampler
.WrapS
= src
->Sampler
.WrapS
;
436 dest
->Sampler
.WrapT
= src
->Sampler
.WrapT
;
437 dest
->Sampler
.WrapR
= src
->Sampler
.WrapR
;
438 dest
->Sampler
.MinFilter
= src
->Sampler
.MinFilter
;
439 dest
->Sampler
.MagFilter
= src
->Sampler
.MagFilter
;
440 dest
->Sampler
.MinLod
= src
->Sampler
.MinLod
;
441 dest
->Sampler
.MaxLod
= src
->Sampler
.MaxLod
;
442 dest
->Sampler
.LodBias
= src
->Sampler
.LodBias
;
443 dest
->BaseLevel
= src
->BaseLevel
;
444 dest
->MaxLevel
= src
->MaxLevel
;
445 dest
->Sampler
.MaxAnisotropy
= src
->Sampler
.MaxAnisotropy
;
446 dest
->Sampler
.CompareMode
= src
->Sampler
.CompareMode
;
447 dest
->Sampler
.CompareFunc
= src
->Sampler
.CompareFunc
;
448 dest
->Sampler
.CubeMapSeamless
= src
->Sampler
.CubeMapSeamless
;
449 dest
->DepthMode
= src
->DepthMode
;
450 dest
->StencilSampling
= src
->StencilSampling
;
451 dest
->Sampler
.sRGBDecode
= src
->Sampler
.sRGBDecode
;
452 dest
->_MaxLevel
= src
->_MaxLevel
;
453 dest
->_MaxLambda
= src
->_MaxLambda
;
454 dest
->GenerateMipmap
= src
->GenerateMipmap
;
455 dest
->_BaseComplete
= src
->_BaseComplete
;
456 dest
->_MipmapComplete
= src
->_MipmapComplete
;
457 COPY_4V(dest
->Swizzle
, src
->Swizzle
);
458 dest
->_Swizzle
= src
->_Swizzle
;
459 dest
->_IsHalfFloat
= src
->_IsHalfFloat
;
460 dest
->_IsFloat
= src
->_IsFloat
;
462 dest
->RequiredTextureImageUnits
= src
->RequiredTextureImageUnits
;
467 * Free all texture images of the given texture object.
469 * \param ctx GL context.
470 * \param t texture object.
472 * \sa _mesa_clear_texture_image().
475 _mesa_clear_texture_object(struct gl_context
*ctx
,
476 struct gl_texture_object
*texObj
)
480 if (texObj
->Target
== 0)
483 for (i
= 0; i
< MAX_FACES
; i
++) {
484 for (j
= 0; j
< MAX_TEXTURE_LEVELS
; j
++) {
485 struct gl_texture_image
*texImage
= texObj
->Image
[i
][j
];
487 _mesa_clear_texture_image(ctx
, texImage
);
494 * Check if the given texture object is valid by examining its Target field.
495 * For debugging only.
498 valid_texture_object(const struct gl_texture_object
*tex
)
500 switch (tex
->Target
) {
505 case GL_TEXTURE_CUBE_MAP_ARB
:
506 case GL_TEXTURE_RECTANGLE_NV
:
507 case GL_TEXTURE_1D_ARRAY_EXT
:
508 case GL_TEXTURE_2D_ARRAY_EXT
:
509 case GL_TEXTURE_BUFFER
:
510 case GL_TEXTURE_EXTERNAL_OES
:
511 case GL_TEXTURE_CUBE_MAP_ARRAY
:
512 case GL_TEXTURE_2D_MULTISAMPLE
:
513 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
516 _mesa_problem(NULL
, "invalid reference to a deleted texture object");
519 _mesa_problem(NULL
, "invalid texture object Target 0x%x, Id = %u",
520 tex
->Target
, tex
->Name
);
527 * Reference (or unreference) a texture object.
528 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
529 * If 'tex' is non-null, increment its refcount.
530 * This is normally only called from the _mesa_reference_texobj() macro
531 * when there's a real pointer change.
534 _mesa_reference_texobj_(struct gl_texture_object
**ptr
,
535 struct gl_texture_object
*tex
)
540 /* Unreference the old texture */
541 GLboolean deleteFlag
= GL_FALSE
;
542 struct gl_texture_object
*oldTex
= *ptr
;
544 assert(valid_texture_object(oldTex
));
545 (void) valid_texture_object
; /* silence warning in release builds */
547 mtx_lock(&oldTex
->Mutex
);
548 assert(oldTex
->RefCount
> 0);
551 deleteFlag
= (oldTex
->RefCount
== 0);
552 mtx_unlock(&oldTex
->Mutex
);
555 /* Passing in the context drastically changes the driver code for
556 * framebuffer deletion.
558 GET_CURRENT_CONTEXT(ctx
);
560 ctx
->Driver
.DeleteTexture(ctx
, oldTex
);
562 _mesa_problem(NULL
, "Unable to delete texture, no context");
570 /* reference new texture */
571 assert(valid_texture_object(tex
));
572 mtx_lock(&tex
->Mutex
);
573 if (tex
->RefCount
== 0) {
574 /* this texture's being deleted (look just above) */
575 /* Not sure this can every really happen. Warn if it does. */
576 _mesa_problem(NULL
, "referencing deleted texture object");
583 mtx_unlock(&tex
->Mutex
);
588 enum base_mipmap
{ BASE
, MIPMAP
};
592 * Mark a texture object as incomplete. There are actually three kinds of
594 * 1. "base incomplete": the base level of the texture is invalid so no
595 * texturing is possible.
596 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
597 * mipmap filtering isn't possible, but non-mipmap filtering is.
598 * 3. "texture incompleteness": some combination of texture state and
599 * sampler state renders the texture incomplete.
601 * \param t texture object
602 * \param bm either BASE or MIPMAP to indicate what's incomplete
603 * \param fmt... string describing why it's incomplete (for debugging).
606 incomplete(struct gl_texture_object
*t
, enum base_mipmap bm
,
607 const char *fmt
, ...)
609 if (MESA_DEBUG_FLAGS
& DEBUG_INCOMPLETE_TEXTURE
) {
614 vsnprintf(s
, sizeof(s
), fmt
, args
);
617 _mesa_debug(NULL
, "Texture Obj %d incomplete because: %s\n", t
->Name
, s
);
621 t
->_BaseComplete
= GL_FALSE
;
622 t
->_MipmapComplete
= GL_FALSE
;
627 * Examine a texture object to determine if it is complete.
629 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
632 * \param ctx GL context.
633 * \param t texture object.
635 * According to the texture target, verifies that each of the mipmaps is
636 * present and has the expected size.
639 _mesa_test_texobj_completeness( const struct gl_context
*ctx
,
640 struct gl_texture_object
*t
)
642 const GLint baseLevel
= t
->BaseLevel
;
643 const struct gl_texture_image
*baseImage
;
646 /* We'll set these to FALSE if tests fail below */
647 t
->_BaseComplete
= GL_TRUE
;
648 t
->_MipmapComplete
= GL_TRUE
;
650 if (t
->Target
== GL_TEXTURE_BUFFER
) {
651 /* Buffer textures are always considered complete. The obvious case where
652 * they would be incomplete (no BO attached) is actually specced to be
653 * undefined rendering results.
658 /* Detect cases where the application set the base level to an invalid
661 if ((baseLevel
< 0) || (baseLevel
>= MAX_TEXTURE_LEVELS
)) {
662 incomplete(t
, BASE
, "base level = %d is invalid", baseLevel
);
666 if (t
->MaxLevel
< baseLevel
) {
667 incomplete(t
, MIPMAP
, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
668 t
->MaxLevel
, baseLevel
);
672 baseImage
= t
->Image
[0][baseLevel
];
674 /* Always need the base level image */
676 incomplete(t
, BASE
, "Image[baseLevel=%d] == NULL", baseLevel
);
680 /* Check width/height/depth for zero */
681 if (baseImage
->Width
== 0 ||
682 baseImage
->Height
== 0 ||
683 baseImage
->Depth
== 0) {
684 incomplete(t
, BASE
, "texture width or height or depth = 0");
688 /* Check if the texture values are integer */
690 GLenum datatype
= _mesa_get_format_datatype(baseImage
->TexFormat
);
691 t
->_IsIntegerFormat
= datatype
== GL_INT
|| datatype
== GL_UNSIGNED_INT
;
694 /* Check if the texture type is Float or HalfFloatOES and ensure Min and Mag
695 * filters are supported in this case.
697 if (_mesa_is_gles(ctx
) && !valid_filter_for_float(ctx
, t
)) {
698 incomplete(t
, BASE
, "Filter is not supported with Float types.");
702 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
703 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
707 case GL_TEXTURE_1D_ARRAY_EXT
:
708 maxLevels
= ctx
->Const
.MaxTextureLevels
;
711 case GL_TEXTURE_2D_ARRAY_EXT
:
712 maxLevels
= ctx
->Const
.MaxTextureLevels
;
715 maxLevels
= ctx
->Const
.Max3DTextureLevels
;
717 case GL_TEXTURE_CUBE_MAP_ARB
:
718 case GL_TEXTURE_CUBE_MAP_ARRAY
:
719 maxLevels
= ctx
->Const
.MaxCubeTextureLevels
;
721 case GL_TEXTURE_RECTANGLE_NV
:
722 case GL_TEXTURE_BUFFER
:
723 case GL_TEXTURE_EXTERNAL_OES
:
724 case GL_TEXTURE_2D_MULTISAMPLE
:
725 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
726 maxLevels
= 1; /* no mipmapping */
729 _mesa_problem(ctx
, "Bad t->Target in _mesa_test_texobj_completeness");
733 assert(maxLevels
> 0);
735 t
->_MaxLevel
= MIN3(t
->MaxLevel
,
736 /* 'p' in the GL spec */
737 (int) (baseLevel
+ baseImage
->MaxNumLevels
- 1),
738 /* 'q' in the GL spec */
742 /* Adjust max level for views: the data store may have more levels than
745 t
->_MaxLevel
= MIN2(t
->_MaxLevel
, t
->NumLevels
- 1);
748 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
749 t
->_MaxLambda
= (GLfloat
) (t
->_MaxLevel
- baseLevel
);
752 /* This texture object was created with glTexStorage1/2/3D() so we
753 * know that all the mipmap levels are the right size and all cube
754 * map faces are the same size.
755 * We don't need to do any of the additional checks below.
760 if (t
->Target
== GL_TEXTURE_CUBE_MAP_ARB
) {
761 /* Make sure that all six cube map level 0 images are the same size.
762 * Note: we know that the image's width==height (we enforce that
763 * at glTexImage time) so we only need to test the width here.
766 assert(baseImage
->Width2
== baseImage
->Height
);
767 for (face
= 1; face
< 6; face
++) {
768 assert(t
->Image
[face
][baseLevel
] == NULL
||
769 t
->Image
[face
][baseLevel
]->Width2
==
770 t
->Image
[face
][baseLevel
]->Height2
);
771 if (t
->Image
[face
][baseLevel
] == NULL
||
772 t
->Image
[face
][baseLevel
]->Width2
!= baseImage
->Width2
) {
773 incomplete(t
, BASE
, "Cube face missing or mismatched size");
780 * Do mipmap consistency checking.
781 * Note: we don't care about the current texture sampler state here.
782 * To determine texture completeness we'll either look at _BaseComplete
783 * or _MipmapComplete depending on the current minification filter mode.
787 const GLint minLevel
= baseLevel
;
788 const GLint maxLevel
= t
->_MaxLevel
;
789 const GLuint numFaces
= _mesa_num_tex_faces(t
->Target
);
790 GLuint width
, height
, depth
, face
;
792 if (minLevel
> maxLevel
) {
793 incomplete(t
, MIPMAP
, "minLevel > maxLevel");
797 /* Get the base image's dimensions */
798 width
= baseImage
->Width2
;
799 height
= baseImage
->Height2
;
800 depth
= baseImage
->Depth2
;
802 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
803 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
805 for (i
= baseLevel
+ 1; i
< maxLevels
; i
++) {
806 /* Compute the expected size of image at level[i] */
810 if (height
> 1 && t
->Target
!= GL_TEXTURE_1D_ARRAY
) {
813 if (depth
> 1 && t
->Target
!= GL_TEXTURE_2D_ARRAY
814 && t
->Target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
818 /* loop over cube faces (or single face otherwise) */
819 for (face
= 0; face
< numFaces
; face
++) {
820 if (i
>= minLevel
&& i
<= maxLevel
) {
821 const struct gl_texture_image
*img
= t
->Image
[face
][i
];
824 incomplete(t
, MIPMAP
, "TexImage[%d] is missing", i
);
827 if (img
->TexFormat
!= baseImage
->TexFormat
) {
828 incomplete(t
, MIPMAP
, "Format[i] != Format[baseLevel]");
831 if (img
->Border
!= baseImage
->Border
) {
832 incomplete(t
, MIPMAP
, "Border[i] != Border[baseLevel]");
835 if (img
->Width2
!= width
) {
836 incomplete(t
, MIPMAP
, "TexImage[%d] bad width %u", i
,
840 if (img
->Height2
!= height
) {
841 incomplete(t
, MIPMAP
, "TexImage[%d] bad height %u", i
,
845 if (img
->Depth2
!= depth
) {
846 incomplete(t
, MIPMAP
, "TexImage[%d] bad depth %u", i
,
851 /* Extra checks for cube textures */
853 /* check that cube faces are the same size */
854 if (img
->Width2
!= t
->Image
[0][i
]->Width2
||
855 img
->Height2
!= t
->Image
[0][i
]->Height2
) {
856 incomplete(t
, MIPMAP
, "CubeMap Image[n][i] bad size");
863 if (width
== 1 && height
== 1 && depth
== 1) {
864 return; /* found smallest needed mipmap, all done! */
872 _mesa_cube_level_complete(const struct gl_texture_object
*texObj
,
875 const struct gl_texture_image
*img0
, *img
;
878 if (texObj
->Target
!= GL_TEXTURE_CUBE_MAP
)
881 if ((level
< 0) || (level
>= MAX_TEXTURE_LEVELS
))
884 /* check first face */
885 img0
= texObj
->Image
[0][level
];
888 img0
->Width
!= img0
->Height
)
891 /* check remaining faces vs. first face */
892 for (face
= 1; face
< 6; face
++) {
893 img
= texObj
->Image
[face
][level
];
895 img
->Width
!= img0
->Width
||
896 img
->Height
!= img0
->Height
||
897 img
->TexFormat
!= img0
->TexFormat
)
905 * Check if the given cube map texture is "cube complete" as defined in
906 * the OpenGL specification.
909 _mesa_cube_complete(const struct gl_texture_object
*texObj
)
911 return _mesa_cube_level_complete(texObj
, texObj
->BaseLevel
);
915 * Mark a texture object dirty. It forces the object to be incomplete
916 * and forces the context to re-validate its state.
918 * \param ctx GL context.
919 * \param texObj texture object.
922 _mesa_dirty_texobj(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
924 texObj
->_BaseComplete
= GL_FALSE
;
925 texObj
->_MipmapComplete
= GL_FALSE
;
926 ctx
->NewState
|= _NEW_TEXTURE
;
931 * Return pointer to a default/fallback texture of the given type/target.
932 * The texture is an RGBA texture with all texels = (0,0,0,1).
933 * That's the value a GLSL sampler should get when sampling from an
934 * incomplete texture.
936 struct gl_texture_object
*
937 _mesa_get_fallback_texture(struct gl_context
*ctx
, gl_texture_index tex
)
939 if (!ctx
->Shared
->FallbackTex
[tex
]) {
940 /* create fallback texture now */
941 const GLsizei width
= 1, height
= 1;
944 struct gl_texture_object
*texObj
;
945 struct gl_texture_image
*texImage
;
946 mesa_format texFormat
;
947 GLuint dims
, face
, numFaces
= 1;
950 for (face
= 0; face
< 6; face
++) {
953 texel
[4*face
+ 2] = 0x0;
954 texel
[4*face
+ 3] = 0xff;
958 case TEXTURE_2D_ARRAY_INDEX
:
960 target
= GL_TEXTURE_2D_ARRAY
;
962 case TEXTURE_1D_ARRAY_INDEX
:
964 target
= GL_TEXTURE_1D_ARRAY
;
966 case TEXTURE_CUBE_INDEX
:
968 target
= GL_TEXTURE_CUBE_MAP
;
971 case TEXTURE_3D_INDEX
:
973 target
= GL_TEXTURE_3D
;
975 case TEXTURE_RECT_INDEX
:
977 target
= GL_TEXTURE_RECTANGLE
;
979 case TEXTURE_2D_INDEX
:
981 target
= GL_TEXTURE_2D
;
983 case TEXTURE_1D_INDEX
:
985 target
= GL_TEXTURE_1D
;
987 case TEXTURE_BUFFER_INDEX
:
989 target
= GL_TEXTURE_BUFFER
;
991 case TEXTURE_CUBE_ARRAY_INDEX
:
993 target
= GL_TEXTURE_CUBE_MAP_ARRAY
;
996 case TEXTURE_EXTERNAL_INDEX
:
998 target
= GL_TEXTURE_EXTERNAL_OES
;
1000 case TEXTURE_2D_MULTISAMPLE_INDEX
:
1002 target
= GL_TEXTURE_2D_MULTISAMPLE
;
1004 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
:
1006 target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
1013 /* create texture object */
1014 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, target
);
1018 assert(texObj
->RefCount
== 1);
1019 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1020 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1022 texFormat
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
,
1026 /* need a loop here just for cube maps */
1027 for (face
= 0; face
< numFaces
; face
++) {
1030 if (target
== GL_TEXTURE_CUBE_MAP
)
1031 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
;
1033 faceTarget
= target
;
1035 /* initialize level[0] texture image */
1036 texImage
= _mesa_get_tex_image(ctx
, texObj
, faceTarget
, 0);
1038 _mesa_init_teximage_fields(ctx
, texImage
,
1040 (dims
> 1) ? height
: 1,
1041 (dims
> 2) ? depth
: 1,
1043 GL_RGBA
, texFormat
);
1045 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
1046 GL_RGBA
, GL_UNSIGNED_BYTE
, texel
,
1047 &ctx
->DefaultPacking
);
1050 _mesa_test_texobj_completeness(ctx
, texObj
);
1051 assert(texObj
->_BaseComplete
);
1052 assert(texObj
->_MipmapComplete
);
1054 ctx
->Shared
->FallbackTex
[tex
] = texObj
;
1056 return ctx
->Shared
->FallbackTex
[tex
];
1061 * Compute the size of the given texture object, in bytes.
1064 texture_size(const struct gl_texture_object
*texObj
)
1066 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1067 GLuint face
, level
, size
= 0;
1069 for (face
= 0; face
< numFaces
; face
++) {
1070 for (level
= 0; level
< MAX_TEXTURE_LEVELS
; level
++) {
1071 const struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
1073 GLuint sz
= _mesa_format_image_size(img
->TexFormat
, img
->Width
,
1074 img
->Height
, img
->Depth
);
1085 * Callback called from _mesa_HashWalk()
1088 count_tex_size(GLuint key
, void *data
, void *userData
)
1090 const struct gl_texture_object
*texObj
=
1091 (const struct gl_texture_object
*) data
;
1092 GLuint
*total
= (GLuint
*) userData
;
1096 *total
= *total
+ texture_size(texObj
);
1101 * Compute total size (in bytes) of all textures for the given context.
1102 * For debugging purposes.
1105 _mesa_total_texture_memory(struct gl_context
*ctx
)
1107 GLuint tgt
, total
= 0;
1109 _mesa_HashWalk(ctx
->Shared
->TexObjects
, count_tex_size
, &total
);
1111 /* plus, the default texture objects */
1112 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
1113 total
+= texture_size(ctx
->Shared
->DefaultTex
[tgt
]);
1121 * Return the base format for the given texture object by looking
1122 * at the base texture image.
1123 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
1126 _mesa_texture_base_format(const struct gl_texture_object
*texObj
)
1128 const struct gl_texture_image
*texImage
= _mesa_base_tex_image(texObj
);
1130 return texImage
? texImage
->_BaseFormat
: GL_NONE
;
1134 static struct gl_texture_object
*
1135 invalidate_tex_image_error_check(struct gl_context
*ctx
, GLuint texture
,
1136 GLint level
, const char *name
)
1138 /* The GL_ARB_invalidate_subdata spec says:
1140 * "If <texture> is zero or is not the name of a texture, the error
1141 * INVALID_VALUE is generated."
1143 * This performs the error check in a different order than listed in the
1144 * spec. We have to get the texture object before we can validate the
1145 * other parameters against values in the texture object.
1147 struct gl_texture_object
*const t
= _mesa_lookup_texture(ctx
, texture
);
1148 if (texture
== 0 || t
== NULL
) {
1149 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(texture)", name
);
1153 /* The GL_ARB_invalidate_subdata spec says:
1155 * "If <level> is less than zero or greater than the base 2 logarithm
1156 * of the maximum texture width, height, or depth, the error
1157 * INVALID_VALUE is generated."
1159 if (level
< 0 || level
> t
->MaxLevel
) {
1160 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1164 /* The GL_ARB_invalidate_subdata spec says:
1166 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1167 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1168 * is not zero, the error INVALID_VALUE is generated."
1171 switch (t
->Target
) {
1172 case GL_TEXTURE_RECTANGLE
:
1173 case GL_TEXTURE_BUFFER
:
1174 case GL_TEXTURE_2D_MULTISAMPLE
:
1175 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1176 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level)", name
);
1188 * Wrapper for the driver function. Need this because _mesa_new_texture_object
1189 * permits a target of 0 and does not initialize targetIndex.
1191 struct gl_texture_object
*
1192 _mesa_create_nameless_texture(struct gl_context
*ctx
, GLenum target
)
1194 struct gl_texture_object
*texObj
= NULL
;
1200 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0, target
);
1201 targetIndex
= _mesa_tex_target_to_index(ctx
, texObj
->Target
);
1202 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1203 texObj
->TargetIndex
= targetIndex
;
1209 * Helper function for glCreateTextures and glGenTextures. Need this because
1210 * glCreateTextures should throw errors if target = 0. This is not exposed to
1211 * the rest of Mesa to encourage Mesa internals to use nameless textures,
1212 * which do not require expensive hash lookups.
1215 create_textures(struct gl_context
*ctx
, GLenum target
,
1216 GLsizei n
, GLuint
*textures
, bool dsa
)
1220 const char *func
= dsa
? "Create" : "Gen";
1222 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1223 _mesa_debug(ctx
, "gl%sTextures %d\n", func
, n
);
1226 _mesa_error( ctx
, GL_INVALID_VALUE
, "gl%sTextures(n < 0)", func
);
1234 * This must be atomic (generation and allocation of texture IDs)
1236 mtx_lock(&ctx
->Shared
->Mutex
);
1238 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->TexObjects
, n
);
1240 /* Allocate new, empty texture objects */
1241 for (i
= 0; i
< n
; i
++) {
1242 struct gl_texture_object
*texObj
;
1244 GLuint name
= first
+ i
;
1245 texObj
= ctx
->Driver
.NewTextureObject(ctx
, name
, target
);
1247 mtx_unlock(&ctx
->Shared
->Mutex
);
1248 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sTextures", func
);
1252 /* Initialize the target index if target is non-zero. */
1254 targetIndex
= _mesa_tex_target_to_index(ctx
, texObj
->Target
);
1255 if (targetIndex
< 0) { /* Bad Target */
1256 mtx_unlock(&ctx
->Shared
->Mutex
);
1257 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sTextures(target = %s)",
1258 func
, _mesa_lookup_enum_by_nr(texObj
->Target
));
1261 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1262 texObj
->TargetIndex
= targetIndex
;
1265 /* insert into hash table */
1266 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texObj
->Name
, texObj
);
1271 mtx_unlock(&ctx
->Shared
->Mutex
);
1277 /***********************************************************************/
1278 /** \name API functions */
1283 * Generate texture names.
1285 * \param n number of texture names to be generated.
1286 * \param textures an array in which will hold the generated texture names.
1288 * \sa glGenTextures(), glCreateTextures().
1290 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1291 * IDs which are stored in \p textures. Corresponding empty texture
1292 * objects are also generated.
1295 _mesa_GenTextures(GLsizei n
, GLuint
*textures
)
1297 GET_CURRENT_CONTEXT(ctx
);
1298 create_textures(ctx
, 0, n
, textures
, false);
1302 * Create texture objects.
1304 * \param target the texture target for each name to be generated.
1305 * \param n number of texture names to be generated.
1306 * \param textures an array in which will hold the generated texture names.
1308 * \sa glCreateTextures(), glGenTextures().
1310 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1311 * IDs which are stored in \p textures. Corresponding empty texture
1312 * objects are also generated.
1315 _mesa_CreateTextures(GLenum target
, GLsizei n
, GLuint
*textures
)
1318 GET_CURRENT_CONTEXT(ctx
);
1321 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1322 * glCreateTextures should do with invalid targets, which was probably an
1323 * oversight. This conforms to the spec for glBindTexture.
1325 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1326 if (targetIndex
< 0) {
1327 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCreateTextures(target)");
1331 create_textures(ctx
, target
, n
, textures
, true);
1335 * Check if the given texture object is bound to the current draw or
1336 * read framebuffer. If so, Unbind it.
1339 unbind_texobj_from_fbo(struct gl_context
*ctx
,
1340 struct gl_texture_object
*texObj
)
1342 bool progress
= false;
1344 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1345 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1348 * "If a texture object is deleted while its image is attached to one
1349 * or more attachment points in the currently bound framebuffer, then
1350 * it is as if FramebufferTexture* had been called, with a texture of
1351 * zero, for each attachment point to which this image was attached in
1352 * the currently bound framebuffer. In other words, this texture image
1353 * is first detached from all attachment points in the currently bound
1354 * framebuffer. Note that the texture image is specifically not
1355 * detached from any other framebuffer objects. Detaching the texture
1356 * image from any other framebuffer objects is the responsibility of
1359 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1360 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, texObj
);
1362 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1363 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1364 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, texObj
)
1369 /* Vertices are already flushed by _mesa_DeleteTextures */
1370 ctx
->NewState
|= _NEW_BUFFERS
;
1375 * Check if the given texture object is bound to any texture image units and
1376 * unbind it if so (revert to default textures).
1379 unbind_texobj_from_texunits(struct gl_context
*ctx
,
1380 struct gl_texture_object
*texObj
)
1382 const gl_texture_index index
= texObj
->TargetIndex
;
1385 if (texObj
->Target
== 0)
1388 for (u
= 0; u
< ctx
->Texture
.NumCurrentTexUsed
; u
++) {
1389 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
1391 if (texObj
== unit
->CurrentTex
[index
]) {
1392 /* Bind the default texture for this unit/target */
1393 _mesa_reference_texobj(&unit
->CurrentTex
[index
],
1394 ctx
->Shared
->DefaultTex
[index
]);
1395 unit
->_BoundTextures
&= ~(1 << index
);
1402 * Check if the given texture object is bound to any shader image unit
1403 * and unbind it if that's the case.
1406 unbind_texobj_from_image_units(struct gl_context
*ctx
,
1407 struct gl_texture_object
*texObj
)
1411 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; i
++) {
1412 struct gl_image_unit
*unit
= &ctx
->ImageUnits
[i
];
1414 if (texObj
== unit
->TexObj
)
1415 _mesa_reference_texobj(&unit
->TexObj
, NULL
);
1421 * Unbinds all textures bound to the given texture image unit.
1424 unbind_textures_from_unit(struct gl_context
*ctx
, GLuint unit
)
1426 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1428 while (texUnit
->_BoundTextures
) {
1429 const GLuint index
= ffs(texUnit
->_BoundTextures
) - 1;
1430 struct gl_texture_object
*texObj
= ctx
->Shared
->DefaultTex
[index
];
1432 _mesa_reference_texobj(&texUnit
->CurrentTex
[index
], texObj
);
1434 /* Pass BindTexture call to device driver */
1435 if (ctx
->Driver
.BindTexture
)
1436 ctx
->Driver
.BindTexture(ctx
, unit
, 0, texObj
);
1438 texUnit
->_BoundTextures
&= ~(1 << index
);
1439 ctx
->NewState
|= _NEW_TEXTURE
;
1445 * Delete named textures.
1447 * \param n number of textures to be deleted.
1448 * \param textures array of texture IDs to be deleted.
1450 * \sa glDeleteTextures().
1452 * If we're about to delete a texture that's currently bound to any
1453 * texture unit, unbind the texture first. Decrement the reference
1454 * count on the texture object and delete it if it's zero.
1455 * Recall that texture objects can be shared among several rendering
1459 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
1461 GET_CURRENT_CONTEXT(ctx
);
1464 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1465 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1468 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n < 0)");
1472 FLUSH_VERTICES(ctx
, 0); /* too complex */
1475 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n)");
1482 for (i
= 0; i
< n
; i
++) {
1483 if (textures
[i
] > 0) {
1484 struct gl_texture_object
*delObj
1485 = _mesa_lookup_texture(ctx
, textures
[i
]);
1488 _mesa_lock_texture(ctx
, delObj
);
1490 /* Check if texture is bound to any framebuffer objects.
1492 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1494 unbind_texobj_from_fbo(ctx
, delObj
);
1496 /* Check if this texture is currently bound to any texture units.
1499 unbind_texobj_from_texunits(ctx
, delObj
);
1501 /* Check if this texture is currently bound to any shader
1502 * image unit. If so, unbind it.
1503 * See section 3.9.X of GL_ARB_shader_image_load_store.
1505 unbind_texobj_from_image_units(ctx
, delObj
);
1507 _mesa_unlock_texture(ctx
, delObj
);
1509 ctx
->NewState
|= _NEW_TEXTURE
;
1511 /* The texture _name_ is now free for re-use.
1512 * Remove it from the hash table now.
1514 mtx_lock(&ctx
->Shared
->Mutex
);
1515 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
1516 mtx_unlock(&ctx
->Shared
->Mutex
);
1518 /* Unreference the texobj. If refcount hits zero, the texture
1521 _mesa_reference_texobj(&delObj
, NULL
);
1528 * This deletes a texObj without altering the hash table.
1531 _mesa_delete_nameless_texture(struct gl_context
*ctx
,
1532 struct gl_texture_object
*texObj
)
1537 FLUSH_VERTICES(ctx
, 0);
1539 _mesa_lock_texture(ctx
, texObj
);
1541 /* Check if texture is bound to any framebuffer objects.
1543 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1545 unbind_texobj_from_fbo(ctx
, texObj
);
1547 /* Check if this texture is currently bound to any texture units.
1550 unbind_texobj_from_texunits(ctx
, texObj
);
1552 /* Check if this texture is currently bound to any shader
1553 * image unit. If so, unbind it.
1554 * See section 3.9.X of GL_ARB_shader_image_load_store.
1556 unbind_texobj_from_image_units(ctx
, texObj
);
1558 _mesa_unlock_texture(ctx
, texObj
);
1560 ctx
->NewState
|= _NEW_TEXTURE
;
1562 /* Unreference the texobj. If refcount hits zero, the texture
1565 _mesa_reference_texobj(&texObj
, NULL
);
1570 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1571 * into the corresponding Mesa texture target index.
1572 * Note that proxy targets are not valid here.
1573 * \return TEXTURE_x_INDEX or -1 if target is invalid
1576 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1580 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1582 return TEXTURE_2D_INDEX
;
1584 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1585 case GL_TEXTURE_CUBE_MAP
:
1586 return ctx
->Extensions
.ARB_texture_cube_map
1587 ? TEXTURE_CUBE_INDEX
: -1;
1588 case GL_TEXTURE_RECTANGLE
:
1589 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1590 ? TEXTURE_RECT_INDEX
: -1;
1591 case GL_TEXTURE_1D_ARRAY
:
1592 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1593 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1594 case GL_TEXTURE_2D_ARRAY
:
1595 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1596 || _mesa_is_gles3(ctx
)
1597 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1598 case GL_TEXTURE_BUFFER
:
1599 return ctx
->API
== API_OPENGL_CORE
&&
1600 ctx
->Extensions
.ARB_texture_buffer_object
?
1601 TEXTURE_BUFFER_INDEX
: -1;
1602 case GL_TEXTURE_EXTERNAL_OES
:
1603 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1604 ? TEXTURE_EXTERNAL_INDEX
: -1;
1605 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1606 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_cube_map_array
1607 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1608 case GL_TEXTURE_2D_MULTISAMPLE
:
1609 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1610 ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1611 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1612 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1613 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1621 * Bind a named texture to a texturing target.
1623 * \param target texture target.
1624 * \param texName texture name.
1626 * \sa glBindTexture().
1628 * Determines the old texture object bound and returns immediately if rebinding
1629 * the same texture. Get the current texture which is either a default texture
1630 * if name is null, a named texture from the hash, or a new texture if the
1631 * given texture name is new. Increments its reference count, binds it, and
1632 * calls dd_function_table::BindTexture. Decrements the old texture reference
1633 * count and deletes it if it reaches zero.
1636 _mesa_BindTexture( GLenum target
, GLuint texName
)
1638 GET_CURRENT_CONTEXT(ctx
);
1639 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
1640 struct gl_texture_object
*newTexObj
= NULL
;
1643 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1644 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1645 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1647 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1648 if (targetIndex
< 0) {
1649 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1652 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1655 * Get pointer to new texture object (newTexObj)
1658 /* Use a default texture object */
1659 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1662 /* non-default texture object */
1663 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1665 /* error checking */
1666 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1667 /* The named texture object's target doesn't match the
1670 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1671 "glBindTexture(target mismatch)" );
1674 if (newTexObj
->Target
== 0) {
1675 finish_texture_init(ctx
, target
, newTexObj
);
1679 if (ctx
->API
== API_OPENGL_CORE
) {
1680 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1681 "glBindTexture(non-gen name)");
1685 /* if this is a new texture id, allocate a texture object now */
1686 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1688 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1692 /* and insert it into hash table */
1693 mtx_lock(&ctx
->Shared
->Mutex
);
1694 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1695 mtx_unlock(&ctx
->Shared
->Mutex
);
1697 newTexObj
->Target
= target
;
1698 newTexObj
->TargetIndex
= targetIndex
;
1701 assert(valid_texture_object(newTexObj
));
1703 /* Check if this texture is only used by this context and is already bound.
1704 * If so, just return.
1707 GLboolean early_out
;
1708 mtx_lock(&ctx
->Shared
->Mutex
);
1709 early_out
= ((ctx
->Shared
->RefCount
== 1)
1710 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1711 mtx_unlock(&ctx
->Shared
->Mutex
);
1717 /* flush before changing binding */
1718 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1720 /* Do the actual binding. The refcount on the previously bound
1721 * texture object will be decremented. It'll be deleted if the
1724 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1725 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1726 ctx
->Texture
.CurrentUnit
+ 1);
1727 assert(texUnit
->CurrentTex
[targetIndex
]);
1730 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1732 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1734 /* Pass BindTexture call to device driver */
1735 if (ctx
->Driver
.BindTexture
)
1736 ctx
->Driver
.BindTexture(ctx
, ctx
->Texture
.CurrentUnit
, target
, newTexObj
);
1740 * Do the actual binding to a numbered texture unit.
1741 * The refcount on the previously bound
1742 * texture object will be decremented. It'll be deleted if the
1746 _mesa_bind_texture_unit(struct gl_context
*ctx
,
1748 struct gl_texture_object
*texObj
)
1750 struct gl_texture_unit
*texUnit
;
1752 /* Get the texture unit (this is an array look-up) */
1753 texUnit
= _mesa_get_tex_unit_err(ctx
, unit
, "glBindTextureUnit");
1757 /* Check if this texture is only used by this context and is already bound.
1758 * If so, just return.
1762 mtx_lock(&ctx
->Shared
->Mutex
);
1763 early_out
= ((ctx
->Shared
->RefCount
== 1)
1764 && (texObj
== texUnit
->CurrentTex
[texObj
->TargetIndex
]));
1765 mtx_unlock(&ctx
->Shared
->Mutex
);
1771 /* flush before changing binding */
1772 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1774 _mesa_reference_texobj(&texUnit
->CurrentTex
[texObj
->TargetIndex
],
1776 assert(texUnit
->CurrentTex
[texObj
->TargetIndex
]);
1777 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1779 texUnit
->_BoundTextures
|= (1 << texObj
->TargetIndex
);
1782 /* Pass BindTexture call to device driver */
1783 if (ctx
->Driver
.BindTexture
) {
1784 ctx
->Driver
.BindTexture(ctx
, unit
, texObj
->Target
, texObj
);
1789 * Bind a named texture to the specified texture unit.
1791 * \param unit texture unit.
1792 * \param texture texture name.
1794 * \sa glBindTexture().
1796 * If the named texture is 0, this will reset each target for the specified
1797 * texture unit to its default texture.
1798 * If the named texture is not 0 or a recognized texture name, this throws
1799 * GL_INVALID_OPERATION.
1802 _mesa_BindTextureUnit(GLuint unit
, GLuint texture
)
1804 GET_CURRENT_CONTEXT(ctx
);
1805 struct gl_texture_object
*texObj
;
1807 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1808 _mesa_debug(ctx
, "glBindTextureUnit %s %d\n",
1809 _mesa_lookup_enum_by_nr(GL_TEXTURE0
+unit
), (GLint
) texture
);
1811 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1813 * "When texture is zero, each of the targets enumerated at the
1814 * beginning of this section is reset to its default texture for the
1815 * corresponding texture image unit."
1818 unbind_textures_from_unit(ctx
, unit
);
1822 /* Get the non-default texture object */
1823 texObj
= _mesa_lookup_texture(ctx
, texture
);
1825 /* Error checking */
1827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1828 "glBindTextureUnit(non-gen name)");
1831 if (texObj
->Target
== 0) {
1832 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTextureUnit(target)");
1835 assert(valid_texture_object(texObj
));
1837 _mesa_bind_texture_unit(ctx
, unit
, texObj
);
1842 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
1844 GET_CURRENT_CONTEXT(ctx
);
1847 /* The ARB_multi_bind spec says:
1849 * "An INVALID_OPERATION error is generated if <first> + <count>
1850 * is greater than the number of texture image units supported
1851 * by the implementation."
1853 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
1854 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1855 "glBindTextures(first=%u + count=%d > the value of "
1856 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
1857 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
1861 /* Flush before changing bindings */
1862 FLUSH_VERTICES(ctx
, 0);
1864 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1868 /* Note that the error semantics for multi-bind commands differ from
1869 * those of other GL commands.
1871 * The issues section in the ARB_multi_bind spec says:
1873 * "(11) Typically, OpenGL specifies that if an error is generated by
1874 * a command, that command has no effect. This is somewhat
1875 * unfortunate for multi-bind commands, because it would require
1876 * a first pass to scan the entire list of bound objects for
1877 * errors and then a second pass to actually perform the
1878 * bindings. Should we have different error semantics?
1880 * RESOLVED: Yes. In this specification, when the parameters for
1881 * one of the <count> binding points are invalid, that binding
1882 * point is not updated and an error will be generated. However,
1883 * other binding points in the same command will be updated if
1884 * their parameters are valid and no other error occurs."
1887 _mesa_begin_texture_lookups(ctx
);
1889 for (i
= 0; i
< count
; i
++) {
1890 if (textures
[i
] != 0) {
1891 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
1892 struct gl_texture_object
*current
= texUnit
->_Current
;
1893 struct gl_texture_object
*texObj
;
1895 if (current
&& current
->Name
== textures
[i
])
1898 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
1900 if (texObj
&& texObj
->Target
!= 0) {
1901 const gl_texture_index targetIndex
= texObj
->TargetIndex
;
1903 if (texUnit
->CurrentTex
[targetIndex
] != texObj
) {
1904 /* Do the actual binding. The refcount on the previously
1905 * bound texture object will be decremented. It will be
1906 * deleted if the count hits zero.
1908 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
],
1911 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1912 ctx
->NewState
|= _NEW_TEXTURE
;
1914 /* Pass the BindTexture call to the device driver */
1915 if (ctx
->Driver
.BindTexture
)
1916 ctx
->Driver
.BindTexture(ctx
, first
+ i
,
1917 texObj
->Target
, texObj
);
1920 /* The ARB_multi_bind spec says:
1922 * "An INVALID_OPERATION error is generated if any value
1923 * in <textures> is not zero or the name of an existing
1924 * texture object (per binding)."
1926 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1927 "glBindTextures(textures[%d]=%u is not zero "
1928 "or the name of an existing texture object)",
1932 unbind_textures_from_unit(ctx
, first
+ i
);
1936 _mesa_end_texture_lookups(ctx
);
1938 /* Unbind all textures in the range <first> through <first>+<count>-1 */
1939 for (i
= 0; i
< count
; i
++)
1940 unbind_textures_from_unit(ctx
, first
+ i
);
1946 * Set texture priorities.
1948 * \param n number of textures.
1949 * \param texName texture names.
1950 * \param priorities corresponding texture priorities.
1952 * \sa glPrioritizeTextures().
1954 * Looks up each texture in the hash, clamps the corresponding priority between
1955 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1958 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1959 const GLclampf
*priorities
)
1961 GET_CURRENT_CONTEXT(ctx
);
1964 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1965 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
1967 FLUSH_VERTICES(ctx
, 0);
1970 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1977 for (i
= 0; i
< n
; i
++) {
1978 if (texName
[i
] > 0) {
1979 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1981 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
1986 ctx
->NewState
|= _NEW_TEXTURE
;
1992 * See if textures are loaded in texture memory.
1994 * \param n number of textures to query.
1995 * \param texName array with the texture names.
1996 * \param residences array which will hold the residence status.
1998 * \return GL_TRUE if all textures are resident and
1999 * residences is left unchanged,
2001 * Note: we assume all textures are always resident
2003 GLboolean GLAPIENTRY
2004 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
2005 GLboolean
*residences
)
2007 GET_CURRENT_CONTEXT(ctx
);
2008 GLboolean allResident
= GL_TRUE
;
2010 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2012 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2013 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
2016 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
2020 if (!texName
|| !residences
)
2023 /* We only do error checking on the texture names */
2024 for (i
= 0; i
< n
; i
++) {
2025 struct gl_texture_object
*t
;
2026 if (texName
[i
] == 0) {
2027 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2030 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2032 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2042 * See if a name corresponds to a texture.
2044 * \param texture texture name.
2046 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
2049 * \sa glIsTexture().
2051 * Calls _mesa_HashLookup().
2053 GLboolean GLAPIENTRY
2054 _mesa_IsTexture( GLuint texture
)
2056 struct gl_texture_object
*t
;
2057 GET_CURRENT_CONTEXT(ctx
);
2058 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2060 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2061 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
2066 t
= _mesa_lookup_texture(ctx
, texture
);
2068 /* IsTexture is true only after object has been bound once. */
2069 return t
&& t
->Target
;
2074 * Simplest implementation of texture locking: grab the shared tex
2075 * mutex. Examine the shared context state timestamp and if there has
2076 * been a change, set the appropriate bits in ctx->NewState.
2078 * This is used to deal with synchronizing things when a texture object
2079 * is used/modified by different contexts (or threads) which are sharing
2082 * See also _mesa_lock/unlock_texture() in teximage.h
2085 _mesa_lock_context_textures( struct gl_context
*ctx
)
2087 mtx_lock(&ctx
->Shared
->TexMutex
);
2089 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
2090 ctx
->NewState
|= _NEW_TEXTURE
;
2091 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
2097 _mesa_unlock_context_textures( struct gl_context
*ctx
)
2099 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
2100 mtx_unlock(&ctx
->Shared
->TexMutex
);
2105 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
2106 GLint yoffset
, GLint zoffset
, GLsizei width
,
2107 GLsizei height
, GLsizei depth
)
2109 struct gl_texture_object
*t
;
2110 struct gl_texture_image
*image
;
2111 GET_CURRENT_CONTEXT(ctx
);
2113 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2114 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
2116 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
2117 "glInvalidateTexSubImage");
2119 /* The GL_ARB_invalidate_subdata spec says:
2121 * "...the specified subregion must be between -<b> and <dim>+<b> where
2122 * <dim> is the size of the dimension of the texture image, and <b> is
2123 * the size of the border of that texture image, otherwise
2124 * INVALID_VALUE is generated (border is not applied to dimensions that
2125 * don't exist in a given texture target)."
2127 image
= t
->Image
[0][level
];
2136 /* The GL_ARB_invalidate_subdata spec says:
2138 * "For texture targets that don't have certain dimensions, this
2139 * command treats those dimensions as having a size of 1. For
2140 * example, to invalidate a portion of a two-dimensional texture,
2141 * the application would use <zoffset> equal to zero and <depth>
2144 switch (t
->Target
) {
2145 case GL_TEXTURE_BUFFER
:
2154 xBorder
= image
->Border
;
2157 imageWidth
= image
->Width
;
2161 case GL_TEXTURE_1D_ARRAY
:
2162 xBorder
= image
->Border
;
2165 imageWidth
= image
->Width
;
2166 imageHeight
= image
->Height
;
2170 case GL_TEXTURE_CUBE_MAP
:
2171 case GL_TEXTURE_RECTANGLE
:
2172 case GL_TEXTURE_2D_MULTISAMPLE
:
2173 xBorder
= image
->Border
;
2174 yBorder
= image
->Border
;
2176 imageWidth
= image
->Width
;
2177 imageHeight
= image
->Height
;
2180 case GL_TEXTURE_2D_ARRAY
:
2181 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2182 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2183 xBorder
= image
->Border
;
2184 yBorder
= image
->Border
;
2186 imageWidth
= image
->Width
;
2187 imageHeight
= image
->Height
;
2188 imageDepth
= image
->Depth
;
2191 xBorder
= image
->Border
;
2192 yBorder
= image
->Border
;
2193 zBorder
= image
->Border
;
2194 imageWidth
= image
->Width
;
2195 imageHeight
= image
->Height
;
2196 imageDepth
= image
->Depth
;
2199 assert(!"Should not get here.");
2209 if (xoffset
< -xBorder
) {
2210 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
2214 if (xoffset
+ width
> imageWidth
+ xBorder
) {
2215 _mesa_error(ctx
, GL_INVALID_VALUE
,
2216 "glInvalidateSubTexImage(xoffset+width)");
2220 if (yoffset
< -yBorder
) {
2221 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
2225 if (yoffset
+ height
> imageHeight
+ yBorder
) {
2226 _mesa_error(ctx
, GL_INVALID_VALUE
,
2227 "glInvalidateSubTexImage(yoffset+height)");
2231 if (zoffset
< -zBorder
) {
2232 _mesa_error(ctx
, GL_INVALID_VALUE
,
2233 "glInvalidateSubTexImage(zoffset)");
2237 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
2238 _mesa_error(ctx
, GL_INVALID_VALUE
,
2239 "glInvalidateSubTexImage(zoffset+depth)");
2244 /* We don't actually do anything for this yet. Just return after
2245 * validating the parameters and generating the required errors.
2252 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
2254 GET_CURRENT_CONTEXT(ctx
);
2256 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2257 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
2259 invalidate_tex_image_error_check(ctx
, texture
, level
,
2260 "glInvalidateTexImage");
2262 /* We don't actually do anything for this yet. Just return after
2263 * validating the parameters and generating the required errors.