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
);
1320 if (!ctx
->Extensions
.ARB_direct_state_access
) {
1321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1322 "glCreateTextures(GL_ARB_direct_state_access "
1323 "is not supported)");
1328 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1329 * glCreateTextures should do with invalid targets, which was probably an
1330 * oversight. This conforms to the spec for glBindTexture.
1332 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1333 if (targetIndex
< 0) {
1334 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCreateTextures(target)");
1338 create_textures(ctx
, target
, n
, textures
, true);
1342 * Check if the given texture object is bound to the current draw or
1343 * read framebuffer. If so, Unbind it.
1346 unbind_texobj_from_fbo(struct gl_context
*ctx
,
1347 struct gl_texture_object
*texObj
)
1349 bool progress
= false;
1351 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1352 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1355 * "If a texture object is deleted while its image is attached to one
1356 * or more attachment points in the currently bound framebuffer, then
1357 * it is as if FramebufferTexture* had been called, with a texture of
1358 * zero, for each attachment point to which this image was attached in
1359 * the currently bound framebuffer. In other words, this texture image
1360 * is first detached from all attachment points in the currently bound
1361 * framebuffer. Note that the texture image is specifically not
1362 * detached from any other framebuffer objects. Detaching the texture
1363 * image from any other framebuffer objects is the responsibility of
1366 if (_mesa_is_user_fbo(ctx
->DrawBuffer
)) {
1367 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->DrawBuffer
, texObj
);
1369 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)
1370 && ctx
->ReadBuffer
!= ctx
->DrawBuffer
) {
1371 progress
= _mesa_detach_renderbuffer(ctx
, ctx
->ReadBuffer
, texObj
)
1376 /* Vertices are already flushed by _mesa_DeleteTextures */
1377 ctx
->NewState
|= _NEW_BUFFERS
;
1382 * Check if the given texture object is bound to any texture image units and
1383 * unbind it if so (revert to default textures).
1386 unbind_texobj_from_texunits(struct gl_context
*ctx
,
1387 struct gl_texture_object
*texObj
)
1389 const gl_texture_index index
= texObj
->TargetIndex
;
1392 if (texObj
->Target
== 0)
1395 for (u
= 0; u
< ctx
->Texture
.NumCurrentTexUsed
; u
++) {
1396 struct gl_texture_unit
*unit
= &ctx
->Texture
.Unit
[u
];
1398 if (texObj
== unit
->CurrentTex
[index
]) {
1399 /* Bind the default texture for this unit/target */
1400 _mesa_reference_texobj(&unit
->CurrentTex
[index
],
1401 ctx
->Shared
->DefaultTex
[index
]);
1402 unit
->_BoundTextures
&= ~(1 << index
);
1409 * Check if the given texture object is bound to any shader image unit
1410 * and unbind it if that's the case.
1413 unbind_texobj_from_image_units(struct gl_context
*ctx
,
1414 struct gl_texture_object
*texObj
)
1418 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; i
++) {
1419 struct gl_image_unit
*unit
= &ctx
->ImageUnits
[i
];
1421 if (texObj
== unit
->TexObj
)
1422 _mesa_reference_texobj(&unit
->TexObj
, NULL
);
1428 * Unbinds all textures bound to the given texture image unit.
1431 unbind_textures_from_unit(struct gl_context
*ctx
, GLuint unit
)
1433 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1435 while (texUnit
->_BoundTextures
) {
1436 const GLuint index
= ffs(texUnit
->_BoundTextures
) - 1;
1437 struct gl_texture_object
*texObj
= ctx
->Shared
->DefaultTex
[index
];
1439 _mesa_reference_texobj(&texUnit
->CurrentTex
[index
], texObj
);
1441 /* Pass BindTexture call to device driver */
1442 if (ctx
->Driver
.BindTexture
)
1443 ctx
->Driver
.BindTexture(ctx
, unit
, 0, texObj
);
1445 texUnit
->_BoundTextures
&= ~(1 << index
);
1446 ctx
->NewState
|= _NEW_TEXTURE
;
1452 * Delete named textures.
1454 * \param n number of textures to be deleted.
1455 * \param textures array of texture IDs to be deleted.
1457 * \sa glDeleteTextures().
1459 * If we're about to delete a texture that's currently bound to any
1460 * texture unit, unbind the texture first. Decrement the reference
1461 * count on the texture object and delete it if it's zero.
1462 * Recall that texture objects can be shared among several rendering
1466 _mesa_DeleteTextures( GLsizei n
, const GLuint
*textures
)
1468 GET_CURRENT_CONTEXT(ctx
);
1471 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1472 _mesa_debug(ctx
, "glDeleteTextures %d\n", n
);
1475 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n < 0)");
1479 FLUSH_VERTICES(ctx
, 0); /* too complex */
1482 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteTextures(n)");
1489 for (i
= 0; i
< n
; i
++) {
1490 if (textures
[i
] > 0) {
1491 struct gl_texture_object
*delObj
1492 = _mesa_lookup_texture(ctx
, textures
[i
]);
1495 _mesa_lock_texture(ctx
, delObj
);
1497 /* Check if texture is bound to any framebuffer objects.
1499 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1501 unbind_texobj_from_fbo(ctx
, delObj
);
1503 /* Check if this texture is currently bound to any texture units.
1506 unbind_texobj_from_texunits(ctx
, delObj
);
1508 /* Check if this texture is currently bound to any shader
1509 * image unit. If so, unbind it.
1510 * See section 3.9.X of GL_ARB_shader_image_load_store.
1512 unbind_texobj_from_image_units(ctx
, delObj
);
1514 _mesa_unlock_texture(ctx
, delObj
);
1516 ctx
->NewState
|= _NEW_TEXTURE
;
1518 /* The texture _name_ is now free for re-use.
1519 * Remove it from the hash table now.
1521 mtx_lock(&ctx
->Shared
->Mutex
);
1522 _mesa_HashRemove(ctx
->Shared
->TexObjects
, delObj
->Name
);
1523 mtx_unlock(&ctx
->Shared
->Mutex
);
1525 /* Unreference the texobj. If refcount hits zero, the texture
1528 _mesa_reference_texobj(&delObj
, NULL
);
1535 * This deletes a texObj without altering the hash table.
1538 _mesa_delete_nameless_texture(struct gl_context
*ctx
,
1539 struct gl_texture_object
*texObj
)
1544 FLUSH_VERTICES(ctx
, 0);
1546 _mesa_lock_texture(ctx
, texObj
);
1548 /* Check if texture is bound to any framebuffer objects.
1550 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1552 unbind_texobj_from_fbo(ctx
, texObj
);
1554 /* Check if this texture is currently bound to any texture units.
1557 unbind_texobj_from_texunits(ctx
, texObj
);
1559 /* Check if this texture is currently bound to any shader
1560 * image unit. If so, unbind it.
1561 * See section 3.9.X of GL_ARB_shader_image_load_store.
1563 unbind_texobj_from_image_units(ctx
, texObj
);
1565 _mesa_unlock_texture(ctx
, texObj
);
1567 ctx
->NewState
|= _NEW_TEXTURE
;
1569 /* Unreference the texobj. If refcount hits zero, the texture
1572 _mesa_reference_texobj(&texObj
, NULL
);
1577 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1578 * into the corresponding Mesa texture target index.
1579 * Note that proxy targets are not valid here.
1580 * \return TEXTURE_x_INDEX or -1 if target is invalid
1583 _mesa_tex_target_to_index(const struct gl_context
*ctx
, GLenum target
)
1587 return _mesa_is_desktop_gl(ctx
) ? TEXTURE_1D_INDEX
: -1;
1589 return TEXTURE_2D_INDEX
;
1591 return ctx
->API
!= API_OPENGLES
? TEXTURE_3D_INDEX
: -1;
1592 case GL_TEXTURE_CUBE_MAP
:
1593 return ctx
->Extensions
.ARB_texture_cube_map
1594 ? TEXTURE_CUBE_INDEX
: -1;
1595 case GL_TEXTURE_RECTANGLE
:
1596 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.NV_texture_rectangle
1597 ? TEXTURE_RECT_INDEX
: -1;
1598 case GL_TEXTURE_1D_ARRAY
:
1599 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
1600 ? TEXTURE_1D_ARRAY_INDEX
: -1;
1601 case GL_TEXTURE_2D_ARRAY
:
1602 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1603 || _mesa_is_gles3(ctx
)
1604 ? TEXTURE_2D_ARRAY_INDEX
: -1;
1605 case GL_TEXTURE_BUFFER
:
1606 return ctx
->API
== API_OPENGL_CORE
&&
1607 ctx
->Extensions
.ARB_texture_buffer_object
?
1608 TEXTURE_BUFFER_INDEX
: -1;
1609 case GL_TEXTURE_EXTERNAL_OES
:
1610 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
1611 ? TEXTURE_EXTERNAL_INDEX
: -1;
1612 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1613 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_cube_map_array
1614 ? TEXTURE_CUBE_ARRAY_INDEX
: -1;
1615 case GL_TEXTURE_2D_MULTISAMPLE
:
1616 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1617 ? TEXTURE_2D_MULTISAMPLE_INDEX
: -1;
1618 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1619 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_texture_multisample
1620 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: -1;
1628 * Bind a named texture to a texturing target.
1630 * \param target texture target.
1631 * \param texName texture name.
1633 * \sa glBindTexture().
1635 * Determines the old texture object bound and returns immediately if rebinding
1636 * the same texture. Get the current texture which is either a default texture
1637 * if name is null, a named texture from the hash, or a new texture if the
1638 * given texture name is new. Increments its reference count, binds it, and
1639 * calls dd_function_table::BindTexture. Decrements the old texture reference
1640 * count and deletes it if it reaches zero.
1643 _mesa_BindTexture( GLenum target
, GLuint texName
)
1645 GET_CURRENT_CONTEXT(ctx
);
1646 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
1647 struct gl_texture_object
*newTexObj
= NULL
;
1650 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1651 _mesa_debug(ctx
, "glBindTexture %s %d\n",
1652 _mesa_lookup_enum_by_nr(target
), (GLint
) texName
);
1654 targetIndex
= _mesa_tex_target_to_index(ctx
, target
);
1655 if (targetIndex
< 0) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTexture(target)");
1659 assert(targetIndex
< NUM_TEXTURE_TARGETS
);
1662 * Get pointer to new texture object (newTexObj)
1665 /* Use a default texture object */
1666 newTexObj
= ctx
->Shared
->DefaultTex
[targetIndex
];
1669 /* non-default texture object */
1670 newTexObj
= _mesa_lookup_texture(ctx
, texName
);
1672 /* error checking */
1673 if (newTexObj
->Target
!= 0 && newTexObj
->Target
!= target
) {
1674 /* The named texture object's target doesn't match the
1677 _mesa_error( ctx
, GL_INVALID_OPERATION
,
1678 "glBindTexture(target mismatch)" );
1681 if (newTexObj
->Target
== 0) {
1682 finish_texture_init(ctx
, target
, newTexObj
);
1686 if (ctx
->API
== API_OPENGL_CORE
) {
1687 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1688 "glBindTexture(non-gen name)");
1692 /* if this is a new texture id, allocate a texture object now */
1693 newTexObj
= ctx
->Driver
.NewTextureObject(ctx
, texName
, target
);
1695 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindTexture");
1699 /* and insert it into hash table */
1700 mtx_lock(&ctx
->Shared
->Mutex
);
1701 _mesa_HashInsert(ctx
->Shared
->TexObjects
, texName
, newTexObj
);
1702 mtx_unlock(&ctx
->Shared
->Mutex
);
1704 newTexObj
->Target
= target
;
1705 newTexObj
->TargetIndex
= targetIndex
;
1708 assert(valid_texture_object(newTexObj
));
1710 /* Check if this texture is only used by this context and is already bound.
1711 * If so, just return.
1714 GLboolean early_out
;
1715 mtx_lock(&ctx
->Shared
->Mutex
);
1716 early_out
= ((ctx
->Shared
->RefCount
== 1)
1717 && (newTexObj
== texUnit
->CurrentTex
[targetIndex
]));
1718 mtx_unlock(&ctx
->Shared
->Mutex
);
1724 /* flush before changing binding */
1725 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1727 /* Do the actual binding. The refcount on the previously bound
1728 * texture object will be decremented. It'll be deleted if the
1731 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
], newTexObj
);
1732 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1733 ctx
->Texture
.CurrentUnit
+ 1);
1734 assert(texUnit
->CurrentTex
[targetIndex
]);
1737 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1739 texUnit
->_BoundTextures
&= ~(1 << targetIndex
);
1741 /* Pass BindTexture call to device driver */
1742 if (ctx
->Driver
.BindTexture
)
1743 ctx
->Driver
.BindTexture(ctx
, ctx
->Texture
.CurrentUnit
, target
, newTexObj
);
1747 * Do the actual binding to a numbered texture unit.
1748 * The refcount on the previously bound
1749 * texture object will be decremented. It'll be deleted if the
1753 _mesa_bind_texture_unit(struct gl_context
*ctx
,
1755 struct gl_texture_object
*texObj
)
1757 struct gl_texture_unit
*texUnit
;
1759 /* Get the texture unit (this is an array look-up) */
1760 texUnit
= _mesa_get_tex_unit_err(ctx
, unit
, "glBindTextureUnit");
1764 /* Check if this texture is only used by this context and is already bound.
1765 * If so, just return.
1769 mtx_lock(&ctx
->Shared
->Mutex
);
1770 early_out
= ((ctx
->Shared
->RefCount
== 1)
1771 && (texObj
== texUnit
->CurrentTex
[texObj
->TargetIndex
]));
1772 mtx_unlock(&ctx
->Shared
->Mutex
);
1778 /* flush before changing binding */
1779 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
1781 _mesa_reference_texobj(&texUnit
->CurrentTex
[texObj
->TargetIndex
],
1783 assert(texUnit
->CurrentTex
[texObj
->TargetIndex
]);
1784 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1786 texUnit
->_BoundTextures
|= (1 << texObj
->TargetIndex
);
1789 /* Pass BindTexture call to device driver */
1790 if (ctx
->Driver
.BindTexture
) {
1791 ctx
->Driver
.BindTexture(ctx
, unit
, texObj
->Target
, texObj
);
1796 * Bind a named texture to the specified texture unit.
1798 * \param unit texture unit.
1799 * \param texture texture name.
1801 * \sa glBindTexture().
1803 * If the named texture is 0, this will reset each target for the specified
1804 * texture unit to its default texture.
1805 * If the named texture is not 0 or a recognized texture name, this throws
1806 * GL_INVALID_OPERATION.
1809 _mesa_BindTextureUnit(GLuint unit
, GLuint texture
)
1811 GET_CURRENT_CONTEXT(ctx
);
1812 struct gl_texture_object
*texObj
;
1814 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1815 _mesa_debug(ctx
, "glBindTextureUnit %s %d\n",
1816 _mesa_lookup_enum_by_nr(GL_TEXTURE0
+unit
), (GLint
) texture
);
1818 if (!ctx
->Extensions
.ARB_direct_state_access
) {
1819 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1820 "glBindTextureUnit(GL_ARB_direct_state_access "
1821 "is not supported)");
1825 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1827 * "When texture is zero, each of the targets enumerated at the
1828 * beginning of this section is reset to its default texture for the
1829 * corresponding texture image unit."
1832 unbind_textures_from_unit(ctx
, unit
);
1836 /* Get the non-default texture object */
1837 texObj
= _mesa_lookup_texture(ctx
, texture
);
1839 /* Error checking */
1841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1842 "glBindTextureUnit(non-gen name)");
1845 if (texObj
->Target
== 0) {
1846 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindTextureUnit(target)");
1849 assert(valid_texture_object(texObj
));
1851 _mesa_bind_texture_unit(ctx
, unit
, texObj
);
1856 _mesa_BindTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
1858 GET_CURRENT_CONTEXT(ctx
);
1861 /* The ARB_multi_bind spec says:
1863 * "An INVALID_OPERATION error is generated if <first> + <count>
1864 * is greater than the number of texture image units supported
1865 * by the implementation."
1867 if (first
+ count
> ctx
->Const
.MaxCombinedTextureImageUnits
) {
1868 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1869 "glBindTextures(first=%u + count=%d > the value of "
1870 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
1871 first
, count
, ctx
->Const
.MaxCombinedTextureImageUnits
);
1875 /* Flush before changing bindings */
1876 FLUSH_VERTICES(ctx
, 0);
1878 ctx
->Texture
.NumCurrentTexUsed
= MAX2(ctx
->Texture
.NumCurrentTexUsed
,
1882 /* Note that the error semantics for multi-bind commands differ from
1883 * those of other GL commands.
1885 * The issues section in the ARB_multi_bind spec says:
1887 * "(11) Typically, OpenGL specifies that if an error is generated by
1888 * a command, that command has no effect. This is somewhat
1889 * unfortunate for multi-bind commands, because it would require
1890 * a first pass to scan the entire list of bound objects for
1891 * errors and then a second pass to actually perform the
1892 * bindings. Should we have different error semantics?
1894 * RESOLVED: Yes. In this specification, when the parameters for
1895 * one of the <count> binding points are invalid, that binding
1896 * point is not updated and an error will be generated. However,
1897 * other binding points in the same command will be updated if
1898 * their parameters are valid and no other error occurs."
1901 _mesa_begin_texture_lookups(ctx
);
1903 for (i
= 0; i
< count
; i
++) {
1904 if (textures
[i
] != 0) {
1905 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[first
+ i
];
1906 struct gl_texture_object
*current
= texUnit
->_Current
;
1907 struct gl_texture_object
*texObj
;
1909 if (current
&& current
->Name
== textures
[i
])
1912 texObj
= _mesa_lookup_texture_locked(ctx
, textures
[i
]);
1914 if (texObj
&& texObj
->Target
!= 0) {
1915 const gl_texture_index targetIndex
= texObj
->TargetIndex
;
1917 if (texUnit
->CurrentTex
[targetIndex
] != texObj
) {
1918 /* Do the actual binding. The refcount on the previously
1919 * bound texture object will be decremented. It will be
1920 * deleted if the count hits zero.
1922 _mesa_reference_texobj(&texUnit
->CurrentTex
[targetIndex
],
1925 texUnit
->_BoundTextures
|= (1 << targetIndex
);
1926 ctx
->NewState
|= _NEW_TEXTURE
;
1928 /* Pass the BindTexture call to the device driver */
1929 if (ctx
->Driver
.BindTexture
)
1930 ctx
->Driver
.BindTexture(ctx
, first
+ i
,
1931 texObj
->Target
, texObj
);
1934 /* The ARB_multi_bind spec says:
1936 * "An INVALID_OPERATION error is generated if any value
1937 * in <textures> is not zero or the name of an existing
1938 * texture object (per binding)."
1940 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1941 "glBindTextures(textures[%d]=%u is not zero "
1942 "or the name of an existing texture object)",
1946 unbind_textures_from_unit(ctx
, first
+ i
);
1950 _mesa_end_texture_lookups(ctx
);
1952 /* Unbind all textures in the range <first> through <first>+<count>-1 */
1953 for (i
= 0; i
< count
; i
++)
1954 unbind_textures_from_unit(ctx
, first
+ i
);
1960 * Set texture priorities.
1962 * \param n number of textures.
1963 * \param texName texture names.
1964 * \param priorities corresponding texture priorities.
1966 * \sa glPrioritizeTextures().
1968 * Looks up each texture in the hash, clamps the corresponding priority between
1969 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1972 _mesa_PrioritizeTextures( GLsizei n
, const GLuint
*texName
,
1973 const GLclampf
*priorities
)
1975 GET_CURRENT_CONTEXT(ctx
);
1978 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1979 _mesa_debug(ctx
, "glPrioritizeTextures %d\n", n
);
1981 FLUSH_VERTICES(ctx
, 0);
1984 _mesa_error( ctx
, GL_INVALID_VALUE
, "glPrioritizeTextures" );
1991 for (i
= 0; i
< n
; i
++) {
1992 if (texName
[i
] > 0) {
1993 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texName
[i
]);
1995 t
->Priority
= CLAMP( priorities
[i
], 0.0F
, 1.0F
);
2000 ctx
->NewState
|= _NEW_TEXTURE
;
2006 * See if textures are loaded in texture memory.
2008 * \param n number of textures to query.
2009 * \param texName array with the texture names.
2010 * \param residences array which will hold the residence status.
2012 * \return GL_TRUE if all textures are resident and
2013 * residences is left unchanged,
2015 * Note: we assume all textures are always resident
2017 GLboolean GLAPIENTRY
2018 _mesa_AreTexturesResident(GLsizei n
, const GLuint
*texName
,
2019 GLboolean
*residences
)
2021 GET_CURRENT_CONTEXT(ctx
);
2022 GLboolean allResident
= GL_TRUE
;
2024 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2026 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2027 _mesa_debug(ctx
, "glAreTexturesResident %d\n", n
);
2030 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident(n)");
2034 if (!texName
|| !residences
)
2037 /* We only do error checking on the texture names */
2038 for (i
= 0; i
< n
; i
++) {
2039 struct gl_texture_object
*t
;
2040 if (texName
[i
] == 0) {
2041 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2044 t
= _mesa_lookup_texture(ctx
, texName
[i
]);
2046 _mesa_error(ctx
, GL_INVALID_VALUE
, "glAreTexturesResident");
2056 * See if a name corresponds to a texture.
2058 * \param texture texture name.
2060 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
2063 * \sa glIsTexture().
2065 * Calls _mesa_HashLookup().
2067 GLboolean GLAPIENTRY
2068 _mesa_IsTexture( GLuint texture
)
2070 struct gl_texture_object
*t
;
2071 GET_CURRENT_CONTEXT(ctx
);
2072 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2074 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2075 _mesa_debug(ctx
, "glIsTexture %d\n", texture
);
2080 t
= _mesa_lookup_texture(ctx
, texture
);
2082 /* IsTexture is true only after object has been bound once. */
2083 return t
&& t
->Target
;
2088 * Simplest implementation of texture locking: grab the shared tex
2089 * mutex. Examine the shared context state timestamp and if there has
2090 * been a change, set the appropriate bits in ctx->NewState.
2092 * This is used to deal with synchronizing things when a texture object
2093 * is used/modified by different contexts (or threads) which are sharing
2096 * See also _mesa_lock/unlock_texture() in teximage.h
2099 _mesa_lock_context_textures( struct gl_context
*ctx
)
2101 mtx_lock(&ctx
->Shared
->TexMutex
);
2103 if (ctx
->Shared
->TextureStateStamp
!= ctx
->TextureStateTimestamp
) {
2104 ctx
->NewState
|= _NEW_TEXTURE
;
2105 ctx
->TextureStateTimestamp
= ctx
->Shared
->TextureStateStamp
;
2111 _mesa_unlock_context_textures( struct gl_context
*ctx
)
2113 assert(ctx
->Shared
->TextureStateStamp
== ctx
->TextureStateTimestamp
);
2114 mtx_unlock(&ctx
->Shared
->TexMutex
);
2119 _mesa_InvalidateTexSubImage(GLuint texture
, GLint level
, GLint xoffset
,
2120 GLint yoffset
, GLint zoffset
, GLsizei width
,
2121 GLsizei height
, GLsizei depth
)
2123 struct gl_texture_object
*t
;
2124 struct gl_texture_image
*image
;
2125 GET_CURRENT_CONTEXT(ctx
);
2127 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2128 _mesa_debug(ctx
, "glInvalidateTexSubImage %d\n", texture
);
2130 t
= invalidate_tex_image_error_check(ctx
, texture
, level
,
2131 "glInvalidateTexSubImage");
2133 /* The GL_ARB_invalidate_subdata spec says:
2135 * "...the specified subregion must be between -<b> and <dim>+<b> where
2136 * <dim> is the size of the dimension of the texture image, and <b> is
2137 * the size of the border of that texture image, otherwise
2138 * INVALID_VALUE is generated (border is not applied to dimensions that
2139 * don't exist in a given texture target)."
2141 image
= t
->Image
[0][level
];
2150 /* The GL_ARB_invalidate_subdata spec says:
2152 * "For texture targets that don't have certain dimensions, this
2153 * command treats those dimensions as having a size of 1. For
2154 * example, to invalidate a portion of a two-dimensional texture,
2155 * the application would use <zoffset> equal to zero and <depth>
2158 switch (t
->Target
) {
2159 case GL_TEXTURE_BUFFER
:
2168 xBorder
= image
->Border
;
2171 imageWidth
= image
->Width
;
2175 case GL_TEXTURE_1D_ARRAY
:
2176 xBorder
= image
->Border
;
2179 imageWidth
= image
->Width
;
2180 imageHeight
= image
->Height
;
2184 case GL_TEXTURE_CUBE_MAP
:
2185 case GL_TEXTURE_RECTANGLE
:
2186 case GL_TEXTURE_2D_MULTISAMPLE
:
2187 xBorder
= image
->Border
;
2188 yBorder
= image
->Border
;
2190 imageWidth
= image
->Width
;
2191 imageHeight
= image
->Height
;
2194 case GL_TEXTURE_2D_ARRAY
:
2195 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2196 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2197 xBorder
= image
->Border
;
2198 yBorder
= image
->Border
;
2200 imageWidth
= image
->Width
;
2201 imageHeight
= image
->Height
;
2202 imageDepth
= image
->Depth
;
2205 xBorder
= image
->Border
;
2206 yBorder
= image
->Border
;
2207 zBorder
= image
->Border
;
2208 imageWidth
= image
->Width
;
2209 imageHeight
= image
->Height
;
2210 imageDepth
= image
->Depth
;
2213 assert(!"Should not get here.");
2223 if (xoffset
< -xBorder
) {
2224 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(xoffset)");
2228 if (xoffset
+ width
> imageWidth
+ xBorder
) {
2229 _mesa_error(ctx
, GL_INVALID_VALUE
,
2230 "glInvalidateSubTexImage(xoffset+width)");
2234 if (yoffset
< -yBorder
) {
2235 _mesa_error(ctx
, GL_INVALID_VALUE
, "glInvalidateSubTexImage(yoffset)");
2239 if (yoffset
+ height
> imageHeight
+ yBorder
) {
2240 _mesa_error(ctx
, GL_INVALID_VALUE
,
2241 "glInvalidateSubTexImage(yoffset+height)");
2245 if (zoffset
< -zBorder
) {
2246 _mesa_error(ctx
, GL_INVALID_VALUE
,
2247 "glInvalidateSubTexImage(zoffset)");
2251 if (zoffset
+ depth
> imageDepth
+ zBorder
) {
2252 _mesa_error(ctx
, GL_INVALID_VALUE
,
2253 "glInvalidateSubTexImage(zoffset+depth)");
2258 /* We don't actually do anything for this yet. Just return after
2259 * validating the parameters and generating the required errors.
2266 _mesa_InvalidateTexImage(GLuint texture
, GLint level
)
2268 GET_CURRENT_CONTEXT(ctx
);
2270 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2271 _mesa_debug(ctx
, "glInvalidateTexImage(%d, %d)\n", texture
, level
);
2273 invalidate_tex_image_error_check(ctx
, texture
, level
,
2274 "glInvalidateTexImage");
2276 /* We don't actually do anything for this yet. Just return after
2277 * validating the parameters and generating the required errors.