Merge remote branch 'origin/master' into radeon-rewrite
[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 "glheader.h"
32 #if FEATURE_colortable
33 #include "colortab.h"
34 #endif
35 #include "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "hash.h"
39 #include "imports.h"
40 #include "macros.h"
41 #include "teximage.h"
42 #include "texstate.h"
43 #include "texobj.h"
44 #include "mtypes.h"
45 #include "shader/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(GLcontext *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( GLcontext *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
111 _mesa_bzero(obj, sizeof(*obj));
112 /* init the non-zero fields */
113 _glthread_INIT_MUTEX(obj->Mutex);
114 obj->RefCount = 1;
115 obj->Name = name;
116 obj->Target = target;
117 obj->Priority = 1.0F;
118 if (target == GL_TEXTURE_RECTANGLE_NV) {
119 obj->WrapS = GL_CLAMP_TO_EDGE;
120 obj->WrapT = GL_CLAMP_TO_EDGE;
121 obj->WrapR = GL_CLAMP_TO_EDGE;
122 obj->MinFilter = GL_LINEAR;
123 }
124 else {
125 obj->WrapS = GL_REPEAT;
126 obj->WrapT = GL_REPEAT;
127 obj->WrapR = GL_REPEAT;
128 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
129 }
130 obj->MagFilter = GL_LINEAR;
131 obj->MinLod = -1000.0;
132 obj->MaxLod = 1000.0;
133 obj->LodBias = 0.0;
134 obj->BaseLevel = 0;
135 obj->MaxLevel = 1000;
136 obj->MaxAnisotropy = 1.0;
137 obj->CompareMode = GL_NONE; /* ARB_shadow */
138 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
139 obj->CompareFailValue = 0.0F; /* ARB_shadow_ambient */
140 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
141 obj->Swizzle[0] = GL_RED;
142 obj->Swizzle[1] = GL_GREEN;
143 obj->Swizzle[2] = GL_BLUE;
144 obj->Swizzle[3] = GL_ALPHA;
145 obj->_Swizzle = SWIZZLE_NOOP;
146 }
147
148
149 /**
150 * Some texture initialization can't be finished until we know which
151 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
152 */
153 static void
154 finish_texture_init(GLcontext *ctx, GLenum target,
155 struct gl_texture_object *obj)
156 {
157 assert(obj->Target == 0);
158
159 if (target == GL_TEXTURE_RECTANGLE_NV) {
160 /* have to init wrap and filter state here - kind of klunky */
161 obj->WrapS = GL_CLAMP_TO_EDGE;
162 obj->WrapT = GL_CLAMP_TO_EDGE;
163 obj->WrapR = GL_CLAMP_TO_EDGE;
164 obj->MinFilter = GL_LINEAR;
165 if (ctx->Driver.TexParameter) {
166 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
167 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
168 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
169 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
170 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
171 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
172 }
173 }
174 }
175
176
177 /**
178 * Deallocate a texture object struct. It should have already been
179 * removed from the texture object pool.
180 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
181 *
182 * \param shared the shared GL state to which the object belongs.
183 * \param texOjb the texture object to delete.
184 */
185 void
186 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
187 {
188 GLuint i, face;
189
190 (void) ctx;
191
192 /* Set Target to an invalid value. With some assertions elsewhere
193 * we can try to detect possible use of deleted textures.
194 */
195 texObj->Target = 0x99;
196
197 #if FEATURE_colortable
198 _mesa_free_colortable_data(&texObj->Palette);
199 #endif
200
201 /* free the texture images */
202 for (face = 0; face < 6; face++) {
203 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
204 if (texObj->Image[face][i]) {
205 _mesa_delete_texture_image( ctx, texObj->Image[face][i] );
206 }
207 }
208 }
209
210 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
211 _glthread_DESTROY_MUTEX(texObj->Mutex);
212
213 /* free this object */
214 _mesa_free(texObj);
215 }
216
217
218
219
220 /**
221 * Copy texture object state from one texture object to another.
222 * Use for glPush/PopAttrib.
223 *
224 * \param dest destination texture object.
225 * \param src source texture object.
226 */
227 void
228 _mesa_copy_texture_object( struct gl_texture_object *dest,
229 const struct gl_texture_object *src )
230 {
231 dest->Target = src->Target;
232 dest->Name = src->Name;
233 dest->Priority = src->Priority;
234 dest->BorderColor[0] = src->BorderColor[0];
235 dest->BorderColor[1] = src->BorderColor[1];
236 dest->BorderColor[2] = src->BorderColor[2];
237 dest->BorderColor[3] = src->BorderColor[3];
238 dest->WrapS = src->WrapS;
239 dest->WrapT = src->WrapT;
240 dest->WrapR = src->WrapR;
241 dest->MinFilter = src->MinFilter;
242 dest->MagFilter = src->MagFilter;
243 dest->MinLod = src->MinLod;
244 dest->MaxLod = src->MaxLod;
245 dest->LodBias = src->LodBias;
246 dest->BaseLevel = src->BaseLevel;
247 dest->MaxLevel = src->MaxLevel;
248 dest->MaxAnisotropy = src->MaxAnisotropy;
249 dest->CompareMode = src->CompareMode;
250 dest->CompareFunc = src->CompareFunc;
251 dest->CompareFailValue = src->CompareFailValue;
252 dest->DepthMode = src->DepthMode;
253 dest->_MaxLevel = src->_MaxLevel;
254 dest->_MaxLambda = src->_MaxLambda;
255 dest->GenerateMipmap = src->GenerateMipmap;
256 dest->Palette = src->Palette;
257 dest->_Complete = src->_Complete;
258 COPY_4V(dest->Swizzle, src->Swizzle);
259 dest->_Swizzle = src->_Swizzle;
260 }
261
262
263 /**
264 * Check if the given texture object is valid by examining its Target field.
265 * For debugging only.
266 */
267 static GLboolean
268 valid_texture_object(const struct gl_texture_object *tex)
269 {
270 switch (tex->Target) {
271 case 0:
272 case GL_TEXTURE_1D:
273 case GL_TEXTURE_2D:
274 case GL_TEXTURE_3D:
275 case GL_TEXTURE_CUBE_MAP_ARB:
276 case GL_TEXTURE_RECTANGLE_NV:
277 case GL_TEXTURE_1D_ARRAY_EXT:
278 case GL_TEXTURE_2D_ARRAY_EXT:
279 return GL_TRUE;
280 case 0x99:
281 _mesa_problem(NULL, "invalid reference to a deleted texture object");
282 return GL_FALSE;
283 default:
284 _mesa_problem(NULL, "invalid texture object Target value");
285 return GL_FALSE;
286 }
287 }
288
289
290 /**
291 * Reference (or unreference) a texture object.
292 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
293 * If 'tex' is non-null, increment its refcount.
294 */
295 void
296 _mesa_reference_texobj(struct gl_texture_object **ptr,
297 struct gl_texture_object *tex)
298 {
299 assert(ptr);
300 if (*ptr == tex) {
301 /* no change */
302 return;
303 }
304
305 if (*ptr) {
306 /* Unreference the old texture */
307 GLboolean deleteFlag = GL_FALSE;
308 struct gl_texture_object *oldTex = *ptr;
309
310 assert(valid_texture_object(oldTex));
311
312 _glthread_LOCK_MUTEX(oldTex->Mutex);
313 ASSERT(oldTex->RefCount > 0);
314 oldTex->RefCount--;
315
316 deleteFlag = (oldTex->RefCount == 0);
317 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
318
319 if (deleteFlag) {
320 GET_CURRENT_CONTEXT(ctx);
321 if (ctx)
322 ctx->Driver.DeleteTexture(ctx, oldTex);
323 else
324 _mesa_problem(NULL, "Unable to delete texture, no context");
325 }
326
327 *ptr = NULL;
328 }
329 assert(!*ptr);
330
331 if (tex) {
332 /* reference new texture */
333 assert(valid_texture_object(tex));
334 _glthread_LOCK_MUTEX(tex->Mutex);
335 if (tex->RefCount == 0) {
336 /* this texture's being deleted (look just above) */
337 /* Not sure this can every really happen. Warn if it does. */
338 _mesa_problem(NULL, "referencing deleted texture object");
339 *ptr = NULL;
340 }
341 else {
342 tex->RefCount++;
343 *ptr = tex;
344 }
345 _glthread_UNLOCK_MUTEX(tex->Mutex);
346 }
347 }
348
349
350
351 /**
352 * Report why a texture object is incomplete.
353 *
354 * \param t texture object.
355 * \param why string describing why it's incomplete.
356 *
357 * \note For debug purposes only.
358 */
359 #if 0
360 static void
361 incomplete(const struct gl_texture_object *t, const char *why)
362 {
363 _mesa_printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
364 }
365 #else
366 #define incomplete(t, why)
367 #endif
368
369
370 /**
371 * Examine a texture object to determine if it is complete.
372 *
373 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
374 * accordingly.
375 *
376 * \param ctx GL context.
377 * \param t texture object.
378 *
379 * According to the texture target, verifies that each of the mipmaps is
380 * present and has the expected size.
381 */
382 void
383 _mesa_test_texobj_completeness( const GLcontext *ctx,
384 struct gl_texture_object *t )
385 {
386 const GLint baseLevel = t->BaseLevel;
387 GLint maxLog2 = 0, maxLevels = 0;
388
389 t->_Complete = GL_TRUE; /* be optimistic */
390
391 /* Detect cases where the application set the base level to an invalid
392 * value.
393 */
394 if ((baseLevel < 0) || (baseLevel > MAX_TEXTURE_LEVELS)) {
395 char s[100];
396 _mesa_sprintf(s, "base level = %d is invalid", baseLevel);
397 incomplete(t, s);
398 t->_Complete = GL_FALSE;
399 return;
400 }
401
402 /* Always need the base level image */
403 if (!t->Image[0][baseLevel]) {
404 char s[100];
405 _mesa_sprintf(s, "Image[baseLevel=%d] == NULL", baseLevel);
406 incomplete(t, s);
407 t->_Complete = GL_FALSE;
408 return;
409 }
410
411 /* Check width/height/depth for zero */
412 if (t->Image[0][baseLevel]->Width == 0 ||
413 t->Image[0][baseLevel]->Height == 0 ||
414 t->Image[0][baseLevel]->Depth == 0) {
415 incomplete(t, "texture width = 0");
416 t->_Complete = GL_FALSE;
417 return;
418 }
419
420 /* Compute _MaxLevel */
421 if ((t->Target == GL_TEXTURE_1D) ||
422 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
423 maxLog2 = t->Image[0][baseLevel]->WidthLog2;
424 maxLevels = ctx->Const.MaxTextureLevels;
425 }
426 else if ((t->Target == GL_TEXTURE_2D) ||
427 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
428 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
429 t->Image[0][baseLevel]->HeightLog2);
430 maxLevels = ctx->Const.MaxTextureLevels;
431 }
432 else if (t->Target == GL_TEXTURE_3D) {
433 GLint max = MAX2(t->Image[0][baseLevel]->WidthLog2,
434 t->Image[0][baseLevel]->HeightLog2);
435 maxLog2 = MAX2(max, (GLint)(t->Image[0][baseLevel]->DepthLog2));
436 maxLevels = ctx->Const.Max3DTextureLevels;
437 }
438 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
439 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
440 t->Image[0][baseLevel]->HeightLog2);
441 maxLevels = ctx->Const.MaxCubeTextureLevels;
442 }
443 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
444 maxLog2 = 0; /* not applicable */
445 maxLevels = 1; /* no mipmapping */
446 }
447 else {
448 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
449 return;
450 }
451
452 ASSERT(maxLevels > 0);
453
454 t->_MaxLevel = baseLevel + maxLog2;
455 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
456 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
457
458 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
459 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
460
461 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
462 /* make sure that all six cube map level 0 images are the same size */
463 const GLuint w = t->Image[0][baseLevel]->Width2;
464 const GLuint h = t->Image[0][baseLevel]->Height2;
465 GLuint face;
466 for (face = 1; face < 6; face++) {
467 if (t->Image[face][baseLevel] == NULL ||
468 t->Image[face][baseLevel]->Width2 != w ||
469 t->Image[face][baseLevel]->Height2 != h) {
470 t->_Complete = GL_FALSE;
471 incomplete(t, "Non-quare cubemap image");
472 return;
473 }
474 }
475 }
476
477 /* extra checking for mipmaps */
478 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
479 /*
480 * Mipmapping: determine if we have a complete set of mipmaps
481 */
482 GLint i;
483 GLint minLevel = baseLevel;
484 GLint maxLevel = t->_MaxLevel;
485
486 if (minLevel > maxLevel) {
487 t->_Complete = GL_FALSE;
488 incomplete(t, "minLevel > maxLevel");
489 return;
490 }
491
492 /* Test dimension-independent attributes */
493 for (i = minLevel; i <= maxLevel; i++) {
494 if (t->Image[0][i]) {
495 if (t->Image[0][i]->TexFormat != t->Image[0][baseLevel]->TexFormat) {
496 t->_Complete = GL_FALSE;
497 incomplete(t, "Format[i] != Format[baseLevel]");
498 return;
499 }
500 if (t->Image[0][i]->Border != t->Image[0][baseLevel]->Border) {
501 t->_Complete = GL_FALSE;
502 incomplete(t, "Border[i] != Border[baseLevel]");
503 return;
504 }
505 }
506 }
507
508 /* Test things which depend on number of texture image dimensions */
509 if ((t->Target == GL_TEXTURE_1D) ||
510 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
511 /* Test 1-D mipmaps */
512 GLuint width = t->Image[0][baseLevel]->Width2;
513 for (i = baseLevel + 1; i < maxLevels; i++) {
514 if (width > 1) {
515 width /= 2;
516 }
517 if (i >= minLevel && i <= maxLevel) {
518 if (!t->Image[0][i]) {
519 t->_Complete = GL_FALSE;
520 incomplete(t, "1D Image[0][i] == NULL");
521 return;
522 }
523 if (t->Image[0][i]->Width2 != width ) {
524 t->_Complete = GL_FALSE;
525 incomplete(t, "1D Image[0][i] bad width");
526 return;
527 }
528 }
529 if (width == 1) {
530 return; /* found smallest needed mipmap, all done! */
531 }
532 }
533 }
534 else if ((t->Target == GL_TEXTURE_2D) ||
535 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
536 /* Test 2-D mipmaps */
537 GLuint width = t->Image[0][baseLevel]->Width2;
538 GLuint height = t->Image[0][baseLevel]->Height2;
539 for (i = baseLevel + 1; i < maxLevels; i++) {
540 if (width > 1) {
541 width /= 2;
542 }
543 if (height > 1) {
544 height /= 2;
545 }
546 if (i >= minLevel && i <= maxLevel) {
547 if (!t->Image[0][i]) {
548 t->_Complete = GL_FALSE;
549 incomplete(t, "2D Image[0][i] == NULL");
550 return;
551 }
552 if (t->Image[0][i]->Width2 != width) {
553 t->_Complete = GL_FALSE;
554 incomplete(t, "2D Image[0][i] bad width");
555 return;
556 }
557 if (t->Image[0][i]->Height2 != height) {
558 t->_Complete = GL_FALSE;
559 incomplete(t, "2D Image[0][i] bad height");
560 return;
561 }
562 if (width==1 && height==1) {
563 return; /* found smallest needed mipmap, all done! */
564 }
565 }
566 }
567 }
568 else if (t->Target == GL_TEXTURE_3D) {
569 /* Test 3-D mipmaps */
570 GLuint width = t->Image[0][baseLevel]->Width2;
571 GLuint height = t->Image[0][baseLevel]->Height2;
572 GLuint depth = t->Image[0][baseLevel]->Depth2;
573 for (i = baseLevel + 1; i < maxLevels; i++) {
574 if (width > 1) {
575 width /= 2;
576 }
577 if (height > 1) {
578 height /= 2;
579 }
580 if (depth > 1) {
581 depth /= 2;
582 }
583 if (i >= minLevel && i <= maxLevel) {
584 if (!t->Image[0][i]) {
585 incomplete(t, "3D Image[0][i] == NULL");
586 t->_Complete = GL_FALSE;
587 return;
588 }
589 if (t->Image[0][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
590 t->_Complete = GL_FALSE;
591 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
592 return;
593 }
594 if (t->Image[0][i]->Width2 != width) {
595 t->_Complete = GL_FALSE;
596 incomplete(t, "3D Image[0][i] bad width");
597 return;
598 }
599 if (t->Image[0][i]->Height2 != height) {
600 t->_Complete = GL_FALSE;
601 incomplete(t, "3D Image[0][i] bad height");
602 return;
603 }
604 if (t->Image[0][i]->Depth2 != depth) {
605 t->_Complete = GL_FALSE;
606 incomplete(t, "3D Image[0][i] bad depth");
607 return;
608 }
609 }
610 if (width == 1 && height == 1 && depth == 1) {
611 return; /* found smallest needed mipmap, all done! */
612 }
613 }
614 }
615 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
616 /* make sure 6 cube faces are consistant */
617 GLuint width = t->Image[0][baseLevel]->Width2;
618 GLuint height = t->Image[0][baseLevel]->Height2;
619 for (i = baseLevel + 1; i < maxLevels; i++) {
620 if (width > 1) {
621 width /= 2;
622 }
623 if (height > 1) {
624 height /= 2;
625 }
626 if (i >= minLevel && i <= maxLevel) {
627 GLuint face;
628 for (face = 0; face < 6; face++) {
629 /* check that we have images defined */
630 if (!t->Image[face][i]) {
631 t->_Complete = GL_FALSE;
632 incomplete(t, "CubeMap Image[n][i] == NULL");
633 return;
634 }
635 /* Don't support GL_DEPTH_COMPONENT for cube maps */
636 if (t->Image[face][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
637 t->_Complete = GL_FALSE;
638 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
639 return;
640 }
641 /* check that all six images have same size */
642 if (t->Image[face][i]->Width2!=width ||
643 t->Image[face][i]->Height2!=height) {
644 t->_Complete = GL_FALSE;
645 incomplete(t, "CubeMap Image[n][i] bad size");
646 return;
647 }
648 }
649 }
650 if (width == 1 && height == 1) {
651 return; /* found smallest needed mipmap, all done! */
652 }
653 }
654 }
655 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
656 /* XXX special checking? */
657 }
658 else {
659 /* Target = ??? */
660 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
661 }
662 }
663 }
664
665 /*@}*/
666
667
668 /***********************************************************************/
669 /** \name API functions */
670 /*@{*/
671
672
673 /**
674 * Generate texture names.
675 *
676 * \param n number of texture names to be generated.
677 * \param textures an array in which will hold the generated texture names.
678 *
679 * \sa glGenTextures().
680 *
681 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
682 * IDs which are stored in \p textures. Corresponding empty texture
683 * objects are also generated.
684 */
685 void GLAPIENTRY
686 _mesa_GenTextures( GLsizei n, GLuint *textures )
687 {
688 GET_CURRENT_CONTEXT(ctx);
689 GLuint first;
690 GLint i;
691 ASSERT_OUTSIDE_BEGIN_END(ctx);
692
693 if (n < 0) {
694 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
695 return;
696 }
697
698 if (!textures)
699 return;
700
701 /*
702 * This must be atomic (generation and allocation of texture IDs)
703 */
704 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
705
706 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
707
708 /* Allocate new, empty texture objects */
709 for (i = 0; i < n; i++) {
710 struct gl_texture_object *texObj;
711 GLuint name = first + i;
712 GLenum target = 0;
713 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
714 if (!texObj) {
715 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
716 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
717 return;
718 }
719
720 /* insert into hash table */
721 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
722
723 textures[i] = name;
724 }
725
726 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
727 }
728
729
730 /**
731 * Check if the given texture object is bound to the current draw or
732 * read framebuffer. If so, Unbind it.
733 */
734 static void
735 unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
736 {
737 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
738 GLuint i;
739
740 for (i = 0; i < n; i++) {
741 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
742 if (fb->Name) {
743 GLuint j;
744 for (j = 0; j < BUFFER_COUNT; j++) {
745 if (fb->Attachment[j].Type == GL_TEXTURE &&
746 fb->Attachment[j].Texture == texObj) {
747 _mesa_remove_attachment(ctx, fb->Attachment + j);
748 }
749 }
750 }
751 }
752 }
753
754
755 /**
756 * Check if the given texture object is bound to any texture image units and
757 * unbind it if so (revert to default textures).
758 */
759 static void
760 unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
761 {
762 GLuint u;
763
764 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
765 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
766 if (texObj == unit->Current1D) {
767 _mesa_reference_texobj(&unit->Current1D, ctx->Shared->Default1D);
768 ASSERT(unit->Current1D);
769 }
770 else if (texObj == unit->Current2D) {
771 _mesa_reference_texobj(&unit->Current2D, ctx->Shared->Default2D);
772 ASSERT(unit->Current2D);
773 }
774 else if (texObj == unit->Current3D) {
775 _mesa_reference_texobj(&unit->Current3D, ctx->Shared->Default3D);
776 ASSERT(unit->Current3D);
777 }
778 else if (texObj == unit->CurrentCubeMap) {
779 _mesa_reference_texobj(&unit->CurrentCubeMap, ctx->Shared->DefaultCubeMap);
780 ASSERT(unit->CurrentCubeMap);
781 }
782 else if (texObj == unit->CurrentRect) {
783 _mesa_reference_texobj(&unit->CurrentRect, ctx->Shared->DefaultRect);
784 ASSERT(unit->CurrentRect);
785 }
786 else if (texObj == unit->Current1DArray) {
787 _mesa_reference_texobj(&unit->Current1DArray, ctx->Shared->Default1DArray);
788 ASSERT(unit->Current1DArray);
789 }
790 else if (texObj == unit->Current2DArray) {
791 _mesa_reference_texobj(&unit->Current2DArray, ctx->Shared->Default2DArray);
792 ASSERT(unit->Current2DArray);
793 }
794 }
795 }
796
797
798 /**
799 * Delete named textures.
800 *
801 * \param n number of textures to be deleted.
802 * \param textures array of texture IDs to be deleted.
803 *
804 * \sa glDeleteTextures().
805 *
806 * If we're about to delete a texture that's currently bound to any
807 * texture unit, unbind the texture first. Decrement the reference
808 * count on the texture object and delete it if it's zero.
809 * Recall that texture objects can be shared among several rendering
810 * contexts.
811 */
812 void GLAPIENTRY
813 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
814 {
815 GET_CURRENT_CONTEXT(ctx);
816 GLint i;
817 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
818
819 if (!textures)
820 return;
821
822 for (i = 0; i < n; i++) {
823 if (textures[i] > 0) {
824 struct gl_texture_object *delObj
825 = _mesa_lookup_texture(ctx, textures[i]);
826
827 if (delObj) {
828 _mesa_lock_texture(ctx, delObj);
829
830 /* Check if texture is bound to any framebuffer objects.
831 * If so, unbind.
832 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
833 */
834 unbind_texobj_from_fbo(ctx, delObj);
835
836 /* Check if this texture is currently bound to any texture units.
837 * If so, unbind it.
838 */
839 unbind_texobj_from_texunits(ctx, delObj);
840
841 _mesa_unlock_texture(ctx, delObj);
842
843 ctx->NewState |= _NEW_TEXTURE;
844
845 /* The texture _name_ is now free for re-use.
846 * Remove it from the hash table now.
847 */
848 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
849 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
850 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
851
852 /* Unreference the texobj. If refcount hits zero, the texture
853 * will be deleted.
854 */
855 _mesa_reference_texobj(&delObj, NULL);
856 }
857 }
858 }
859 }
860
861
862 /**
863 * Bind a named texture to a texturing target.
864 *
865 * \param target texture target.
866 * \param texName texture name.
867 *
868 * \sa glBindTexture().
869 *
870 * Determines the old texture object bound and returns immediately if rebinding
871 * the same texture. Get the current texture which is either a default texture
872 * if name is null, a named texture from the hash, or a new texture if the
873 * given texture name is new. Increments its reference count, binds it, and
874 * calls dd_function_table::BindTexture. Decrements the old texture reference
875 * count and deletes it if it reaches zero.
876 */
877 void GLAPIENTRY
878 _mesa_BindTexture( GLenum target, GLuint texName )
879 {
880 GET_CURRENT_CONTEXT(ctx);
881 const GLuint unit = ctx->Texture.CurrentUnit;
882 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
883 struct gl_texture_object *newTexObj = NULL, *defaultTexObj = NULL;
884 ASSERT_OUTSIDE_BEGIN_END(ctx);
885
886 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
887 _mesa_debug(ctx, "glBindTexture %s %d\n",
888 _mesa_lookup_enum_by_nr(target), (GLint) texName);
889
890 switch (target) {
891 case GL_TEXTURE_1D:
892 defaultTexObj = ctx->Shared->Default1D;
893 break;
894 case GL_TEXTURE_2D:
895 defaultTexObj = ctx->Shared->Default2D;
896 break;
897 case GL_TEXTURE_3D:
898 defaultTexObj = ctx->Shared->Default3D;
899 break;
900 case GL_TEXTURE_CUBE_MAP_ARB:
901 defaultTexObj = ctx->Shared->DefaultCubeMap;
902 break;
903 case GL_TEXTURE_RECTANGLE_NV:
904 defaultTexObj = ctx->Shared->DefaultRect;
905 break;
906 case GL_TEXTURE_1D_ARRAY_EXT:
907 defaultTexObj = ctx->Shared->Default1DArray;
908 break;
909 case GL_TEXTURE_2D_ARRAY_EXT:
910 defaultTexObj = ctx->Shared->Default2DArray;
911 break;
912 default:
913 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
914 return;
915 }
916
917 /*
918 * Get pointer to new texture object (newTexObj)
919 */
920 if (texName == 0) {
921 newTexObj = defaultTexObj;
922 }
923 else {
924 /* non-default texture object */
925 newTexObj = _mesa_lookup_texture(ctx, texName);
926 if (newTexObj) {
927 /* error checking */
928 if (newTexObj->Target != 0 && newTexObj->Target != target) {
929 /* the named texture object's target doesn't match the given target */
930 _mesa_error( ctx, GL_INVALID_OPERATION,
931 "glBindTexture(target mismatch)" );
932 return;
933 }
934 if (newTexObj->Target == 0) {
935 finish_texture_init(ctx, target, newTexObj);
936 }
937 }
938 else {
939 /* if this is a new texture id, allocate a texture object now */
940 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
941 if (!newTexObj) {
942 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
943 return;
944 }
945
946 /* and insert it into hash table */
947 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
948 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
949 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
950 }
951 newTexObj->Target = target;
952 }
953
954 assert(valid_texture_object(newTexObj));
955
956 /* flush before changing binding */
957 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
958
959 /* Do the actual binding. The refcount on the previously bound
960 * texture object will be decremented. It'll be deleted if the
961 * count hits zero.
962 */
963 switch (target) {
964 case GL_TEXTURE_1D:
965 _mesa_reference_texobj(&texUnit->Current1D, newTexObj);
966 ASSERT(texUnit->Current1D);
967 break;
968 case GL_TEXTURE_2D:
969 _mesa_reference_texobj(&texUnit->Current2D, newTexObj);
970 ASSERT(texUnit->Current2D);
971 break;
972 case GL_TEXTURE_3D:
973 _mesa_reference_texobj(&texUnit->Current3D, newTexObj);
974 ASSERT(texUnit->Current3D);
975 break;
976 case GL_TEXTURE_CUBE_MAP_ARB:
977 _mesa_reference_texobj(&texUnit->CurrentCubeMap, newTexObj);
978 ASSERT(texUnit->CurrentCubeMap);
979 break;
980 case GL_TEXTURE_RECTANGLE_NV:
981 _mesa_reference_texobj(&texUnit->CurrentRect, newTexObj);
982 ASSERT(texUnit->CurrentRect);
983 break;
984 case GL_TEXTURE_1D_ARRAY_EXT:
985 _mesa_reference_texobj(&texUnit->Current1DArray, newTexObj);
986 ASSERT(texUnit->Current1DArray);
987 break;
988 case GL_TEXTURE_2D_ARRAY_EXT:
989 _mesa_reference_texobj(&texUnit->Current2DArray, newTexObj);
990 ASSERT(texUnit->Current2DArray);
991 break;
992 default:
993 /* Bad target should be caught above */
994 _mesa_problem(ctx, "bad target in BindTexture");
995 return;
996 }
997
998 /* Pass BindTexture call to device driver */
999 if (ctx->Driver.BindTexture)
1000 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
1001 }
1002
1003
1004 /**
1005 * Set texture priorities.
1006 *
1007 * \param n number of textures.
1008 * \param texName texture names.
1009 * \param priorities corresponding texture priorities.
1010 *
1011 * \sa glPrioritizeTextures().
1012 *
1013 * Looks up each texture in the hash, clamps the corresponding priority between
1014 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1015 */
1016 void GLAPIENTRY
1017 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1018 const GLclampf *priorities )
1019 {
1020 GET_CURRENT_CONTEXT(ctx);
1021 GLint i;
1022 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1023
1024 if (n < 0) {
1025 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1026 return;
1027 }
1028
1029 if (!priorities)
1030 return;
1031
1032 for (i = 0; i < n; i++) {
1033 if (texName[i] > 0) {
1034 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1035 if (t) {
1036 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1037 if (ctx->Driver.PrioritizeTexture)
1038 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
1039 }
1040 }
1041 }
1042
1043 ctx->NewState |= _NEW_TEXTURE;
1044 }
1045
1046 /**
1047 * See if textures are loaded in texture memory.
1048 *
1049 * \param n number of textures to query.
1050 * \param texName array with the texture names.
1051 * \param residences array which will hold the residence status.
1052 *
1053 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1054 *
1055 * \sa glAreTexturesResident().
1056 *
1057 * Looks up each texture in the hash and calls
1058 * dd_function_table::IsTextureResident.
1059 */
1060 GLboolean GLAPIENTRY
1061 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1062 GLboolean *residences)
1063 {
1064 GET_CURRENT_CONTEXT(ctx);
1065 GLboolean allResident = GL_TRUE;
1066 GLint i, j;
1067 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1068
1069 if (n < 0) {
1070 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1071 return GL_FALSE;
1072 }
1073
1074 if (!texName || !residences)
1075 return GL_FALSE;
1076
1077 for (i = 0; i < n; i++) {
1078 struct gl_texture_object *t;
1079 if (texName[i] == 0) {
1080 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1081 return GL_FALSE;
1082 }
1083 t = _mesa_lookup_texture(ctx, texName[i]);
1084 if (!t) {
1085 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1086 return GL_FALSE;
1087 }
1088 if (!ctx->Driver.IsTextureResident ||
1089 ctx->Driver.IsTextureResident(ctx, t)) {
1090 /* The texture is resident */
1091 if (!allResident)
1092 residences[i] = GL_TRUE;
1093 }
1094 else {
1095 /* The texture is not resident */
1096 if (allResident) {
1097 allResident = GL_FALSE;
1098 for (j = 0; j < i; j++)
1099 residences[j] = GL_TRUE;
1100 }
1101 residences[i] = GL_FALSE;
1102 }
1103 }
1104
1105 return allResident;
1106 }
1107
1108 /**
1109 * See if a name corresponds to a texture.
1110 *
1111 * \param texture texture name.
1112 *
1113 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1114 * otherwise.
1115 *
1116 * \sa glIsTexture().
1117 *
1118 * Calls _mesa_HashLookup().
1119 */
1120 GLboolean GLAPIENTRY
1121 _mesa_IsTexture( GLuint texture )
1122 {
1123 struct gl_texture_object *t;
1124 GET_CURRENT_CONTEXT(ctx);
1125 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1126
1127 if (!texture)
1128 return GL_FALSE;
1129
1130 t = _mesa_lookup_texture(ctx, texture);
1131
1132 /* IsTexture is true only after object has been bound once. */
1133 return t && t->Target;
1134 }
1135
1136
1137 /**
1138 * Simplest implementation of texture locking: Grab the a new mutex in
1139 * the shared context. Examine the shared context state timestamp and
1140 * if there has been a change, set the appropriate bits in
1141 * ctx->NewState.
1142 *
1143 * This is used to deal with synchronizing things when a texture object
1144 * is used/modified by different contexts (or threads) which are sharing
1145 * the texture.
1146 *
1147 * See also _mesa_lock/unlock_texture() in teximage.h
1148 */
1149 void
1150 _mesa_lock_context_textures( GLcontext *ctx )
1151 {
1152 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1153
1154 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1155 ctx->NewState |= _NEW_TEXTURE;
1156 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1157 }
1158 }
1159
1160
1161 void
1162 _mesa_unlock_context_textures( GLcontext *ctx )
1163 {
1164 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1165 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1166 }
1167
1168 /*@}*/
1169
1170