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