416b7df20c92cbf121279c7ccce59cf2d5199291
[mesa.git] / src / mesa / main / texobj.c
1 /**
2 * \file texobj.c
3 * Texture object management.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
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.
28 */
29
30
31 #include "bufferobj.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "fbobject.h"
36 #include "formats.h"
37 #include "hash.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "teximage.h"
41 #include "texobj.h"
42 #include "texstate.h"
43 #include "mtypes.h"
44 #include "program/prog_instruction.h"
45
46
47
48 /**********************************************************************/
49 /** \name Internal functions */
50 /*@{*/
51
52
53 /**
54 * Return the gl_texture_object for a given ID.
55 */
56 struct gl_texture_object *
57 _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
58 {
59 return (struct gl_texture_object *)
60 _mesa_HashLookup(ctx->Shared->TexObjects, id);
61 }
62
63 /**
64 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
65 * is not in the hash table. After calling _mesa_error, it returns NULL.
66 */
67 struct gl_texture_object *
68 _mesa_lookup_texture_err(struct gl_context *ctx, GLuint id, const char* func)
69 {
70 struct gl_texture_object *texObj;
71
72 texObj = _mesa_lookup_texture(ctx, id); /* Returns NULL if not found. */
73
74 if (!texObj)
75 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture)", func);
76
77 return texObj;
78 }
79
80 void
81 _mesa_begin_texture_lookups(struct gl_context *ctx)
82 {
83 _mesa_HashLockMutex(ctx->Shared->TexObjects);
84 }
85
86
87 void
88 _mesa_end_texture_lookups(struct gl_context *ctx)
89 {
90 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
91 }
92
93
94 struct gl_texture_object *
95 _mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id)
96 {
97 return (struct gl_texture_object *)
98 _mesa_HashLookupLocked(ctx->Shared->TexObjects, id);
99 }
100
101 /**
102 * Return a pointer to the current texture object for the given target
103 * on the current texture unit.
104 * Note: all <target> error checking should have been done by this point.
105 */
106 struct gl_texture_object *
107 _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
108 {
109 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
110 const GLboolean arrayTex = ctx->Extensions.EXT_texture_array;
111
112 switch (target) {
113 case GL_TEXTURE_1D:
114 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
115 case GL_PROXY_TEXTURE_1D:
116 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
117 case GL_TEXTURE_2D:
118 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
119 case GL_PROXY_TEXTURE_2D:
120 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
121 case GL_TEXTURE_3D:
122 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
123 case GL_PROXY_TEXTURE_3D:
124 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
125 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
126 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
127 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
128 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
129 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
130 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
131 case GL_TEXTURE_CUBE_MAP_ARB:
132 return ctx->Extensions.ARB_texture_cube_map
133 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
134 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
135 return ctx->Extensions.ARB_texture_cube_map
136 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
137 case GL_TEXTURE_CUBE_MAP_ARRAY:
138 return ctx->Extensions.ARB_texture_cube_map_array
139 ? texUnit->CurrentTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
140 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
141 return ctx->Extensions.ARB_texture_cube_map_array
142 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
143 case GL_TEXTURE_RECTANGLE_NV:
144 return ctx->Extensions.NV_texture_rectangle
145 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
146 case GL_PROXY_TEXTURE_RECTANGLE_NV:
147 return ctx->Extensions.NV_texture_rectangle
148 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
149 case GL_TEXTURE_1D_ARRAY_EXT:
150 return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
151 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
152 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
153 case GL_TEXTURE_2D_ARRAY_EXT:
154 return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
155 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
156 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
157 case GL_TEXTURE_BUFFER:
158 return ctx->API == API_OPENGL_CORE &&
159 ctx->Extensions.ARB_texture_buffer_object ?
160 texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
161 case GL_TEXTURE_EXTERNAL_OES:
162 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
163 ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
164 case GL_TEXTURE_2D_MULTISAMPLE:
165 return ctx->Extensions.ARB_texture_multisample
166 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
167 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
168 return ctx->Extensions.ARB_texture_multisample
169 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
170 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
171 return ctx->Extensions.ARB_texture_multisample
172 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
173 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
174 return ctx->Extensions.ARB_texture_multisample
175 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
176 default:
177 _mesa_problem(NULL, "bad target in _mesa_get_current_tex_object()");
178 return NULL;
179 }
180 }
181
182
183 /**
184 * Allocate and initialize a new texture object. But don't put it into the
185 * texture object hash table.
186 *
187 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
188 * driver.
189 *
190 * \param shared the shared GL state structure to contain the texture object
191 * \param name integer name for the texture object
192 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
193 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
194 * of GenTextures()
195 *
196 * \return pointer to new texture object.
197 */
198 struct gl_texture_object *
199 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target )
200 {
201 struct gl_texture_object *obj;
202 (void) ctx;
203 obj = MALLOC_STRUCT(gl_texture_object);
204 _mesa_initialize_texture_object(ctx, obj, name, target);
205 return obj;
206 }
207
208
209 /**
210 * Initialize a new texture object to default values.
211 * \param obj the texture object
212 * \param name the texture name
213 * \param target the texture target
214 */
215 void
216 _mesa_initialize_texture_object( struct gl_context *ctx,
217 struct gl_texture_object *obj,
218 GLuint name, GLenum target )
219 {
220 ASSERT(target == 0 ||
221 target == GL_TEXTURE_1D ||
222 target == GL_TEXTURE_2D ||
223 target == GL_TEXTURE_3D ||
224 target == GL_TEXTURE_CUBE_MAP_ARB ||
225 target == GL_TEXTURE_RECTANGLE_NV ||
226 target == GL_TEXTURE_1D_ARRAY_EXT ||
227 target == GL_TEXTURE_2D_ARRAY_EXT ||
228 target == GL_TEXTURE_EXTERNAL_OES ||
229 target == GL_TEXTURE_CUBE_MAP_ARRAY ||
230 target == GL_TEXTURE_BUFFER ||
231 target == GL_TEXTURE_2D_MULTISAMPLE ||
232 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
233
234 memset(obj, 0, sizeof(*obj));
235 /* init the non-zero fields */
236 mtx_init(&obj->Mutex, mtx_plain);
237 obj->RefCount = 1;
238 obj->Name = name;
239 obj->Target = target;
240 obj->Priority = 1.0F;
241 obj->BaseLevel = 0;
242 obj->MaxLevel = 1000;
243
244 /* must be one; no support for (YUV) planes in separate buffers */
245 obj->RequiredTextureImageUnits = 1;
246
247 /* sampler state */
248 if (target == GL_TEXTURE_RECTANGLE_NV ||
249 target == GL_TEXTURE_EXTERNAL_OES) {
250 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
251 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
252 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
253 obj->Sampler.MinFilter = GL_LINEAR;
254 }
255 else {
256 obj->Sampler.WrapS = GL_REPEAT;
257 obj->Sampler.WrapT = GL_REPEAT;
258 obj->Sampler.WrapR = GL_REPEAT;
259 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
260 }
261 obj->Sampler.MagFilter = GL_LINEAR;
262 obj->Sampler.MinLod = -1000.0;
263 obj->Sampler.MaxLod = 1000.0;
264 obj->Sampler.LodBias = 0.0;
265 obj->Sampler.MaxAnisotropy = 1.0;
266 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */
267 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */
268 obj->DepthMode = ctx->API == API_OPENGL_CORE ? GL_RED : GL_LUMINANCE;
269 obj->StencilSampling = false;
270 obj->Sampler.CubeMapSeamless = GL_FALSE;
271 obj->Swizzle[0] = GL_RED;
272 obj->Swizzle[1] = GL_GREEN;
273 obj->Swizzle[2] = GL_BLUE;
274 obj->Swizzle[3] = GL_ALPHA;
275 obj->_Swizzle = SWIZZLE_NOOP;
276 obj->Sampler.sRGBDecode = GL_DECODE_EXT;
277 obj->BufferObjectFormat = GL_R8;
278 obj->_BufferObjectFormat = MESA_FORMAT_R_UNORM8;
279 obj->ImageFormatCompatibilityType = GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE;
280 }
281
282
283 /**
284 * Some texture initialization can't be finished until we know which
285 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
286 */
287 static void
288 finish_texture_init(struct gl_context *ctx, GLenum target,
289 struct gl_texture_object *obj)
290 {
291 GLenum filter = GL_LINEAR;
292 assert(obj->Target == 0);
293
294 switch (target) {
295 case GL_TEXTURE_2D_MULTISAMPLE:
296 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
297 filter = GL_NEAREST;
298 /* fallthrough */
299
300 case GL_TEXTURE_RECTANGLE_NV:
301 case GL_TEXTURE_EXTERNAL_OES:
302 /* have to init wrap and filter state here - kind of klunky */
303 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
304 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
305 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
306 obj->Sampler.MinFilter = filter;
307 obj->Sampler.MagFilter = filter;
308 if (ctx->Driver.TexParameter) {
309 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
310 const GLfloat fparam_filter[1] = {(GLfloat) filter};
311 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
312 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
313 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
314 ctx->Driver.TexParameter(ctx, obj,
315 GL_TEXTURE_MIN_FILTER, fparam_filter);
316 ctx->Driver.TexParameter(ctx, obj,
317 GL_TEXTURE_MAG_FILTER, fparam_filter);
318 }
319 break;
320
321 default:
322 /* nothing needs done */
323 break;
324 }
325 }
326
327
328 /**
329 * Deallocate a texture object struct. It should have already been
330 * removed from the texture object pool.
331 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
332 *
333 * \param shared the shared GL state to which the object belongs.
334 * \param texObj the texture object to delete.
335 */
336 void
337 _mesa_delete_texture_object(struct gl_context *ctx,
338 struct gl_texture_object *texObj)
339 {
340 GLuint i, face;
341
342 /* Set Target to an invalid value. With some assertions elsewhere
343 * we can try to detect possible use of deleted textures.
344 */
345 texObj->Target = 0x99;
346
347 /* free the texture images */
348 for (face = 0; face < 6; face++) {
349 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
350 if (texObj->Image[face][i]) {
351 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
352 }
353 }
354 }
355
356 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
357
358 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
359 mtx_destroy(&texObj->Mutex);
360
361 free(texObj->Label);
362
363 /* free this object */
364 free(texObj);
365 }
366
367
368 /**
369 * Copy texture object state from one texture object to another.
370 * Use for glPush/PopAttrib.
371 *
372 * \param dest destination texture object.
373 * \param src source texture object.
374 */
375 void
376 _mesa_copy_texture_object( struct gl_texture_object *dest,
377 const struct gl_texture_object *src )
378 {
379 dest->Target = src->Target;
380 dest->TargetIndex = src->TargetIndex;
381 dest->Name = src->Name;
382 dest->Priority = src->Priority;
383 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
384 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
385 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
386 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
387 dest->Sampler.WrapS = src->Sampler.WrapS;
388 dest->Sampler.WrapT = src->Sampler.WrapT;
389 dest->Sampler.WrapR = src->Sampler.WrapR;
390 dest->Sampler.MinFilter = src->Sampler.MinFilter;
391 dest->Sampler.MagFilter = src->Sampler.MagFilter;
392 dest->Sampler.MinLod = src->Sampler.MinLod;
393 dest->Sampler.MaxLod = src->Sampler.MaxLod;
394 dest->Sampler.LodBias = src->Sampler.LodBias;
395 dest->BaseLevel = src->BaseLevel;
396 dest->MaxLevel = src->MaxLevel;
397 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
398 dest->Sampler.CompareMode = src->Sampler.CompareMode;
399 dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
400 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
401 dest->DepthMode = src->DepthMode;
402 dest->StencilSampling = src->StencilSampling;
403 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
404 dest->_MaxLevel = src->_MaxLevel;
405 dest->_MaxLambda = src->_MaxLambda;
406 dest->GenerateMipmap = src->GenerateMipmap;
407 dest->_BaseComplete = src->_BaseComplete;
408 dest->_MipmapComplete = src->_MipmapComplete;
409 COPY_4V(dest->Swizzle, src->Swizzle);
410 dest->_Swizzle = src->_Swizzle;
411
412 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
413 }
414
415
416 /**
417 * Free all texture images of the given texture object.
418 *
419 * \param ctx GL context.
420 * \param t texture object.
421 *
422 * \sa _mesa_clear_texture_image().
423 */
424 void
425 _mesa_clear_texture_object(struct gl_context *ctx,
426 struct gl_texture_object *texObj)
427 {
428 GLuint i, j;
429
430 if (texObj->Target == 0)
431 return;
432
433 for (i = 0; i < MAX_FACES; i++) {
434 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
435 struct gl_texture_image *texImage = texObj->Image[i][j];
436 if (texImage)
437 _mesa_clear_texture_image(ctx, texImage);
438 }
439 }
440 }
441
442
443 /**
444 * Check if the given texture object is valid by examining its Target field.
445 * For debugging only.
446 */
447 static GLboolean
448 valid_texture_object(const struct gl_texture_object *tex)
449 {
450 switch (tex->Target) {
451 case 0:
452 case GL_TEXTURE_1D:
453 case GL_TEXTURE_2D:
454 case GL_TEXTURE_3D:
455 case GL_TEXTURE_CUBE_MAP_ARB:
456 case GL_TEXTURE_RECTANGLE_NV:
457 case GL_TEXTURE_1D_ARRAY_EXT:
458 case GL_TEXTURE_2D_ARRAY_EXT:
459 case GL_TEXTURE_BUFFER:
460 case GL_TEXTURE_EXTERNAL_OES:
461 case GL_TEXTURE_CUBE_MAP_ARRAY:
462 case GL_TEXTURE_2D_MULTISAMPLE:
463 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
464 return GL_TRUE;
465 case 0x99:
466 _mesa_problem(NULL, "invalid reference to a deleted texture object");
467 return GL_FALSE;
468 default:
469 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
470 tex->Target, tex->Name);
471 return GL_FALSE;
472 }
473 }
474
475
476 /**
477 * Reference (or unreference) a texture object.
478 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
479 * If 'tex' is non-null, increment its refcount.
480 * This is normally only called from the _mesa_reference_texobj() macro
481 * when there's a real pointer change.
482 */
483 void
484 _mesa_reference_texobj_(struct gl_texture_object **ptr,
485 struct gl_texture_object *tex)
486 {
487 assert(ptr);
488
489 if (*ptr) {
490 /* Unreference the old texture */
491 GLboolean deleteFlag = GL_FALSE;
492 struct gl_texture_object *oldTex = *ptr;
493
494 ASSERT(valid_texture_object(oldTex));
495 (void) valid_texture_object; /* silence warning in release builds */
496
497 mtx_lock(&oldTex->Mutex);
498 ASSERT(oldTex->RefCount > 0);
499 oldTex->RefCount--;
500
501 deleteFlag = (oldTex->RefCount == 0);
502 mtx_unlock(&oldTex->Mutex);
503
504 if (deleteFlag) {
505 GET_CURRENT_CONTEXT(ctx);
506 if (ctx)
507 ctx->Driver.DeleteTexture(ctx, oldTex);
508 else
509 _mesa_problem(NULL, "Unable to delete texture, no context");
510 }
511
512 *ptr = NULL;
513 }
514 assert(!*ptr);
515
516 if (tex) {
517 /* reference new texture */
518 ASSERT(valid_texture_object(tex));
519 mtx_lock(&tex->Mutex);
520 if (tex->RefCount == 0) {
521 /* this texture's being deleted (look just above) */
522 /* Not sure this can every really happen. Warn if it does. */
523 _mesa_problem(NULL, "referencing deleted texture object");
524 *ptr = NULL;
525 }
526 else {
527 tex->RefCount++;
528 *ptr = tex;
529 }
530 mtx_unlock(&tex->Mutex);
531 }
532 }
533
534
535 enum base_mipmap { BASE, MIPMAP };
536
537
538 /**
539 * Mark a texture object as incomplete. There are actually three kinds of
540 * (in)completeness:
541 * 1. "base incomplete": the base level of the texture is invalid so no
542 * texturing is possible.
543 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
544 * mipmap filtering isn't possible, but non-mipmap filtering is.
545 * 3. "texture incompleteness": some combination of texture state and
546 * sampler state renders the texture incomplete.
547 *
548 * \param t texture object
549 * \param bm either BASE or MIPMAP to indicate what's incomplete
550 * \param fmt... string describing why it's incomplete (for debugging).
551 */
552 static void
553 incomplete(struct gl_texture_object *t, enum base_mipmap bm,
554 const char *fmt, ...)
555 {
556 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_TEXTURE) {
557 va_list args;
558 char s[100];
559
560 va_start(args, fmt);
561 vsnprintf(s, sizeof(s), fmt, args);
562 va_end(args);
563
564 _mesa_debug(NULL, "Texture Obj %d incomplete because: %s\n", t->Name, s);
565 }
566
567 if (bm == BASE)
568 t->_BaseComplete = GL_FALSE;
569 t->_MipmapComplete = GL_FALSE;
570 }
571
572
573 /**
574 * Examine a texture object to determine if it is complete.
575 *
576 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
577 * accordingly.
578 *
579 * \param ctx GL context.
580 * \param t texture object.
581 *
582 * According to the texture target, verifies that each of the mipmaps is
583 * present and has the expected size.
584 */
585 void
586 _mesa_test_texobj_completeness( const struct gl_context *ctx,
587 struct gl_texture_object *t )
588 {
589 const GLint baseLevel = t->BaseLevel;
590 const struct gl_texture_image *baseImage;
591 GLint maxLevels = 0;
592
593 /* We'll set these to FALSE if tests fail below */
594 t->_BaseComplete = GL_TRUE;
595 t->_MipmapComplete = GL_TRUE;
596
597 if (t->Target == GL_TEXTURE_BUFFER) {
598 /* Buffer textures are always considered complete. The obvious case where
599 * they would be incomplete (no BO attached) is actually specced to be
600 * undefined rendering results.
601 */
602 return;
603 }
604
605 /* Detect cases where the application set the base level to an invalid
606 * value.
607 */
608 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
609 incomplete(t, BASE, "base level = %d is invalid", baseLevel);
610 return;
611 }
612
613 if (t->MaxLevel < baseLevel) {
614 incomplete(t, MIPMAP, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
615 t->MaxLevel, baseLevel);
616 return;
617 }
618
619 baseImage = t->Image[0][baseLevel];
620
621 /* Always need the base level image */
622 if (!baseImage) {
623 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel);
624 return;
625 }
626
627 /* Check width/height/depth for zero */
628 if (baseImage->Width == 0 ||
629 baseImage->Height == 0 ||
630 baseImage->Depth == 0) {
631 incomplete(t, BASE, "texture width or height or depth = 0");
632 return;
633 }
634
635 /* Check if the texture values are integer */
636 {
637 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat);
638 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT;
639 }
640
641 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
642 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
643 */
644 switch (t->Target) {
645 case GL_TEXTURE_1D:
646 case GL_TEXTURE_1D_ARRAY_EXT:
647 maxLevels = ctx->Const.MaxTextureLevels;
648 break;
649 case GL_TEXTURE_2D:
650 case GL_TEXTURE_2D_ARRAY_EXT:
651 maxLevels = ctx->Const.MaxTextureLevels;
652 break;
653 case GL_TEXTURE_3D:
654 maxLevels = ctx->Const.Max3DTextureLevels;
655 break;
656 case GL_TEXTURE_CUBE_MAP_ARB:
657 case GL_TEXTURE_CUBE_MAP_ARRAY:
658 maxLevels = ctx->Const.MaxCubeTextureLevels;
659 break;
660 case GL_TEXTURE_RECTANGLE_NV:
661 case GL_TEXTURE_BUFFER:
662 case GL_TEXTURE_EXTERNAL_OES:
663 case GL_TEXTURE_2D_MULTISAMPLE:
664 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
665 maxLevels = 1; /* no mipmapping */
666 break;
667 default:
668 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
669 return;
670 }
671
672 ASSERT(maxLevels > 0);
673
674 t->_MaxLevel = MIN3(t->MaxLevel,
675 /* 'p' in the GL spec */
676 (int) (baseLevel + baseImage->MaxNumLevels - 1),
677 /* 'q' in the GL spec */
678 maxLevels - 1);
679
680 if (t->Immutable) {
681 /* Adjust max level for views: the data store may have more levels than
682 * the view exposes.
683 */
684 t->_MaxLevel = MIN2(t->_MaxLevel, t->NumLevels - 1);
685 }
686
687 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
688 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel);
689
690 if (t->Immutable) {
691 /* This texture object was created with glTexStorage1/2/3D() so we
692 * know that all the mipmap levels are the right size and all cube
693 * map faces are the same size.
694 * We don't need to do any of the additional checks below.
695 */
696 return;
697 }
698
699 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
700 /* Make sure that all six cube map level 0 images are the same size.
701 * Note: we know that the image's width==height (we enforce that
702 * at glTexImage time) so we only need to test the width here.
703 */
704 GLuint face;
705 assert(baseImage->Width2 == baseImage->Height);
706 for (face = 1; face < 6; face++) {
707 assert(t->Image[face][baseLevel] == NULL ||
708 t->Image[face][baseLevel]->Width2 ==
709 t->Image[face][baseLevel]->Height2);
710 if (t->Image[face][baseLevel] == NULL ||
711 t->Image[face][baseLevel]->Width2 != baseImage->Width2) {
712 incomplete(t, BASE, "Cube face missing or mismatched size");
713 return;
714 }
715 }
716 }
717
718 /*
719 * Do mipmap consistency checking.
720 * Note: we don't care about the current texture sampler state here.
721 * To determine texture completeness we'll either look at _BaseComplete
722 * or _MipmapComplete depending on the current minification filter mode.
723 */
724 {
725 GLint i;
726 const GLint minLevel = baseLevel;
727 const GLint maxLevel = t->_MaxLevel;
728 const GLuint numFaces = _mesa_num_tex_faces(t->Target);
729 GLuint width, height, depth, face;
730
731 if (minLevel > maxLevel) {
732 incomplete(t, MIPMAP, "minLevel > maxLevel");
733 return;
734 }
735
736 /* Get the base image's dimensions */
737 width = baseImage->Width2;
738 height = baseImage->Height2;
739 depth = baseImage->Depth2;
740
741 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
742 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
743 */
744 for (i = baseLevel + 1; i < maxLevels; i++) {
745 /* Compute the expected size of image at level[i] */
746 if (width > 1) {
747 width /= 2;
748 }
749 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) {
750 height /= 2;
751 }
752 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY
753 && t->Target != GL_TEXTURE_CUBE_MAP_ARRAY) {
754 depth /= 2;
755 }
756
757 /* loop over cube faces (or single face otherwise) */
758 for (face = 0; face < numFaces; face++) {
759 if (i >= minLevel && i <= maxLevel) {
760 const struct gl_texture_image *img = t->Image[face][i];
761
762 if (!img) {
763 incomplete(t, MIPMAP, "TexImage[%d] is missing", i);
764 return;
765 }
766 if (img->TexFormat != baseImage->TexFormat) {
767 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]");
768 return;
769 }
770 if (img->Border != baseImage->Border) {
771 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]");
772 return;
773 }
774 if (img->Width2 != width) {
775 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i,
776 img->Width2);
777 return;
778 }
779 if (img->Height2 != height) {
780 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i,
781 img->Height2);
782 return;
783 }
784 if (img->Depth2 != depth) {
785 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i,
786 img->Depth2);
787 return;
788 }
789
790 /* Extra checks for cube textures */
791 if (face > 0) {
792 /* check that cube faces are the same size */
793 if (img->Width2 != t->Image[0][i]->Width2 ||
794 img->Height2 != t->Image[0][i]->Height2) {
795 incomplete(t, MIPMAP, "CubeMap Image[n][i] bad size");
796 return;
797 }
798 }
799 }
800 }
801
802 if (width == 1 && height == 1 && depth == 1) {
803 return; /* found smallest needed mipmap, all done! */
804 }
805 }
806 }
807 }
808
809
810 /**
811 * Check if the given cube map texture is "cube complete" as defined in
812 * the OpenGL specification.
813 */
814 GLboolean
815 _mesa_cube_complete(const struct gl_texture_object *texObj)
816 {
817 const GLint baseLevel = texObj->BaseLevel;
818 const struct gl_texture_image *img0, *img;
819 GLuint face;
820
821 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
822 return GL_FALSE;
823
824 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS))
825 return GL_FALSE;
826
827 /* check first face */
828 img0 = texObj->Image[0][baseLevel];
829 if (!img0 ||
830 img0->Width < 1 ||
831 img0->Width != img0->Height)
832 return GL_FALSE;
833
834 /* check remaining faces vs. first face */
835 for (face = 1; face < 6; face++) {
836 img = texObj->Image[face][baseLevel];
837 if (!img ||
838 img->Width != img0->Width ||
839 img->Height != img0->Height ||
840 img->TexFormat != img0->TexFormat)
841 return GL_FALSE;
842 }
843
844 return GL_TRUE;
845 }
846
847
848 /**
849 * Mark a texture object dirty. It forces the object to be incomplete
850 * and forces the context to re-validate its state.
851 *
852 * \param ctx GL context.
853 * \param texObj texture object.
854 */
855 void
856 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj)
857 {
858 texObj->_BaseComplete = GL_FALSE;
859 texObj->_MipmapComplete = GL_FALSE;
860 ctx->NewState |= _NEW_TEXTURE;
861 }
862
863
864 /**
865 * Return pointer to a default/fallback texture of the given type/target.
866 * The texture is an RGBA texture with all texels = (0,0,0,1).
867 * That's the value a GLSL sampler should get when sampling from an
868 * incomplete texture.
869 */
870 struct gl_texture_object *
871 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex)
872 {
873 if (!ctx->Shared->FallbackTex[tex]) {
874 /* create fallback texture now */
875 const GLsizei width = 1, height = 1;
876 GLsizei depth = 1;
877 GLubyte texel[24];
878 struct gl_texture_object *texObj;
879 struct gl_texture_image *texImage;
880 mesa_format texFormat;
881 GLuint dims, face, numFaces = 1;
882 GLenum target;
883
884 for (face = 0; face < 6; face++) {
885 texel[4*face + 0] =
886 texel[4*face + 1] =
887 texel[4*face + 2] = 0x0;
888 texel[4*face + 3] = 0xff;
889 }
890
891 switch (tex) {
892 case TEXTURE_2D_ARRAY_INDEX:
893 dims = 3;
894 target = GL_TEXTURE_2D_ARRAY;
895 break;
896 case TEXTURE_1D_ARRAY_INDEX:
897 dims = 2;
898 target = GL_TEXTURE_1D_ARRAY;
899 break;
900 case TEXTURE_CUBE_INDEX:
901 dims = 2;
902 target = GL_TEXTURE_CUBE_MAP;
903 numFaces = 6;
904 break;
905 case TEXTURE_3D_INDEX:
906 dims = 3;
907 target = GL_TEXTURE_3D;
908 break;
909 case TEXTURE_RECT_INDEX:
910 dims = 2;
911 target = GL_TEXTURE_RECTANGLE;
912 break;
913 case TEXTURE_2D_INDEX:
914 dims = 2;
915 target = GL_TEXTURE_2D;
916 break;
917 case TEXTURE_1D_INDEX:
918 dims = 1;
919 target = GL_TEXTURE_1D;
920 break;
921 case TEXTURE_BUFFER_INDEX:
922 dims = 0;
923 target = GL_TEXTURE_BUFFER;
924 break;
925 case TEXTURE_CUBE_ARRAY_INDEX:
926 dims = 3;
927 target = GL_TEXTURE_CUBE_MAP_ARRAY;
928 depth = 6;
929 break;
930 case TEXTURE_EXTERNAL_INDEX:
931 dims = 2;
932 target = GL_TEXTURE_EXTERNAL_OES;
933 break;
934 case TEXTURE_2D_MULTISAMPLE_INDEX:
935 dims = 2;
936 target = GL_TEXTURE_2D_MULTISAMPLE;
937 break;
938 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX:
939 dims = 3;
940 target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
941 break;
942 default:
943 /* no-op */
944 return NULL;
945 }
946
947 /* create texture object */
948 texObj = ctx->Driver.NewTextureObject(ctx, 0, target);
949 if (!texObj)
950 return NULL;
951
952 assert(texObj->RefCount == 1);
953 texObj->Sampler.MinFilter = GL_NEAREST;
954 texObj->Sampler.MagFilter = GL_NEAREST;
955
956 texFormat = ctx->Driver.ChooseTextureFormat(ctx, target,
957 GL_RGBA, GL_RGBA,
958 GL_UNSIGNED_BYTE);
959
960 /* need a loop here just for cube maps */
961 for (face = 0; face < numFaces; face++) {
962 GLenum faceTarget;
963
964 if (target == GL_TEXTURE_CUBE_MAP)
965 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
966 else
967 faceTarget = target;
968
969 /* initialize level[0] texture image */
970 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0);
971
972 _mesa_init_teximage_fields(ctx, texImage,
973 width,
974 (dims > 1) ? height : 1,
975 (dims > 2) ? depth : 1,
976 0, /* border */
977 GL_RGBA, texFormat);
978
979 ctx->Driver.TexImage(ctx, dims, texImage,
980 GL_RGBA, GL_UNSIGNED_BYTE, texel,
981 &ctx->DefaultPacking);
982 }
983
984 _mesa_test_texobj_completeness(ctx, texObj);
985 assert(texObj->_BaseComplete);
986 assert(texObj->_MipmapComplete);
987
988 ctx->Shared->FallbackTex[tex] = texObj;
989 }
990 return ctx->Shared->FallbackTex[tex];
991 }
992
993
994 /**
995 * Compute the size of the given texture object, in bytes.
996 */
997 static GLuint
998 texture_size(const struct gl_texture_object *texObj)
999 {
1000 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1001 GLuint face, level, size = 0;
1002
1003 for (face = 0; face < numFaces; face++) {
1004 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) {
1005 const struct gl_texture_image *img = texObj->Image[face][level];
1006 if (img) {
1007 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width,
1008 img->Height, img->Depth);
1009 size += sz;
1010 }
1011 }
1012 }
1013
1014 return size;
1015 }
1016
1017
1018 /**
1019 * Callback called from _mesa_HashWalk()
1020 */
1021 static void
1022 count_tex_size(GLuint key, void *data, void *userData)
1023 {
1024 const struct gl_texture_object *texObj =
1025 (const struct gl_texture_object *) data;
1026 GLuint *total = (GLuint *) userData;
1027
1028 (void) key;
1029
1030 *total = *total + texture_size(texObj);
1031 }
1032
1033
1034 /**
1035 * Compute total size (in bytes) of all textures for the given context.
1036 * For debugging purposes.
1037 */
1038 GLuint
1039 _mesa_total_texture_memory(struct gl_context *ctx)
1040 {
1041 GLuint tgt, total = 0;
1042
1043 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total);
1044
1045 /* plus, the default texture objects */
1046 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1047 total += texture_size(ctx->Shared->DefaultTex[tgt]);
1048 }
1049
1050 return total;
1051 }
1052
1053
1054 /**
1055 * Return the base format for the given texture object by looking
1056 * at the base texture image.
1057 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
1058 */
1059 GLenum
1060 _mesa_texture_base_format(const struct gl_texture_object *texObj)
1061 {
1062 const struct gl_texture_image *texImage = _mesa_base_tex_image(texObj);
1063
1064 return texImage ? texImage->_BaseFormat : GL_NONE;
1065 }
1066
1067
1068 static struct gl_texture_object *
1069 invalidate_tex_image_error_check(struct gl_context *ctx, GLuint texture,
1070 GLint level, const char *name)
1071 {
1072 /* The GL_ARB_invalidate_subdata spec says:
1073 *
1074 * "If <texture> is zero or is not the name of a texture, the error
1075 * INVALID_VALUE is generated."
1076 *
1077 * This performs the error check in a different order than listed in the
1078 * spec. We have to get the texture object before we can validate the
1079 * other parameters against values in the texture object.
1080 */
1081 struct gl_texture_object *const t = _mesa_lookup_texture(ctx, texture);
1082 if (texture == 0 || t == NULL) {
1083 _mesa_error(ctx, GL_INVALID_VALUE, "%s(texture)", name);
1084 return NULL;
1085 }
1086
1087 /* The GL_ARB_invalidate_subdata spec says:
1088 *
1089 * "If <level> is less than zero or greater than the base 2 logarithm
1090 * of the maximum texture width, height, or depth, the error
1091 * INVALID_VALUE is generated."
1092 */
1093 if (level < 0 || level > t->MaxLevel) {
1094 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
1095 return NULL;
1096 }
1097
1098 /* The GL_ARB_invalidate_subdata spec says:
1099 *
1100 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1101 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1102 * is not zero, the error INVALID_VALUE is generated."
1103 */
1104 if (level != 0) {
1105 switch (t->Target) {
1106 case GL_TEXTURE_RECTANGLE:
1107 case GL_TEXTURE_BUFFER:
1108 case GL_TEXTURE_2D_MULTISAMPLE:
1109 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1110 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
1111 return NULL;
1112
1113 default:
1114 break;
1115 }
1116 }
1117
1118 return t;
1119 }
1120
1121 /**
1122 * Helper function for glCreateTextures and glGenTextures. Need this because
1123 * glCreateTextures should throw errors if target = 0. This is not exposed to
1124 * the rest of Mesa to encourage Mesa internals to use nameless textures,
1125 * which do not require expensive hash lookups.
1126 */
1127 static void
1128 create_textures(struct gl_context *ctx, GLenum target,
1129 GLsizei n, GLuint *textures, bool dsa)
1130 {
1131 GLuint first;
1132 GLint i;
1133 const char *func = dsa ? "Create" : "Gen";
1134
1135 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1136 _mesa_debug(ctx, "gl%sTextures %d\n", func, n);
1137
1138 if (n < 0) {
1139 _mesa_error( ctx, GL_INVALID_VALUE, "gl%sTextures(n < 0)", func );
1140 return;
1141 }
1142
1143 if (!textures)
1144 return;
1145
1146 /*
1147 * This must be atomic (generation and allocation of texture IDs)
1148 */
1149 mtx_lock(&ctx->Shared->Mutex);
1150
1151 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
1152
1153 /* Allocate new, empty texture objects */
1154 for (i = 0; i < n; i++) {
1155 struct gl_texture_object *texObj;
1156 GLint targetIndex;
1157 GLuint name = first + i;
1158 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
1159 if (!texObj) {
1160 mtx_unlock(&ctx->Shared->Mutex);
1161 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sTextures", func);
1162 return;
1163 }
1164
1165 /* Initialize the target index if target is non-zero. */
1166 if (target != 0) {
1167 targetIndex = _mesa_tex_target_to_index(ctx, texObj->Target);
1168 if (targetIndex < 0) { /* Bad Target */
1169 mtx_unlock(&ctx->Shared->Mutex);
1170 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sTextures(target = %s)",
1171 func, _mesa_lookup_enum_by_nr(texObj->Target));
1172 return;
1173 }
1174 assert(targetIndex < NUM_TEXTURE_TARGETS);
1175 texObj->TargetIndex = targetIndex;
1176 }
1177
1178 /* insert into hash table */
1179 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
1180
1181 textures[i] = name;
1182 }
1183
1184 mtx_unlock(&ctx->Shared->Mutex);
1185 }
1186
1187 /*@}*/
1188
1189
1190 /***********************************************************************/
1191 /** \name API functions */
1192 /*@{*/
1193
1194
1195 /**
1196 * Generate texture names.
1197 *
1198 * \param n number of texture names to be generated.
1199 * \param textures an array in which will hold the generated texture names.
1200 *
1201 * \sa glGenTextures(), glCreateTextures().
1202 *
1203 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1204 * IDs which are stored in \p textures. Corresponding empty texture
1205 * objects are also generated.
1206 */
1207 void GLAPIENTRY
1208 _mesa_GenTextures(GLsizei n, GLuint *textures)
1209 {
1210 GET_CURRENT_CONTEXT(ctx);
1211 create_textures(ctx, 0, n, textures, false);
1212 }
1213
1214 /**
1215 * Create texture objects.
1216 *
1217 * \param target the texture target for each name to be generated.
1218 * \param n number of texture names to be generated.
1219 * \param textures an array in which will hold the generated texture names.
1220 *
1221 * \sa glCreateTextures(), glGenTextures().
1222 *
1223 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1224 * IDs which are stored in \p textures. Corresponding empty texture
1225 * objects are also generated.
1226 */
1227 void GLAPIENTRY
1228 _mesa_CreateTextures(GLenum target, GLsizei n, GLuint *textures)
1229 {
1230 GLint targetIndex;
1231 GET_CURRENT_CONTEXT(ctx);
1232
1233 /*
1234 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1235 * glCreateTextures should do with invalid targets, which was probably an
1236 * oversight. This conforms to the spec for glBindTexture.
1237 */
1238 targetIndex = _mesa_tex_target_to_index(ctx, target);
1239 if (targetIndex < 0) {
1240 _mesa_error(ctx, GL_INVALID_ENUM, "glCreateTextures(target)");
1241 return;
1242 }
1243
1244 create_textures(ctx, target, n, textures, true);
1245 }
1246
1247 /**
1248 * Check if the given texture object is bound to the current draw or
1249 * read framebuffer. If so, Unbind it.
1250 */
1251 static void
1252 unbind_texobj_from_fbo(struct gl_context *ctx,
1253 struct gl_texture_object *texObj)
1254 {
1255 bool progress = false;
1256
1257 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1258 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1259 * says:
1260 *
1261 * "If a texture object is deleted while its image is attached to one
1262 * or more attachment points in the currently bound framebuffer, then
1263 * it is as if FramebufferTexture* had been called, with a texture of
1264 * zero, for each attachment point to which this image was attached in
1265 * the currently bound framebuffer. In other words, this texture image
1266 * is first detached from all attachment points in the currently bound
1267 * framebuffer. Note that the texture image is specifically not
1268 * detached from any other framebuffer objects. Detaching the texture
1269 * image from any other framebuffer objects is the responsibility of
1270 * the application."
1271 */
1272 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1273 progress = _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, texObj);
1274 }
1275 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1276 && ctx->ReadBuffer != ctx->DrawBuffer) {
1277 progress = _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, texObj)
1278 || progress;
1279 }
1280
1281 if (progress)
1282 /* Vertices are already flushed by _mesa_DeleteTextures */
1283 ctx->NewState |= _NEW_BUFFERS;
1284 }
1285
1286
1287 /**
1288 * Check if the given texture object is bound to any texture image units and
1289 * unbind it if so (revert to default textures).
1290 */
1291 static void
1292 unbind_texobj_from_texunits(struct gl_context *ctx,
1293 struct gl_texture_object *texObj)
1294 {
1295 const gl_texture_index index = texObj->TargetIndex;
1296 GLuint u;
1297
1298 if (texObj->Target == 0)
1299 return;
1300
1301 for (u = 0; u < ctx->Texture.NumCurrentTexUsed; u++) {
1302 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
1303
1304 if (texObj == unit->CurrentTex[index]) {
1305 /* Bind the default texture for this unit/target */
1306 _mesa_reference_texobj(&unit->CurrentTex[index],
1307 ctx->Shared->DefaultTex[index]);
1308 unit->_BoundTextures &= ~(1 << index);
1309 }
1310 }
1311 }
1312
1313
1314 /**
1315 * Check if the given texture object is bound to any shader image unit
1316 * and unbind it if that's the case.
1317 */
1318 static void
1319 unbind_texobj_from_image_units(struct gl_context *ctx,
1320 struct gl_texture_object *texObj)
1321 {
1322 GLuint i;
1323
1324 for (i = 0; i < ctx->Const.MaxImageUnits; i++) {
1325 struct gl_image_unit *unit = &ctx->ImageUnits[i];
1326
1327 if (texObj == unit->TexObj)
1328 _mesa_reference_texobj(&unit->TexObj, NULL);
1329 }
1330 }
1331
1332
1333 /**
1334 * Unbinds all textures bound to the given texture image unit.
1335 */
1336 static void
1337 unbind_textures_from_unit(struct gl_context *ctx, GLuint unit)
1338 {
1339 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1340
1341 while (texUnit->_BoundTextures) {
1342 const GLuint index = ffs(texUnit->_BoundTextures) - 1;
1343 struct gl_texture_object *texObj = ctx->Shared->DefaultTex[index];
1344
1345 _mesa_reference_texobj(&texUnit->CurrentTex[index], texObj);
1346
1347 /* Pass BindTexture call to device driver */
1348 if (ctx->Driver.BindTexture)
1349 ctx->Driver.BindTexture(ctx, unit, 0, texObj);
1350
1351 texUnit->_BoundTextures &= ~(1 << index);
1352 ctx->NewState |= _NEW_TEXTURE;
1353 }
1354 }
1355
1356
1357 /**
1358 * Delete named textures.
1359 *
1360 * \param n number of textures to be deleted.
1361 * \param textures array of texture IDs to be deleted.
1362 *
1363 * \sa glDeleteTextures().
1364 *
1365 * If we're about to delete a texture that's currently bound to any
1366 * texture unit, unbind the texture first. Decrement the reference
1367 * count on the texture object and delete it if it's zero.
1368 * Recall that texture objects can be shared among several rendering
1369 * contexts.
1370 */
1371 void GLAPIENTRY
1372 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
1373 {
1374 GET_CURRENT_CONTEXT(ctx);
1375 GLint i;
1376
1377 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1378 _mesa_debug(ctx, "glDeleteTextures %d\n", n);
1379
1380 FLUSH_VERTICES(ctx, 0); /* too complex */
1381
1382 if (!textures)
1383 return;
1384
1385 for (i = 0; i < n; i++) {
1386 if (textures[i] > 0) {
1387 struct gl_texture_object *delObj
1388 = _mesa_lookup_texture(ctx, textures[i]);
1389
1390 if (delObj) {
1391 _mesa_lock_texture(ctx, delObj);
1392
1393 /* Check if texture is bound to any framebuffer objects.
1394 * If so, unbind.
1395 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1396 */
1397 unbind_texobj_from_fbo(ctx, delObj);
1398
1399 /* Check if this texture is currently bound to any texture units.
1400 * If so, unbind it.
1401 */
1402 unbind_texobj_from_texunits(ctx, delObj);
1403
1404 /* Check if this texture is currently bound to any shader
1405 * image unit. If so, unbind it.
1406 * See section 3.9.X of GL_ARB_shader_image_load_store.
1407 */
1408 unbind_texobj_from_image_units(ctx, delObj);
1409
1410 _mesa_unlock_texture(ctx, delObj);
1411
1412 ctx->NewState |= _NEW_TEXTURE;
1413
1414 /* The texture _name_ is now free for re-use.
1415 * Remove it from the hash table now.
1416 */
1417 mtx_lock(&ctx->Shared->Mutex);
1418 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
1419 mtx_unlock(&ctx->Shared->Mutex);
1420
1421 /* Unreference the texobj. If refcount hits zero, the texture
1422 * will be deleted.
1423 */
1424 _mesa_reference_texobj(&delObj, NULL);
1425 }
1426 }
1427 }
1428 }
1429
1430
1431 /**
1432 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1433 * into the corresponding Mesa texture target index.
1434 * Note that proxy targets are not valid here.
1435 * \return TEXTURE_x_INDEX or -1 if target is invalid
1436 */
1437 int
1438 _mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target)
1439 {
1440 switch (target) {
1441 case GL_TEXTURE_1D:
1442 return _mesa_is_desktop_gl(ctx) ? TEXTURE_1D_INDEX : -1;
1443 case GL_TEXTURE_2D:
1444 return TEXTURE_2D_INDEX;
1445 case GL_TEXTURE_3D:
1446 return ctx->API != API_OPENGLES ? TEXTURE_3D_INDEX : -1;
1447 case GL_TEXTURE_CUBE_MAP:
1448 return ctx->Extensions.ARB_texture_cube_map
1449 ? TEXTURE_CUBE_INDEX : -1;
1450 case GL_TEXTURE_RECTANGLE:
1451 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle
1452 ? TEXTURE_RECT_INDEX : -1;
1453 case GL_TEXTURE_1D_ARRAY:
1454 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array
1455 ? TEXTURE_1D_ARRAY_INDEX : -1;
1456 case GL_TEXTURE_2D_ARRAY:
1457 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1458 || _mesa_is_gles3(ctx)
1459 ? TEXTURE_2D_ARRAY_INDEX : -1;
1460 case GL_TEXTURE_BUFFER:
1461 return ctx->API == API_OPENGL_CORE &&
1462 ctx->Extensions.ARB_texture_buffer_object ?
1463 TEXTURE_BUFFER_INDEX : -1;
1464 case GL_TEXTURE_EXTERNAL_OES:
1465 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
1466 ? TEXTURE_EXTERNAL_INDEX : -1;
1467 case GL_TEXTURE_CUBE_MAP_ARRAY:
1468 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_cube_map_array
1469 ? TEXTURE_CUBE_ARRAY_INDEX : -1;
1470 case GL_TEXTURE_2D_MULTISAMPLE:
1471 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample
1472 ? TEXTURE_2D_MULTISAMPLE_INDEX: -1;
1473 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1474 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample
1475 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: -1;
1476 default:
1477 return -1;
1478 }
1479 }
1480
1481
1482 /**
1483 * Bind a named texture to a texturing target.
1484 *
1485 * \param target texture target.
1486 * \param texName texture name.
1487 *
1488 * \sa glBindTexture().
1489 *
1490 * Determines the old texture object bound and returns immediately if rebinding
1491 * the same texture. Get the current texture which is either a default texture
1492 * if name is null, a named texture from the hash, or a new texture if the
1493 * given texture name is new. Increments its reference count, binds it, and
1494 * calls dd_function_table::BindTexture. Decrements the old texture reference
1495 * count and deletes it if it reaches zero.
1496 */
1497 void GLAPIENTRY
1498 _mesa_BindTexture( GLenum target, GLuint texName )
1499 {
1500 GET_CURRENT_CONTEXT(ctx);
1501 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
1502 struct gl_texture_object *newTexObj = NULL;
1503 GLint targetIndex;
1504
1505 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1506 _mesa_debug(ctx, "glBindTexture %s %d\n",
1507 _mesa_lookup_enum_by_nr(target), (GLint) texName);
1508
1509 targetIndex = _mesa_tex_target_to_index(ctx, target);
1510 if (targetIndex < 0) {
1511 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1512 return;
1513 }
1514 assert(targetIndex < NUM_TEXTURE_TARGETS);
1515
1516 /*
1517 * Get pointer to new texture object (newTexObj)
1518 */
1519 if (texName == 0) {
1520 /* Use a default texture object */
1521 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1522 }
1523 else {
1524 /* non-default texture object */
1525 newTexObj = _mesa_lookup_texture(ctx, texName);
1526 if (newTexObj) {
1527 /* error checking */
1528 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1529 /* The named texture object's target doesn't match the
1530 * given target
1531 */
1532 _mesa_error( ctx, GL_INVALID_OPERATION,
1533 "glBindTexture(target mismatch)" );
1534 return;
1535 }
1536 if (newTexObj->Target == 0) {
1537 finish_texture_init(ctx, target, newTexObj);
1538 }
1539 }
1540 else {
1541 if (ctx->API == API_OPENGL_CORE) {
1542 _mesa_error(ctx, GL_INVALID_OPERATION,
1543 "glBindTexture(non-gen name)");
1544 return;
1545 }
1546
1547 /* if this is a new texture id, allocate a texture object now */
1548 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1549 if (!newTexObj) {
1550 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1551 return;
1552 }
1553
1554 /* and insert it into hash table */
1555 mtx_lock(&ctx->Shared->Mutex);
1556 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1557 mtx_unlock(&ctx->Shared->Mutex);
1558 }
1559 newTexObj->Target = target;
1560 newTexObj->TargetIndex = targetIndex;
1561 }
1562
1563 assert(valid_texture_object(newTexObj));
1564
1565 /* Check if this texture is only used by this context and is already bound.
1566 * If so, just return.
1567 */
1568 {
1569 GLboolean early_out;
1570 mtx_lock(&ctx->Shared->Mutex);
1571 early_out = ((ctx->Shared->RefCount == 1)
1572 && (newTexObj == texUnit->CurrentTex[targetIndex]));
1573 mtx_unlock(&ctx->Shared->Mutex);
1574 if (early_out) {
1575 return;
1576 }
1577 }
1578
1579 /* flush before changing binding */
1580 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1581
1582 /* Do the actual binding. The refcount on the previously bound
1583 * texture object will be decremented. It'll be deleted if the
1584 * count hits zero.
1585 */
1586 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1587 ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
1588 ctx->Texture.CurrentUnit + 1);
1589 ASSERT(texUnit->CurrentTex[targetIndex]);
1590
1591 if (texName != 0)
1592 texUnit->_BoundTextures |= (1 << targetIndex);
1593 else
1594 texUnit->_BoundTextures &= ~(1 << targetIndex);
1595
1596 /* Pass BindTexture call to device driver */
1597 if (ctx->Driver.BindTexture)
1598 ctx->Driver.BindTexture(ctx, ctx->Texture.CurrentUnit, target, newTexObj);
1599 }
1600
1601
1602 void GLAPIENTRY
1603 _mesa_BindTextures(GLuint first, GLsizei count, const GLuint *textures)
1604 {
1605 GET_CURRENT_CONTEXT(ctx);
1606 GLint i;
1607
1608 /* The ARB_multi_bind spec says:
1609 *
1610 * "An INVALID_OPERATION error is generated if <first> + <count>
1611 * is greater than the number of texture image units supported
1612 * by the implementation."
1613 */
1614 if (first + count > ctx->Const.MaxCombinedTextureImageUnits) {
1615 _mesa_error(ctx, GL_INVALID_OPERATION,
1616 "glBindTextures(first=%u + count=%d > the value of "
1617 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
1618 first, count, ctx->Const.MaxCombinedTextureImageUnits);
1619 return;
1620 }
1621
1622 /* Flush before changing bindings */
1623 FLUSH_VERTICES(ctx, 0);
1624
1625 ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
1626 first + count);
1627
1628 if (textures) {
1629 /* Note that the error semantics for multi-bind commands differ from
1630 * those of other GL commands.
1631 *
1632 * The issues section in the ARB_multi_bind spec says:
1633 *
1634 * "(11) Typically, OpenGL specifies that if an error is generated by
1635 * a command, that command has no effect. This is somewhat
1636 * unfortunate for multi-bind commands, because it would require
1637 * a first pass to scan the entire list of bound objects for
1638 * errors and then a second pass to actually perform the
1639 * bindings. Should we have different error semantics?
1640 *
1641 * RESOLVED: Yes. In this specification, when the parameters for
1642 * one of the <count> binding points are invalid, that binding
1643 * point is not updated and an error will be generated. However,
1644 * other binding points in the same command will be updated if
1645 * their parameters are valid and no other error occurs."
1646 */
1647
1648 _mesa_begin_texture_lookups(ctx);
1649
1650 for (i = 0; i < count; i++) {
1651 if (textures[i] != 0) {
1652 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[first + i];
1653 struct gl_texture_object *current = texUnit->_Current;
1654 struct gl_texture_object *texObj;
1655
1656 if (current && current->Name == textures[i])
1657 texObj = current;
1658 else
1659 texObj = _mesa_lookup_texture_locked(ctx, textures[i]);
1660
1661 if (texObj && texObj->Target != 0) {
1662 const gl_texture_index targetIndex = texObj->TargetIndex;
1663
1664 if (texUnit->CurrentTex[targetIndex] != texObj) {
1665 /* Do the actual binding. The refcount on the previously
1666 * bound texture object will be decremented. It will be
1667 * deleted if the count hits zero.
1668 */
1669 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex],
1670 texObj);
1671
1672 texUnit->_BoundTextures |= (1 << targetIndex);
1673 ctx->NewState |= _NEW_TEXTURE;
1674
1675 /* Pass the BindTexture call to the device driver */
1676 if (ctx->Driver.BindTexture)
1677 ctx->Driver.BindTexture(ctx, first + i,
1678 texObj->Target, texObj);
1679 }
1680 } else {
1681 /* The ARB_multi_bind spec says:
1682 *
1683 * "An INVALID_OPERATION error is generated if any value
1684 * in <textures> is not zero or the name of an existing
1685 * texture object (per binding)."
1686 */
1687 _mesa_error(ctx, GL_INVALID_OPERATION,
1688 "glBindTextures(textures[%d]=%u is not zero "
1689 "or the name of an existing texture object)",
1690 i, textures[i]);
1691 }
1692 } else {
1693 unbind_textures_from_unit(ctx, first + i);
1694 }
1695 }
1696
1697 _mesa_end_texture_lookups(ctx);
1698 } else {
1699 /* Unbind all textures in the range <first> through <first>+<count>-1 */
1700 for (i = 0; i < count; i++)
1701 unbind_textures_from_unit(ctx, first + i);
1702 }
1703 }
1704
1705
1706 /**
1707 * Set texture priorities.
1708 *
1709 * \param n number of textures.
1710 * \param texName texture names.
1711 * \param priorities corresponding texture priorities.
1712 *
1713 * \sa glPrioritizeTextures().
1714 *
1715 * Looks up each texture in the hash, clamps the corresponding priority between
1716 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1717 */
1718 void GLAPIENTRY
1719 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1720 const GLclampf *priorities )
1721 {
1722 GET_CURRENT_CONTEXT(ctx);
1723 GLint i;
1724
1725 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1726 _mesa_debug(ctx, "glPrioritizeTextures %d\n", n);
1727
1728 FLUSH_VERTICES(ctx, 0);
1729
1730 if (n < 0) {
1731 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1732 return;
1733 }
1734
1735 if (!priorities)
1736 return;
1737
1738 for (i = 0; i < n; i++) {
1739 if (texName[i] > 0) {
1740 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1741 if (t) {
1742 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1743 }
1744 }
1745 }
1746
1747 ctx->NewState |= _NEW_TEXTURE;
1748 }
1749
1750
1751
1752 /**
1753 * See if textures are loaded in texture memory.
1754 *
1755 * \param n number of textures to query.
1756 * \param texName array with the texture names.
1757 * \param residences array which will hold the residence status.
1758 *
1759 * \return GL_TRUE if all textures are resident and
1760 * residences is left unchanged,
1761 *
1762 * Note: we assume all textures are always resident
1763 */
1764 GLboolean GLAPIENTRY
1765 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1766 GLboolean *residences)
1767 {
1768 GET_CURRENT_CONTEXT(ctx);
1769 GLboolean allResident = GL_TRUE;
1770 GLint i;
1771 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1772
1773 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1774 _mesa_debug(ctx, "glAreTexturesResident %d\n", n);
1775
1776 if (n < 0) {
1777 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1778 return GL_FALSE;
1779 }
1780
1781 if (!texName || !residences)
1782 return GL_FALSE;
1783
1784 /* We only do error checking on the texture names */
1785 for (i = 0; i < n; i++) {
1786 struct gl_texture_object *t;
1787 if (texName[i] == 0) {
1788 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1789 return GL_FALSE;
1790 }
1791 t = _mesa_lookup_texture(ctx, texName[i]);
1792 if (!t) {
1793 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1794 return GL_FALSE;
1795 }
1796 }
1797
1798 return allResident;
1799 }
1800
1801
1802 /**
1803 * See if a name corresponds to a texture.
1804 *
1805 * \param texture texture name.
1806 *
1807 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1808 * otherwise.
1809 *
1810 * \sa glIsTexture().
1811 *
1812 * Calls _mesa_HashLookup().
1813 */
1814 GLboolean GLAPIENTRY
1815 _mesa_IsTexture( GLuint texture )
1816 {
1817 struct gl_texture_object *t;
1818 GET_CURRENT_CONTEXT(ctx);
1819 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1820
1821 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1822 _mesa_debug(ctx, "glIsTexture %d\n", texture);
1823
1824 if (!texture)
1825 return GL_FALSE;
1826
1827 t = _mesa_lookup_texture(ctx, texture);
1828
1829 /* IsTexture is true only after object has been bound once. */
1830 return t && t->Target;
1831 }
1832
1833
1834 /**
1835 * Simplest implementation of texture locking: grab the shared tex
1836 * mutex. Examine the shared context state timestamp and if there has
1837 * been a change, set the appropriate bits in ctx->NewState.
1838 *
1839 * This is used to deal with synchronizing things when a texture object
1840 * is used/modified by different contexts (or threads) which are sharing
1841 * the texture.
1842 *
1843 * See also _mesa_lock/unlock_texture() in teximage.h
1844 */
1845 void
1846 _mesa_lock_context_textures( struct gl_context *ctx )
1847 {
1848 mtx_lock(&ctx->Shared->TexMutex);
1849
1850 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1851 ctx->NewState |= _NEW_TEXTURE;
1852 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1853 }
1854 }
1855
1856
1857 void
1858 _mesa_unlock_context_textures( struct gl_context *ctx )
1859 {
1860 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1861 mtx_unlock(&ctx->Shared->TexMutex);
1862 }
1863
1864
1865 void GLAPIENTRY
1866 _mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset,
1867 GLint yoffset, GLint zoffset, GLsizei width,
1868 GLsizei height, GLsizei depth)
1869 {
1870 struct gl_texture_object *t;
1871 struct gl_texture_image *image;
1872 GET_CURRENT_CONTEXT(ctx);
1873
1874 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1875 _mesa_debug(ctx, "glInvalidateTexSubImage %d\n", texture);
1876
1877 t = invalidate_tex_image_error_check(ctx, texture, level,
1878 "glInvalidateTexSubImage");
1879
1880 /* The GL_ARB_invalidate_subdata spec says:
1881 *
1882 * "...the specified subregion must be between -<b> and <dim>+<b> where
1883 * <dim> is the size of the dimension of the texture image, and <b> is
1884 * the size of the border of that texture image, otherwise
1885 * INVALID_VALUE is generated (border is not applied to dimensions that
1886 * don't exist in a given texture target)."
1887 */
1888 image = t->Image[0][level];
1889 if (image) {
1890 int xBorder;
1891 int yBorder;
1892 int zBorder;
1893 int imageWidth;
1894 int imageHeight;
1895 int imageDepth;
1896
1897 /* The GL_ARB_invalidate_subdata spec says:
1898 *
1899 * "For texture targets that don't have certain dimensions, this
1900 * command treats those dimensions as having a size of 1. For
1901 * example, to invalidate a portion of a two-dimensional texture,
1902 * the application would use <zoffset> equal to zero and <depth>
1903 * equal to one."
1904 */
1905 switch (t->Target) {
1906 case GL_TEXTURE_BUFFER:
1907 xBorder = 0;
1908 yBorder = 0;
1909 zBorder = 0;
1910 imageWidth = 1;
1911 imageHeight = 1;
1912 imageDepth = 1;
1913 break;
1914 case GL_TEXTURE_1D:
1915 xBorder = image->Border;
1916 yBorder = 0;
1917 zBorder = 0;
1918 imageWidth = image->Width;
1919 imageHeight = 1;
1920 imageDepth = 1;
1921 break;
1922 case GL_TEXTURE_1D_ARRAY:
1923 xBorder = image->Border;
1924 yBorder = 0;
1925 zBorder = 0;
1926 imageWidth = image->Width;
1927 imageHeight = image->Height;
1928 imageDepth = 1;
1929 break;
1930 case GL_TEXTURE_2D:
1931 case GL_TEXTURE_CUBE_MAP:
1932 case GL_TEXTURE_RECTANGLE:
1933 case GL_TEXTURE_2D_MULTISAMPLE:
1934 xBorder = image->Border;
1935 yBorder = image->Border;
1936 zBorder = 0;
1937 imageWidth = image->Width;
1938 imageHeight = image->Height;
1939 imageDepth = 1;
1940 break;
1941 case GL_TEXTURE_2D_ARRAY:
1942 case GL_TEXTURE_CUBE_MAP_ARRAY:
1943 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1944 xBorder = image->Border;
1945 yBorder = image->Border;
1946 zBorder = 0;
1947 imageWidth = image->Width;
1948 imageHeight = image->Height;
1949 imageDepth = image->Depth;
1950 break;
1951 case GL_TEXTURE_3D:
1952 xBorder = image->Border;
1953 yBorder = image->Border;
1954 zBorder = image->Border;
1955 imageWidth = image->Width;
1956 imageHeight = image->Height;
1957 imageDepth = image->Depth;
1958 break;
1959 default:
1960 assert(!"Should not get here.");
1961 xBorder = 0;
1962 yBorder = 0;
1963 zBorder = 0;
1964 imageWidth = 0;
1965 imageHeight = 0;
1966 imageDepth = 0;
1967 break;
1968 }
1969
1970 if (xoffset < -xBorder) {
1971 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(xoffset)");
1972 return;
1973 }
1974
1975 if (xoffset + width > imageWidth + xBorder) {
1976 _mesa_error(ctx, GL_INVALID_VALUE,
1977 "glInvalidateSubTexImage(xoffset+width)");
1978 return;
1979 }
1980
1981 if (yoffset < -yBorder) {
1982 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(yoffset)");
1983 return;
1984 }
1985
1986 if (yoffset + height > imageHeight + yBorder) {
1987 _mesa_error(ctx, GL_INVALID_VALUE,
1988 "glInvalidateSubTexImage(yoffset+height)");
1989 return;
1990 }
1991
1992 if (zoffset < -zBorder) {
1993 _mesa_error(ctx, GL_INVALID_VALUE,
1994 "glInvalidateSubTexImage(zoffset)");
1995 return;
1996 }
1997
1998 if (zoffset + depth > imageDepth + zBorder) {
1999 _mesa_error(ctx, GL_INVALID_VALUE,
2000 "glInvalidateSubTexImage(zoffset+depth)");
2001 return;
2002 }
2003 }
2004
2005 /* We don't actually do anything for this yet. Just return after
2006 * validating the parameters and generating the required errors.
2007 */
2008 return;
2009 }
2010
2011
2012 void GLAPIENTRY
2013 _mesa_InvalidateTexImage(GLuint texture, GLint level)
2014 {
2015 GET_CURRENT_CONTEXT(ctx);
2016
2017 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2018 _mesa_debug(ctx, "glInvalidateTexImage(%d, %d)\n", texture, level);
2019
2020 invalidate_tex_image_error_check(ctx, texture, level,
2021 "glInvalidateTexImage");
2022
2023 /* We don't actually do anything for this yet. Just return after
2024 * validating the parameters and generating the required errors.
2025 */
2026 return;
2027 }
2028
2029 /*@}*/