OSDN Git Service

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